Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve error codes for invariants on www #12539

Merged
merged 2 commits into from Apr 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 38 additions & 0 deletions 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;
3 changes: 3 additions & 0 deletions packages/shared/reactProdInvariant.js
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
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
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
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