diff --git a/scripts/error-codes/replace-invariant-error-codes.js b/scripts/error-codes/replace-invariant-error-codes.js index 0af41f0fb1..1bc9ee5e05 100644 --- a/scripts/error-codes/replace-invariant-error-codes.js +++ b/scripts/error-codes/replace-invariant-error-codes.js @@ -91,13 +91,6 @@ module.exports = function(babel) { var condition = node.arguments[0]; var errorMsgLiteral = evalToString(node.arguments[1]); - var prodErrorId = errorMap[errorMsgLiteral]; - if (prodErrorId === undefined) { - // The error cannot be found in the map. - node[SEEN_SYMBOL] = true; - return; - } - var devInvariant = t.callExpression( node.callee, [ @@ -109,10 +102,22 @@ module.exports = function(babel) { devInvariant[SEEN_SYMBOL] = true; var localInvariantId = getProdInvariantIdentifier(path, this); - var prodInvariant = t.callExpression( - localInvariantId, - [t.stringLiteral(prodErrorId)].concat(node.arguments.slice(2)) - ); + + var prodErrorId = errorMap[errorMsgLiteral]; + var prodInvariant; + if (prodErrorId === undefined) { + // The error cannot be found in the map. + // (This case isn't expected to occur.) + // Even if it does, it's best to transform the invariant to a ternary, + // So we don't risk executing any slow code unnecessarily + // (eg generating an invariant message we don't actually need). + prodInvariant = path.node.arguments[1]; + } else { + prodInvariant = t.callExpression( + localInvariantId, + [t.stringLiteral(prodErrorId)].concat(node.arguments.slice(2)) + ); + } prodInvariant[SEEN_SYMBOL] = true; path.replaceWith( diff --git a/scripts/rollup/build.js b/scripts/rollup/build.js index 14dc5a6cd8..3d50bbbb26 100644 --- a/scripts/rollup/build.js +++ b/scripts/rollup/build.js @@ -375,11 +375,13 @@ function getPlugins( modulesToStub, featureFlags ) { + // Extract error codes 1st so we can replace invariant messages in prod builds + // Without re-running the slow build script. const plugins = [ - babel(updateBabelConfig(babelOpts, bundleType)), alias( Modules.getAliases(paths, bundleType, moduleType, argv['extract-errors']) ), + babel(updateBabelConfig(babelOpts, bundleType)), ]; const replaceModules = Modules.getDefaultReplaceModules(