Skip to content

Commit

Permalink
Preserve error codes for invariants on www
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Apr 6, 2018
1 parent 7a3416f commit 0eb9531
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 1 deletion.
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
1 change: 1 addition & 0 deletions scripts/error-codes/replace-invariant-error-codes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
'use strict';

const fs = require('fs');
const {basename} = require('path');
const evalToString = require('../shared/evalToString');
const invertObject = require('./invertObject');

Expand Down
10 changes: 9 additions & 1 deletion 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 Expand Up @@ -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;
},
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 0eb9531

Please sign in to comment.