Skip to content

Commit

Permalink
First round of tests
Browse files Browse the repository at this point in the history
Fixing bugs exposed by the tests!
  • Loading branch information
aboktor committed Apr 18, 2023
1 parent 73662d7 commit ffeba44
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 691 deletions.
11 changes: 5 additions & 6 deletions lib/RuntimeTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -936,18 +936,17 @@ class RuntimeTemplate {
message,
chunkName: block.chunkName
});
const fetchPriority = JSON.stringify(chunkGroup.options.fetchPriority);
if (chunks.length === 1) {
const chunkId = JSON.stringify(chunks[0].id);
runtimeRequirements.add(RuntimeGlobals.ensureChunk);
return `${RuntimeGlobals.ensureChunk}(${comment}${chunkId})`;
return `${RuntimeGlobals.ensureChunk}(${comment}${chunkId}, ${fetchPriority})`;
} else if (chunks.length > 0) {
runtimeRequirements.add(RuntimeGlobals.ensureChunk);
const requireChunkId = chunk =>
`${RuntimeGlobals.ensureChunk}(${JSON.stringify(chunk.id)}, ${
chunkGroup.options.fetchPriority
? JSON.stringify(chunkGroup.options.fetchPriority)
: ""
})`;
`${RuntimeGlobals.ensureChunk}(${JSON.stringify(
chunk.id
)}, ${fetchPriority})`;
return `Promise.all(${comment.trim()}[${chunks
.map(requireChunkId)
.join(", ")}])`;
Expand Down
4 changes: 3 additions & 1 deletion lib/runtime/LoadScriptRuntimeModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ class LoadScriptRuntimeModule extends HelperRuntimeModule {
uniqueName
? 'script.setAttribute("data-webpack", dataWebpackPrefix + key);'
: "",
`if(fetchPriority) script.setAttribute("fetchpriority", fetchPriority);`,
"if(fetchPriority) {",
Template.indent('script.setAttribute("fetchpriority", fetchPriority);'),
"}",
`script.src = ${
this._withCreateScriptUrl
? `${RuntimeGlobals.createScriptUrl}(url)`
Expand Down
342 changes: 0 additions & 342 deletions test/__snapshots__/ConfigCacheTestCases.longtest.js.snap

This file was deleted.

342 changes: 0 additions & 342 deletions test/__snapshots__/ConfigTestCases.basictest.js.snap

This file was deleted.

1 change: 1 addition & 0 deletions test/cases/chunks/inline-options/dir14/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default "a";
1 change: 1 addition & 0 deletions test/cases/chunks/inline-options/dir14/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default "b";
1 change: 1 addition & 0 deletions test/cases/chunks/inline-options/dir14/c.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default "c";
7 changes: 7 additions & 0 deletions test/cases/chunks/inline-options/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,13 @@ if (process.env.NODE_ENV === "production") {
}
);
});
it("should be able to load with webpackFetchPriorty high, low and auto", function () {
return Promise.all([
import(/* webpackFetchPriority: "high"*/ "./dir14/a"),
import(/* webpackFetchPriority: "low"*/ "./dir14/b"),
import(/* webpackFetchPriority: "auto"*/ "./dir14/c"),
])
})
}

function testChunkLoading(load, expectedSyncInitial, expectedSyncRequested) {
Expand Down
1 change: 1 addition & 0 deletions test/configCases/web/fetch-priority/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default "a";
3 changes: 3 additions & 0 deletions test/configCases/web/fetch-priority/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as shared from './shared';
console.log(shared);
export default "b";
3 changes: 3 additions & 0 deletions test/configCases/web/fetch-priority/b2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as shared from './shared';
console.log(shared);
export default "b";
1 change: 1 addition & 0 deletions test/configCases/web/fetch-priority/c.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default "c";
1 change: 1 addition & 0 deletions test/configCases/web/fetch-priority/d.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default "d";
31 changes: 31 additions & 0 deletions test/configCases/web/fetch-priority/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
it("should set fetchPriority", () => {
// Single Chunk
import(/* webpackFetchPriority: "high" */ "./a");
expect(document.head._children).toHaveLength(1);
const script1 = document.head._children[0];
expect(script1._attributes.fetchpriority).toBe("high");

// Multiple Chunks
import(/* webpackFetchPriority: "high" */ "./b");
import(/* webpackFetchPriority: "high" */ "./b2");
expect(document.head._children).toHaveLength(4);
const script2 = document.head._children[1];
const script3 = document.head._children[2];
const script4 = document.head._children[3];
expect(script2._attributes.fetchpriority).toBe("high");
expect(script3._attributes.fetchpriority).toBe("high");
expect(script4._attributes.fetchpriority).toBe("high");

// Single Chunk, low
import(/* webpackFetchPriority: "low" */ "./c");
expect(document.head._children).toHaveLength(5);
const script5 = document.head._children[4];
expect(script5._attributes.fetchpriority).toBe("low");

// Single Chunk, auto
import(/* webpackFetchPriority: "auto" */ "./d");
expect(document.head._children).toHaveLength(6);
const script6 = document.head._children[5];
expect(script6._attributes.fetchpriority).toBe("auto");

})
1 change: 1 addition & 0 deletions test/configCases/web/fetch-priority/shared.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default "shared";
14 changes: 14 additions & 0 deletions test/configCases/web/fetch-priority/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
target: "web",
output: {
chunkFilename: "[name].js",
crossOriginLoading: "anonymous"
},
optimization: {
minimize: false,
splitChunks: {
minSize: 1
}
}
};

0 comments on commit ffeba44

Please sign in to comment.