From 5ed64a6aa72c845157dbad3f301db5e292762137 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Fri, 6 Apr 2018 00:58:48 +0100 Subject: [PATCH 1/2] Preserve error codes for invariants on www --- .../shared/forks/reactProdInvariant.www.js | 38 +++++++++++++++++++ packages/shared/reactProdInvariant.js | 3 ++ scripts/rollup/build.js | 10 ++++- scripts/rollup/forks.js | 11 ++++++ scripts/rollup/wrappers.js | 2 + 5 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 packages/shared/forks/reactProdInvariant.www.js diff --git a/packages/shared/forks/reactProdInvariant.www.js b/packages/shared/forks/reactProdInvariant.www.js new file mode 100644 index 0000000000000..36768ad28d0dd --- /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 9bba7a793eef9..2b629c5d32964 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 30bfc8ab13609..ef9c853573a6f 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, { @@ -197,7 +205,7 @@ function getPlugins( return [ // Extract error codes from invariant() messages into a file. shouldExtractErrors && { - transform(source) { + transform(source, opts) { findAndRecordErrorCodes(source); return source; }, diff --git a/scripts/rollup/forks.js b/scripts/rollup/forks.js index f6d2a0623cc27..f02e771b85a8e 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 df7fb4019ade5..8be9187d80314 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}`; From 155af9ccacfbccdba077f2c311cb83ec4ea0dae7 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Mon, 9 Apr 2018 14:58:55 +0100 Subject: [PATCH 2/2] Remove unintentional change --- scripts/rollup/build.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/rollup/build.js b/scripts/rollup/build.js index ef9c853573a6f..0237845eddce7 100644 --- a/scripts/rollup/build.js +++ b/scripts/rollup/build.js @@ -205,7 +205,7 @@ function getPlugins( return [ // Extract error codes from invariant() messages into a file. shouldExtractErrors && { - transform(source, opts) { + transform(source) { findAndRecordErrorCodes(source); return source; },