From 213b1bbcb5438dabe36fb0aea63b045b498b4171 Mon Sep 17 00:00:00 2001 From: Fathy Boundjadj Date: Sat, 21 Jul 2018 22:21:25 +0200 Subject: [PATCH] Support sideEffect: false with CommonJS Fixes #1699 --- src/scope-hoisting/hoist.js | 7 +++++-- .../scope-hoisting/commonjs/side-effects-false/a.js | 1 + .../side-effects-false/node_modules/bar/bar.js | 3 +++ .../side-effects-false/node_modules/bar/foo.js | 3 +++ .../side-effects-false/node_modules/bar/index.js | 2 ++ .../side-effects-false/node_modules/bar/package.json | 4 ++++ test/scope-hoisting.js | 10 ++++++++++ 7 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 test/integration/scope-hoisting/commonjs/side-effects-false/a.js create mode 100644 test/integration/scope-hoisting/commonjs/side-effects-false/node_modules/bar/bar.js create mode 100644 test/integration/scope-hoisting/commonjs/side-effects-false/node_modules/bar/foo.js create mode 100644 test/integration/scope-hoisting/commonjs/side-effects-false/node_modules/bar/index.js create mode 100644 test/integration/scope-hoisting/commonjs/side-effects-false/node_modules/bar/package.json diff --git a/src/scope-hoisting/hoist.js b/src/scope-hoisting/hoist.js index b1e34ddcef3..7e9b1f37ec9 100644 --- a/src/scope-hoisting/hoist.js +++ b/src/scope-hoisting/hoist.js @@ -280,8 +280,9 @@ module.exports = { } if (t.isIdentifier(callee, {name: 'require'})) { + let source = args[0].value; // Ignore require calls that were ignored earlier. - if (!asset.dependencies.has(args[0].value)) { + if (!asset.dependencies.has(source)) { return; } @@ -296,9 +297,11 @@ module.exports = { p.isSequenceExpression() ); if (!parent.isProgram() || bail) { - asset.dependencies.get(args[0].value).shouldWrap = true; + asset.dependencies.get(source).shouldWrap = true; } + asset.cacheData.imports['$require$' + source] = [source, '*']; + // Generate a variable name based on the current asset id and the module name to require. // This will be replaced by the final variable name of the resolved asset in the packager. path.replaceWith( diff --git a/test/integration/scope-hoisting/commonjs/side-effects-false/a.js b/test/integration/scope-hoisting/commonjs/side-effects-false/a.js new file mode 100644 index 00000000000..20a695d7239 --- /dev/null +++ b/test/integration/scope-hoisting/commonjs/side-effects-false/a.js @@ -0,0 +1 @@ +output = require('bar').foo(3) diff --git a/test/integration/scope-hoisting/commonjs/side-effects-false/node_modules/bar/bar.js b/test/integration/scope-hoisting/commonjs/side-effects-false/node_modules/bar/bar.js new file mode 100644 index 00000000000..dca7bebf277 --- /dev/null +++ b/test/integration/scope-hoisting/commonjs/side-effects-false/node_modules/bar/bar.js @@ -0,0 +1,3 @@ +export default function bar() { + return 2; +} diff --git a/test/integration/scope-hoisting/commonjs/side-effects-false/node_modules/bar/foo.js b/test/integration/scope-hoisting/commonjs/side-effects-false/node_modules/bar/foo.js new file mode 100644 index 00000000000..36b0b344468 --- /dev/null +++ b/test/integration/scope-hoisting/commonjs/side-effects-false/node_modules/bar/foo.js @@ -0,0 +1,3 @@ +export default function foo(a) { + return a * a; +} diff --git a/test/integration/scope-hoisting/commonjs/side-effects-false/node_modules/bar/index.js b/test/integration/scope-hoisting/commonjs/side-effects-false/node_modules/bar/index.js new file mode 100644 index 00000000000..f9e940c9f6a --- /dev/null +++ b/test/integration/scope-hoisting/commonjs/side-effects-false/node_modules/bar/index.js @@ -0,0 +1,2 @@ +export foo from './foo'; +export bar from './bar'; diff --git a/test/integration/scope-hoisting/commonjs/side-effects-false/node_modules/bar/package.json b/test/integration/scope-hoisting/commonjs/side-effects-false/node_modules/bar/package.json new file mode 100644 index 00000000000..1ea9bea7dfa --- /dev/null +++ b/test/integration/scope-hoisting/commonjs/side-effects-false/node_modules/bar/package.json @@ -0,0 +1,4 @@ +{ + "name": "bar", + "sideEffects": false +} diff --git a/test/scope-hoisting.js b/test/scope-hoisting.js index e88c70ab191..b9332d4eb18 100644 --- a/test/scope-hoisting.js +++ b/test/scope-hoisting.js @@ -816,5 +816,15 @@ describe('scope hoisting', function() { let output = await run(b); assert.deepEqual(output, 3); }); + + it('should support sideEffects: false', async function() { + let b = await bundle( + __dirname + + '/integration/scope-hoisting/commonjs/side-effects-false/a.js' + ); + + let output = await run(b); + assert.deepEqual(output, 9); + }); }); });