Skip to content

Commit

Permalink
Dynamic imports priority fix closes #6980 (#7061)
Browse files Browse the repository at this point in the history
  • Loading branch information
highvoltag3 committed Mar 20, 2022
1 parent c9a4238 commit c394019
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 23 deletions.
@@ -0,0 +1 @@
export default {};
@@ -0,0 +1 @@
export default import('./async').then(() => document.head.children);
68 changes: 45 additions & 23 deletions packages/core/integration-tests/test/javascript.js
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
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

0 comments on commit c394019

Please sign in to comment.