From 43a4c256c6f155deaa8303499bf0c078130cbbc5 Mon Sep 17 00:00:00 2001 From: Brian Vaughn Date: Thu, 19 Oct 2017 15:14:40 -0700 Subject: [PATCH 1/2] Rollup build scripts can now extract error codes while building --- .../replace-invariant-error-codes.js | 27 +++++++++++-------- scripts/rollup/build.js | 4 ++- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/scripts/error-codes/replace-invariant-error-codes.js b/scripts/error-codes/replace-invariant-error-codes.js index 0af41f0fb12..148b807d521 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 turnary, + // 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 14dc5a6cd8f..3d50bbbb262 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( From 3553d7b1e60d77224ee857d02df96f0e4f28a450 Mon Sep 17 00:00:00 2001 From: Brian Vaughn Date: Thu, 19 Oct 2017 15:22:05 -0700 Subject: [PATCH 2/2] I can't spell ternary --- scripts/error-codes/replace-invariant-error-codes.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/error-codes/replace-invariant-error-codes.js b/scripts/error-codes/replace-invariant-error-codes.js index 148b807d521..1bc9ee5e05d 100644 --- a/scripts/error-codes/replace-invariant-error-codes.js +++ b/scripts/error-codes/replace-invariant-error-codes.js @@ -108,7 +108,7 @@ module.exports = function(babel) { 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 turnary, + // 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];