-
-
Notifications
You must be signed in to change notification settings - Fork 615
Closed
Labels
Description
- Rollup Plugin Name: @rollup/plugin-commonjs
- Rollup Plugin Version: 22.0.0
- Rollup Version: 2.70.2
- Operating System (or Browser): macOS
- Node Version: v14.16.1
- Link to reproduction (
⚠️ read below): https://github.com/privatenumber/issue-reproductions/tree/master/reproductions/rollup/plugins/1177
Input
index.js
if (process.env.NODE_ENV === 'production') {
require('./prod.js');
}prod.js
console.log('side effect');
export default 'production';Output
var src = {};
console.log('side effect');
if (process.env.NODE_ENV === 'production') ;
export { src as default };Expected Behavior
For the console.log('side effect'); to be delayed until the if-condition passes.
Note, this only happens because prod.js is an ESM file. If the export ... is removed for it to be interpreted as a CJS file, it will compile expectedly:
var src = {};
var prod = {};
var hasRequiredProd;
function requireProd () {
if (hasRequiredProd) return prod;
hasRequiredProd = 1;
console.log('side effect');
// export default 'production';
return prod;
}
if (process.env.NODE_ENV === 'production') {
requireProd();
}
export { src as default };Actual Behavior
The side effects of the ESM file are hoisted up and executed even if the if-condition doesn't pass.
Additional Information
Reactions are currently unavailable