diff --git a/packages/shared/forks/reactProdInvariant.www.js b/packages/shared/forks/reactProdInvariant.www.js new file mode 100644 index 000000000000..36768ad28d0d --- /dev/null +++ b/packages/shared/forks/reactProdInvariant.www.js @@ -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; diff --git a/packages/shared/reactProdInvariant.js b/packages/shared/reactProdInvariant.js index 9bba7a793eef..2b629c5d3296 100644 --- a/packages/shared/reactProdInvariant.js +++ b/packages/shared/reactProdInvariant.js @@ -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 diff --git a/scripts/rollup/build.js b/scripts/rollup/build.js index 30bfc8ab1360..0237845eddce 100644 --- a/scripts/rollup/build.js +++ b/scripts/rollup/build.js @@ -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, { diff --git a/scripts/rollup/forks.js b/scripts/rollup/forks.js index f6d2a0623cc2..f02e771b85a8 100644 --- a/scripts/rollup/forks.js +++ b/scripts/rollup/forks.js @@ -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) { diff --git a/scripts/rollup/wrappers.js b/scripts/rollup/wrappers.js index df7fb4019ade..8be9187d8031 100644 --- a/scripts/rollup/wrappers.js +++ b/scripts/rollup/wrappers.js @@ -94,6 +94,7 @@ ${license} * * @noflow * @preventMunge + * @preserve-invariant-messages */ 'use strict'; @@ -112,6 +113,7 @@ ${license} * * @noflow * @preventMunge + * @preserve-invariant-messages */ ${source}`;