Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support sideEffect: false with CommonJS #1770

Merged
merged 1 commit into from Jul 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/scope-hoisting/hoist.js
Expand Up @@ -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;
}

Expand All @@ -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, '*'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could potentially conflict with a real import, right? e.g. import $require$foo from './foo'

Copy link
Contributor Author

@fathyb fathyb Jul 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, in imports we would use $id$import$$require$foo as key :

asset.cacheData.imports = {
  "$require$foo": ['foo', '*'],
  "$id$import$$require$foo": ['./foo', 'default']
}

or is there another place where it would conflict?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh right...


// 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 test/scope-hoisting.js
Expand Up @@ -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);
});
});
});