From 8dfb0578816435a1a72f04506ee20d3c55d0f9bc Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Mon, 9 Apr 2018 23:58:34 +0100 Subject: [PATCH] Unfork invariant and instead use it from reactProdInvariant (#12585) --- .../shared/forks/reactProdInvariant.www.js | 38 ---------------- packages/shared/reactProdInvariant.js | 43 +++++++++---------- scripts/rollup/forks.js | 11 ----- 3 files changed, 21 insertions(+), 71 deletions(-) delete mode 100644 packages/shared/forks/reactProdInvariant.www.js diff --git a/packages/shared/forks/reactProdInvariant.www.js b/packages/shared/forks/reactProdInvariant.www.js deleted file mode 100644 index 36768ad28d0dd..0000000000000 --- a/packages/shared/forks/reactProdInvariant.www.js +++ /dev/null @@ -1,38 +0,0 @@ -/** - * 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 2b629c5d32964..92764573fe3ca 100644 --- a/packages/shared/reactProdInvariant.js +++ b/packages/shared/reactProdInvariant.js @@ -7,6 +7,10 @@ * @flow */ +// Relying on the `invariant()` implementation lets us +// have preserve the format and params in the www builds. +import invariant from 'fbjs/lib/invariant'; + /** * WARNING: DO NOT manually require this module. * This is a replacement for `invariant(...)` used by the error code system @@ -15,30 +19,25 @@ */ 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; - + let url = 'http://reactjs.org/docs/error-decoder.html?invariant=' + code; for (let argIdx = 0; argIdx < argCount; argIdx++) { - message += '&args[]=' + encodeURIComponent(arguments[argIdx + 1]); + url += '&args[]=' + encodeURIComponent(arguments[argIdx + 1]); } - - message += - ' 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 - - throw error; + // Rename it so that our build transform doesn't atttempt + // to replace this invariant() call with reactProdInvariant(). + const i = invariant; + i( + false, + // The error code is intentionally part of the message (and + // not the format argument) so that we could deduplicate + // different errors in logs based on the code. + 'Minified React error #' + + code + + '; visit %s ' + + 'for the full message or use the non-minified dev environment ' + + 'for full errors and additional helpful warnings. ', + url, + ); } export default reactProdInvariant; diff --git a/scripts/rollup/forks.js b/scripts/rollup/forks.js index f02e771b85a8e..f6d2a0623cc27 100644 --- a/scripts/rollup/forks.js +++ b/scripts/rollup/forks.js @@ -84,17 +84,6 @@ 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) {