question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Common JS UMD Pattern Detection Removes IF checks for module exports

See original GitHub issue

Recent 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:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
ChadKillingsworthcommented, May 25, 2018

It’s the same thing. The logic looks for exports nested inside an if or else block.

0reactions
ChadKillingsworthcommented, May 25, 2018
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found