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 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
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);
68 changes: 45 additions & 23 deletions packages/core/integration-tests/test/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -594,20 +594,20 @@ describe('javascript', function () {
let output = await run(b);
let headChildren = await output.default;

assert.equal(headChildren.length, 3);
assert.strictEqual(headChildren.length, 4);

assert(headChildren[0].tag === 'script');
assert(headChildren[0].src.match(/async\..*\.js/));
assert.strictEqual(headChildren[1].tag, 'script');
assert(headChildren[1].src.match(/async\..*\.js/));

assert(headChildren[1].tag === 'link');
assert(headChildren[1].rel === 'prefetch');
assert(headChildren[1].as === 'script');
assert(headChildren[1].href.match(/prefetched\..*\.js/));
assert.strictEqual(headChildren[2].tag, 'link');
assert.strictEqual(headChildren[2].rel, 'prefetch');
assert.strictEqual(headChildren[2].as, 'script');
assert(headChildren[2].href.match(/prefetched\..*\.js/));

assert(headChildren[2].tag === 'link');
assert(headChildren[2].rel === 'prefetch');
assert(headChildren[2].as === 'style');
assert(headChildren[2].href.match(/prefetched\..*\.css/));
assert.strictEqual(headChildren[3].tag, 'link');
assert.strictEqual(headChildren[3].rel, 'prefetch');
assert.strictEqual(headChildren[3].as, 'style');
assert(headChildren[3].href.match(/prefetched\..*\.css/));
});

it('should load additional links that were prefetched', async function () {
Expand All @@ -623,7 +623,7 @@ describe('javascript', function () {
await outputReturn.loadDependency();

let headChildren = outputReturn.children;
assert.equal(headChildren.length, 5);
assert.equal(headChildren.length, 7);
let cssBundles = headChildren.filter(child =>
child.href?.match(/prefetched-loaded\..*\.css/),
);
Expand All @@ -647,20 +647,17 @@ describe('javascript', function () {
let output = await run(b);
let headChildren = await output.default;

assert(headChildren.length === 3);

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

assert(headChildren[1].tag === 'link');
assert(headChildren[1].rel === 'preload');
assert(headChildren[1].as === 'script');
assert(headChildren[1].href.match(/preloaded\..*\.js/));
assert(headChildren.length === 4);

assert(headChildren[2].tag === 'link');
assert(headChildren[2].rel === 'preload');
assert(headChildren[2].as === 'style');
assert(headChildren[2].href.match(/preloaded\..*\.css/));
assert(headChildren[2].as === 'script');
assert(headChildren[2].href.match(/preloaded\..*\.js/));

assert(headChildren[3].tag === 'link');
assert(headChildren[3].rel === 'preload');
assert(headChildren[3].as === 'style');
assert(headChildren[3].href.match(/preloaded\..*\.css/));
});

// TODO: Implement when we can evaluate bundles against esmodule targets
Expand Down Expand Up @@ -874,6 +871,31 @@ 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[0].tag === 'link');
assert(headChildren[0].rel === 'preload');
assert(headChildren[0].as === 'script');

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

assert(headChildren[0].href === headChildren[1].src);
});

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
6 changes: 6 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 preloadLink = document.createElement('link');
preloadLink.href = bundle;
preloadLink.rel = 'preload';
preloadLink.as = 'script';
document.head.appendChild(preloadLink);

var script = document.createElement('script');
script.async = true;
script.type = 'text/javascript';
Expand Down