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

Fix hoisting of optional require calls #2078

Merged
merged 1 commit into from
Oct 6, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/packagers/JSConcatPackager.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,13 @@ class JSConcatPackager extends Packager {
let [source, name] = asset.cacheData.imports[identifier];
let dep = asset.depAssets.get(asset.dependencies.get(source));

if (name === '*') {
this.markUsedExports(dep);
}
if (dep) {
if (name === '*') {
this.markUsedExports(dep);
}

this.markUsed(dep, name);
this.markUsed(dep, name);
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions test/integration/scope-hoisting/commonjs/wrap-optional/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
try {
output = require('noop')
}
catch(_) {
output = 42
}
12 changes: 12 additions & 0 deletions test/scope-hoisting.js
Original file line number Diff line number Diff line change
Expand Up @@ -1053,5 +1053,17 @@ describe('scope hoisting', function() {
let output = await run(b);
assert.deepEqual(output, 9);
});

it('should support optional requires', async function() {
let b = await bundle(
path.join(
__dirname,
'/integration/scope-hoisting/commonjs/wrap-optional/a.js'
)
);

let output = await run(b);
assert.deepEqual(output, 42);
});
});
});