Skip to content

Commit

Permalink
Support sideEffect: false with CommonJS (#1770)
Browse files Browse the repository at this point in the history
Fixes #1699
  • Loading branch information
fathyb authored and devongovett committed Jul 22, 2018
1 parent 7a77bca commit 92bb3ea
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/core/parcel/src/scope-hoisting/hoist.js
Expand Up @@ -300,8 +300,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;
}

Expand All @@ -316,9 +317,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(
Expand Down
@@ -0,0 +1 @@
output = require('bar').foo(3)

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions packages/core/parcel/test/scope-hoisting.js
Expand Up @@ -832,5 +832,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);
});
});
});

0 comments on commit 92bb3ea

Please sign in to comment.