Skip to content

Commit

Permalink
Preserve error codes for invariants on www (#12539)
Browse files Browse the repository at this point in the history
* Preserve error codes for invariants on www

* Remove unintentional change
  • Loading branch information
gaearon committed Apr 9, 2018
1 parent ea37545 commit 76b4ba0
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
38 changes: 38 additions & 0 deletions packages/shared/forks/reactProdInvariant.www.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

const invariant = require('invariant');

function reactProdInvariant(code: string): void {
const argCount = arguments.length - 1;

let message =
'Minified React error #' +
code +
'; visit ' +
'http://reactjs.org/docs/error-decoder.html?invariant=' +
code;

for (let argIdx = 0; argIdx < argCount; argIdx++) {
message += '&args[]=' + encodeURIComponent(arguments[argIdx + 1]);
}

message +=
' for the full message or use the non-minified dev environment' +
' for full errors and additional helpful warnings.';

// www doesn't strip this because we mark the React bundle
// with @preserve-invariant-messages docblock.
const i = invariant;
// However, we call it with a different name to avoid
// transforming this file itself as part of React's own build.
i(false, message);
}

export default reactProdInvariant;
3 changes: 3 additions & 0 deletions packages/shared/reactProdInvariant.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ function reactProdInvariant(code: string): void {
' for the full message or use the non-minified dev environment' +
' for full errors and additional helpful warnings.';

// Note: if you update the code above, don't forget
// to update the www fork in forks/reactProdInvariant.www.js.

const error: Error & {framesToPop?: number} = new Error(message);
error.name = 'Invariant Violation';
error.framesToPop = 1; // we don't care about reactProdInvariant's own frame
Expand Down
8 changes: 8 additions & 0 deletions scripts/rollup/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ function getBabelConfig(updateBabelOptions, bundleType, filename) {
switch (bundleType) {
case FB_DEV:
case FB_PROD:
return Object.assign({}, options, {
plugins: options.plugins.concat([
// Minify invariant messages
require('../error-codes/replace-invariant-error-codes'),
// Wrap warning() calls in a __DEV__ check so they are stripped from production.
require('../babel/wrap-warning-with-env-check'),
]),
});
case RN_DEV:
case RN_PROD:
return Object.assign({}, options, {
Expand Down
11 changes: 11 additions & 0 deletions scripts/rollup/forks.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ const forks = Object.freeze({
}
},

// Route production invariants on www through the www invariant module.
'shared/reactProdInvariant': (bundleType, entry) => {
switch (bundleType) {
case FB_DEV:
case FB_PROD:
return 'shared/forks/reactProdInvariant.www.js';
default:
return null;
}
},

// Different dialogs for caught errors.
'react-reconciler/src/ReactFiberErrorDialog': (bundleType, entry) => {
switch (bundleType) {
Expand Down
2 changes: 2 additions & 0 deletions scripts/rollup/wrappers.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ ${license}
*
* @noflow
* @preventMunge
* @preserve-invariant-messages
*/
'use strict';
Expand All @@ -112,6 +113,7 @@ ${license}
*
* @noflow
* @preventMunge
* @preserve-invariant-messages
*/
${source}`;
Expand Down

0 comments on commit 76b4ba0

Please sign in to comment.