Skip to content

Commit

Permalink
Merge branch 'kingdaro-esm-dynamic-import-fix'
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed Dec 9, 2017
2 parents cdda044 + a79adfc commit c8295de
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
13 changes: 2 additions & 11 deletions src/builtins/bundle-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ function loadBundles(bundles) {
var id = Array.isArray(bundles) ? bundles[bundles.length - 1] : bundles;

try {
return Promise.resolve(requireModule(id));
return Promise.resolve(require(id));
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') {
return new LazyPromise(function (resolve, reject) {
Promise.all(bundles.slice(0, -1).map(loadBundle)).then(function () {
return requireModule(id);
return require(id);
}).then(resolve, reject);
});
}
Expand Down Expand Up @@ -75,15 +75,6 @@ function loadCSSBundle(bundle) {
});
}

function requireModule(id) {
var res = require(id);
if (res.__esModule) {
return res.default;
}

return res;
}

function LazyPromise(executor) {
this.executor = executor;
this.promise = null;
Expand Down
7 changes: 7 additions & 0 deletions test/integration/dynamic-esm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var local = import('./local');

export default function () {
return local.then(function (l) {
return l.a + l.b;
});
};
2 changes: 2 additions & 0 deletions test/integration/dynamic-esm/local.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const a = 1;
export const b = 2;
17 changes: 17 additions & 0 deletions test/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@ describe('javascript', function() {
assert.equal(await output(), 3);
});

it('should return all exports as an object when using ES modules', async function() {
let b = await bundle(__dirname + '/integration/dynamic-esm/index.js');

assertBundleTree(b, {
name: 'index.js',
assets: ['index.js', 'bundle-loader.js', 'bundle-url.js'],
childBundles: [{
assets: ['local.js'],
childBundles: []
}]
});

let output = run(b).default;
assert.equal(typeof output, 'function');
assert.equal(await output(), 3);
});

it('should hoist common dependencies into a parent bundle', async function() {
let b = await bundle(__dirname + '/integration/dynamic-hoist/index.js');

Expand Down

0 comments on commit c8295de

Please sign in to comment.