Skip to content

Commit fbfc1fe

Browse files
committed
Preserve error codes for invariants on www
1 parent 7a3416f commit fbfc1fe

File tree

6 files changed

+68
-0
lines changed

6 files changed

+68
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright (c) 2013-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
const invariant = require('invariant');
11+
12+
function reactProdInvariant(code: string): void {
13+
const argCount = arguments.length - 1;
14+
15+
let message =
16+
'Minified React error #' +
17+
code +
18+
'; visit ' +
19+
'http://reactjs.org/docs/error-decoder.html?invariant=' +
20+
code;
21+
22+
for (let argIdx = 0; argIdx < argCount; argIdx++) {
23+
message += '&args[]=' + encodeURIComponent(arguments[argIdx + 1]);
24+
}
25+
26+
message +=
27+
' for the full message or use the non-minified dev environment' +
28+
' for full errors and additional helpful warnings.';
29+
30+
// www doesn't strip this because we mark the React bundle
31+
// with @preserve-invariant-messages docblock.
32+
// eslint-disable-next-line react-internal/warning-and-invariant-args
33+
invariant(false, message);
34+
}
35+
36+
export default reactProdInvariant;

packages/shared/reactProdInvariant.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ function reactProdInvariant(code: string): void {
3131
' for the full message or use the non-minified dev environment' +
3232
' for full errors and additional helpful warnings.';
3333

34+
// Note: if you update the code above, don't forget
35+
// to update the www fork in forks/reactProdInvariant.www.js.
36+
3437
const error: Error & {framesToPop?: number} = new Error(message);
3538
error.name = 'Invariant Violation';
3639
error.framesToPop = 1; // we don't care about reactProdInvariant's own frame

scripts/error-codes/replace-invariant-error-codes.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
'use strict';
88

99
const fs = require('fs');
10+
const {basename} = require('path');
1011
const evalToString = require('../shared/evalToString');
1112
const invertObject = require('./invertObject');
1213

@@ -37,6 +38,13 @@ module.exports = function(babel) {
3738
visitor: {
3839
CallExpression: {
3940
exit(path, file) {
41+
if (
42+
basename(file.file.opts.filename) === 'reactProdInvariant.www.js'
43+
) {
44+
// In the www fork, `invariant` refers to the www invariant implementation.
45+
// So we should not attempt to replace it.
46+
return;
47+
}
4048
const node = path.node;
4149
// Ignore if it's already been processed
4250
if (node[SEEN_SYMBOL]) {

scripts/rollup/build.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ function getBabelConfig(updateBabelOptions, bundleType, filename) {
8282
switch (bundleType) {
8383
case FB_DEV:
8484
case FB_PROD:
85+
return Object.assign({}, options, {
86+
plugins: options.plugins.concat([
87+
// Minify invariant messages
88+
require('../error-codes/replace-invariant-error-codes'),
89+
// Wrap warning() calls in a __DEV__ check so they are stripped from production.
90+
require('../babel/wrap-warning-with-env-check'),
91+
]),
92+
});
8593
case RN_DEV:
8694
case RN_PROD:
8795
return Object.assign({}, options, {

scripts/rollup/forks.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ const forks = Object.freeze({
8484
}
8585
},
8686

87+
// Route production invariants on www through the www invariant module.
88+
'shared/reactProdInvariant': (bundleType, entry) => {
89+
switch (bundleType) {
90+
case FB_DEV:
91+
case FB_PROD:
92+
return 'shared/forks/reactProdInvariant.www.js';
93+
default:
94+
return null;
95+
}
96+
},
97+
8798
// Different dialogs for caught errors.
8899
'react-reconciler/src/ReactFiberErrorDialog': (bundleType, entry) => {
89100
switch (bundleType) {

scripts/rollup/wrappers.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ ${license}
9494
*
9595
* @noflow
9696
* @preventMunge
97+
* @preserve-invariant-messages
9798
*/
9899
99100
'use strict';
@@ -112,6 +113,7 @@ ${license}
112113
*
113114
* @noflow
114115
* @preventMunge
116+
* @preserve-invariant-messages
115117
*/
116118
117119
${source}`;

0 commit comments

Comments
 (0)