Common JS UMD Pattern Detection Removes IF checks for module exports
See original GitHub issueRecent compilers miss emiting a condition
Repro:
cat <<EOF >index.js
'use strict';
// Stripped down demo from react-dom/index.js (16.2.0)
function checkDCE() {
if (process.env.NODE_ENV !== 'production') {
throw new Error('^_^');
}
}
if (process.env.NODE_ENV === 'production') {
checkDCE();
module.exports = function() { return "foo"; };
} else {
module.exports = function() { return "bar"; };
}
EOF
curl -O http://repo1.maven.org/maven2/com/google/javascript/closure-compiler/v20170910/closure-compiler-v20170910.jar
curl -O http://repo1.maven.org/maven2/com/google/javascript/closure-compiler/v20180204/closure-compiler-v20180204.jar
java -jar closure-compiler-v20180204.jar --js index.js --process_common_js_modules --js_output_file failure.js
java -jar closure-compiler-v20170910.jar --js index.js --process_common_js_modules --js_output_file success.js
Output failure.js
of the recent compiler unconditionally keeps the process.env.NODE_ENV === 'production'
branch “optimizing” away the else. 😉
Issue Analytics
- State:
- Created 6 years ago
- Reactions:6
- Comments:6 (1 by maintainers)
Top Results From Across the Web
What the heck are CJS, AMD, UMD, and ESM in Javascript?
Check here for more patterns. UMD is usually used as a fallback module when using bundler like Rollup/ Webpack. ESM. ESM stands for...
Read more >Understanding (all) JavaScript module formats and tools
The argument evaluation detects the environment (check the module variable and exports variable of CommonJS/Node.js, as well as the define ...
Read more >What is AMD, CommonJS, and UMD? - David Calhoun's blog
Personal blog of photographer and web developer David Calhoun.
Read more >How JavaScript works: the module pattern + comparing ...
How JavaScript works: the module pattern + comparing CommonJS, AMD, UMD, and ES6 Modules. This is post # 50 of the series, dedicated...
Read more >Which Module Formats Should Your JavaScript Library Support?
Regardless of whether an application consumes your library as a CommonJS, AMD or IIFE module, UMD conditionally checks for the module format ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It’s the same thing. The logic looks for exports nested inside an if or else block.
I’ve known this could be a potential problem for quite some time: https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/ProcessCommonJSModules.java#L577-L580