From 10f2dc211a4c33384a2d18025a0d2e9c5e018159 Mon Sep 17 00:00:00 2001 From: Fathy Boundjadj Date: Sat, 29 Sep 2018 08:22:45 +0200 Subject: [PATCH] Fix hoisting of optional require calls --- src/packagers/JSConcatPackager.js | 10 ++++++---- .../scope-hoisting/commonjs/wrap-optional/a.js | 6 ++++++ test/scope-hoisting.js | 12 ++++++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 test/integration/scope-hoisting/commonjs/wrap-optional/a.js diff --git a/src/packagers/JSConcatPackager.js b/src/packagers/JSConcatPackager.js index 139ae89bf9b..2c339c90128 100644 --- a/src/packagers/JSConcatPackager.js +++ b/src/packagers/JSConcatPackager.js @@ -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); + } } } diff --git a/test/integration/scope-hoisting/commonjs/wrap-optional/a.js b/test/integration/scope-hoisting/commonjs/wrap-optional/a.js new file mode 100644 index 00000000000..54368ffd38a --- /dev/null +++ b/test/integration/scope-hoisting/commonjs/wrap-optional/a.js @@ -0,0 +1,6 @@ +try { + output = require('noop') +} +catch(_) { + output = 42 +} diff --git a/test/scope-hoisting.js b/test/scope-hoisting.js index ebf2a765a6d..17ef11470b9 100644 --- a/test/scope-hoisting.js +++ b/test/scope-hoisting.js @@ -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); + }); }); });