Skip to content

Commit

Permalink
Fix hoisting of optional require calls (#2078)
Browse files Browse the repository at this point in the history
  • Loading branch information
fathyb authored and devongovett committed Oct 6, 2018
1 parent 9f4cec7 commit cadb1d4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
10 changes: 6 additions & 4 deletions packages/core/parcel-bundler/src/packagers/JSConcatPackager.js
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
@@ -0,0 +1,6 @@
try {
output = require('noop')
}
catch(_) {
output = 42
}
12 changes: 12 additions & 0 deletions packages/core/parcel-bundler/test/scope-hoisting.js
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);
});
});
});

0 comments on commit cadb1d4

Please sign in to comment.