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

Dynamic imports priority fix closes #6980 #7061

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default import('./async').then(() => document.head.children);
23 changes: 23 additions & 0 deletions packages/core/integration-tests/test/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,29 @@ describe('javascript', function() {
assert.deepEqual(res, {default: 42});
});

it('dynamic imports loaded as high-priority scripts when not all engines support esmodules natively', async function() {
let b = await bundle(
path.join(__dirname, '/integration/dynamic-imports-high-prio/index.js'),
{
defaultTargetOptions: {
engines: {
browsers: 'IE 11',
},
},
},
);

let output = await run(b);
let headChildren = await output.default;

assert(headChildren[1].tag === 'link');
assert(headChildren[1].rel === 'preload');
assert(headChildren[1].as === 'script');

assert(headChildren[2].tag === 'script');
assert(headChildren[2].src.match(/async\..*\.js/));
});

it('should support bundling workers with dynamic import in both page and worker', async function() {
let b = await bundle(
path.join(__dirname, '/integration/worker-dynamic/index-async.js'),
Expand Down
7 changes: 7 additions & 0 deletions packages/runtimes/js/src/helpers/browser/js-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ module.exports = cacheLoader(function loadJSBundle(bundle) {
return;
}

var link = document.createElement('link');
link.href = bundle;
link.rel = 'preload';
link.as = 'script';
document.head.appendChild(link);

var script = document.createElement('script');
script.async = true;
script.type = 'text/javascript';
Expand All @@ -29,6 +35,7 @@ module.exports = cacheLoader(function loadJSBundle(bundle) {
resolve();
};

document.getElementsByTagName('head')[0].appendChild(link);
document.getElementsByTagName('head')[0].appendChild(script);
});
});