@samitouri / QOS-React-2 / commits / 845b1afdc5

Rollup script can now extract error codes and build in a single pass (#11291)

* Rollup build scripts can now extract error codes while building * I can't spell ternary

Brian Vaughn committed Oct 20, 2017 at 08:35 UTC 845b1afdc5ee82dd33751270fbde3cb8def4070a
2 files changed +19 -12
scripts/error-codes/replace-invariant-error-codes.js
+16 -11
@@ -91,13 +91,6 @@ module.exports = function(babel) {
91 var condition = node.arguments[0];
92 var errorMsgLiteral = evalToString(node.arguments[1]);
93
94 - var prodErrorId = errorMap[errorMsgLiteral];
95 - if (prodErrorId === undefined) {
96 - // The error cannot be found in the map.
97 - node[SEEN_SYMBOL] = true;
98 - return;
99 - }
100 -
94 var devInvariant = t.callExpression(
95 node.callee,
96 [
@@ -109,10 +102,22 @@ module.exports = function(babel) {
102 devInvariant[SEEN_SYMBOL] = true;
103
104 var localInvariantId = getProdInvariantIdentifier(path, this);
112 - var prodInvariant = t.callExpression(
113 - localInvariantId,
114 - [t.stringLiteral(prodErrorId)].concat(node.arguments.slice(2))
115 - );
105 +
106 + var prodErrorId = errorMap[errorMsgLiteral];
107 + var prodInvariant;
108 + if (prodErrorId === undefined) {
109 + // The error cannot be found in the map.
110 + // (This case isn't expected to occur.)
111 + // Even if it does, it's best to transform the invariant to a ternary,
112 + // So we don't risk executing any slow code unnecessarily
113 + // (eg generating an invariant message we don't actually need).
114 + prodInvariant = path.node.arguments[1];
115 + } else {
116 + prodInvariant = t.callExpression(
117 + localInvariantId,
118 + [t.stringLiteral(prodErrorId)].concat(node.arguments.slice(2))
119 + );
120 + }
121
122 prodInvariant[SEEN_SYMBOL] = true;
123 path.replaceWith(
scripts/rollup/build.js
+3 -1
@@ -375,11 +375,13 @@ function getPlugins(
375 modulesToStub,
376 featureFlags
377 ) {
378 + // Extract error codes 1st so we can replace invariant messages in prod builds
379 + // Without re-running the slow build script.
380 const plugins = [
379 - babel(updateBabelConfig(babelOpts, bundleType)),
381 alias(
382 Modules.getAliases(paths, bundleType, moduleType, argv['extract-errors'])
383 ),
384 + babel(updateBabelConfig(babelOpts, bundleType)),
385 ];
386
387 const replaceModules = Modules.getDefaultReplaceModules(