-
Notifications
You must be signed in to change notification settings - Fork 47.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FB bundles wrap warning() calls in __DEV__ (#10314)
FB bundles wrap warning() calls in __DEV__ Split dev-mode transforms into separate parts: 1) umd+cjs+fb: Wrap warning calls with process.env checks 2) umd+cjs: Replace error messages with minified codes Also updated transforms to use __DEV__ since it transforms to smaller code after stripEnvVariables is run. Also renamed 'scripts/error-codes/dev-expression-with-codes.js' -> 'scripts/error-codes/replace-invariant-error-codes.js'
- Loading branch information
Showing
9 changed files
with
130 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
scripts/rollup/plugins/__tests__/wrap-warning-with-env-check-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Copyright (c) 2013-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
/* eslint-disable quotes */ | ||
'use strict'; | ||
|
||
let babel = require('babel-core'); | ||
let wrapWarningWithEnvCheck = require('../wrap-warning-with-env-check'); | ||
|
||
function transform(input) { | ||
return babel.transform(input, { | ||
plugins: [wrapWarningWithEnvCheck], | ||
}).code; | ||
} | ||
|
||
function compare(input, output) { | ||
var compiled = transform(input); | ||
expect(compiled).toEqual(output); | ||
} | ||
|
||
var oldEnv; | ||
|
||
describe('wrap-warning-with-env-check', () => { | ||
beforeEach(() => { | ||
oldEnv = process.env.NODE_ENV; | ||
process.env.NODE_ENV = ''; | ||
}); | ||
|
||
afterEach(() => { | ||
process.env.NODE_ENV = oldEnv; | ||
}); | ||
|
||
it('should wrap warning calls', () => { | ||
compare( | ||
"warning(condition, 'a %s b', 'c');", | ||
"__DEV__ ? warning(condition, 'a %s b', 'c') : void 0;" | ||
); | ||
}); | ||
|
||
it('should not wrap invariant calls', () => { | ||
compare( | ||
"invariant(condition, 'a %s b', 'c');", | ||
"invariant(condition, 'a %s b', 'c');" | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* Copyright (c) 2013-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
'use strict'; | ||
|
||
module.exports = function(babel, options) { | ||
var t = babel.types; | ||
|
||
var DEV_EXPRESSION = t.identifier('__DEV__'); | ||
|
||
var SEEN_SYMBOL = Symbol('expression.seen'); | ||
|
||
return { | ||
visitor: { | ||
CallExpression: { | ||
exit: function(path) { | ||
var node = path.node; | ||
|
||
// Ignore if it's already been processed | ||
if (node[SEEN_SYMBOL]) { | ||
return; | ||
} | ||
|
||
if (path.get('callee').isIdentifier({name: 'warning'})) { | ||
node[SEEN_SYMBOL] = true; | ||
|
||
// Turns this code: | ||
// | ||
// warning(condition, argument, argument); | ||
// | ||
// into this: | ||
// | ||
// if (__DEV__) { | ||
// warning(condition, argument, argument); | ||
// } | ||
// | ||
// The goal is to strip out warning calls entirely in production. | ||
path.replaceWith( | ||
t.ifStatement( | ||
DEV_EXPRESSION, | ||
t.blockStatement([t.expressionStatement(node)]) | ||
) | ||
); | ||
} | ||
}, | ||
}, | ||
}, | ||
}; | ||
}; |