Skip to content

Commit c7dd066

Browse files
fix: context modules with import attributes (#19984)
1 parent 85bacbd commit c7dd066

File tree

12 files changed

+66
-8
lines changed

12 files changed

+66
-8
lines changed

lib/ContextModule.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,12 @@ class ContextModule extends Module {
254254
} else if (this.options.namespaceObject) {
255255
identifier += "|namespace object";
256256
}
257+
if (this.options.attributes) {
258+
identifier += `|importAttributes: ${JSON.stringify(this.options.attributes)}`;
259+
}
257260
if (this.layer) {
258261
identifier += `|layer: ${this.layer}`;
259262
}
260-
261263
return identifier;
262264
}
263265

lib/asset/AssetBytesGenerator.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class AssetSourceGenerator extends Generator {
5353

5454
const encodedSource = originalSource.buffer().toString("base64");
5555

56+
runtimeRequirements.add(RuntimeGlobals.requireScope);
5657
runtimeRequirements.add(RuntimeGlobals.toBinary);
5758

5859
let sourceContent;

lib/dependencies/ContextElementDependency.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ class ContextElementDependency extends ModuleDependency {
6868
*/
6969
getResourceIdentifier() {
7070
let str = super.getResourceIdentifier();
71-
if (this.attributes !== undefined) {
72-
str += JSON.stringify(this.attributes);
71+
if (this.attributes) {
72+
str += `|importAttributes${JSON.stringify(this.attributes)}`;
7373
}
7474
return str;
7575
}

lib/dependencies/HarmonyImportDependency.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,11 @@ class HarmonyImportDependency extends ModuleDependency {
7979
*/
8080
getResourceIdentifier() {
8181
let str = super.getResourceIdentifier();
82-
if (this.attributes !== undefined) {
83-
str += JSON.stringify(this.attributes);
82+
if (this.defer) {
83+
str += "|defer";
84+
}
85+
if (this.attributes) {
86+
str += `|importAttributes${JSON.stringify(this.attributes)}`;
8487
}
8588
return str;
8689
}

lib/dependencies/ImportContextDependency.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ class ImportContextDependency extends ContextDependency {
3535
return "esm";
3636
}
3737

38+
/**
39+
* @returns {string | null} an identifier to merge equal requests
40+
*/
41+
getResourceIdentifier() {
42+
let str = super.getResourceIdentifier();
43+
44+
if (this.options.attributes) {
45+
str += `|importAttributes${JSON.stringify(this.options.attributes)}`;
46+
}
47+
48+
return str;
49+
}
50+
3851
/**
3952
* @param {ObjectSerializerContext} context context
4053
*/

lib/dependencies/ImportDependency.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ class ImportDependency extends ModuleDependency {
5050
*/
5151
getResourceIdentifier() {
5252
let str = super.getResourceIdentifier();
53-
if (this.attributes !== undefined) {
54-
str += JSON.stringify(this.attributes);
53+
if (this.attributes) {
54+
str += `|importAttributes${JSON.stringify(this.attributes)}`;
5555
}
5656
return str;
5757
}

test/configCases/asset-modules/bytes/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import * as style from "./style.css";
22
import file from "./file.text" with { type: "bytes" };
33

4-
it("should work", () => {
4+
it("should work", async () => {
55
const decoder = new TextDecoder('utf-8');
66
const text = decoder.decode(file);
77

88
expect(text).toBe("a Ā 𐀀 文 🦄 Text");
99

10+
const dyn = (await import("./file.text?other", { with: { type: "bytes" } })).default;
11+
const dynText = decoder.decode(dyn);
12+
13+
expect(dynText).toBe("a Ā 𐀀 文 🦄 Text");
14+
1015
if (typeof getComputedStyle === "function") {
1116
const style = getComputedStyle(document.body);
1217
expect(style.getPropertyValue("--my-url")).toBe(" url(data:application/octet-stream;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA2MDAgNjAwIj48dGl0bGU+aWNvbi1zcXVhcmUtc21hbGw8L3RpdGxlPjxwYXRoIGZpbGw9IiNGRkYiIGQ9Ik0zMDAgLjFMNTY1IDE1MHYyOTkuOUwzMDAgNTk5LjggMzUgNDQ5LjlWMTUweiIvPjxwYXRoIGZpbGw9IiM4RUQ2RkIiIGQ9Ik01MTcuNyA0MzkuNUwzMDguOCA1NTcuOHYtOTJMNDM5IDM5NC4xbDc4LjcgNDUuNHptMTQuMy0xMi45VjE3OS40bC03Ni40IDQ0LjF2MTU5bDc2LjQgNDQuMXpNODEuNSA0MzkuNWwyMDguOSAxMTguMnYtOTJsLTEzMC4yLTcxLjYtNzguNyA0NS40em0tMTQuMy0xMi45VjE3OS40bDc2LjQgNDQuMXYxNTlsLTc2LjQgNDQuMXptOC45LTI2My4yTDI5MC40IDQyLjJ2ODlsLTEzNy4zIDc1LjUtMS4xLjYtNzUuOS00My45em00NDYuOSAwTDMwOC44IDQyLjJ2ODlMNDQ2IDIwNi44bDEuMS42IDc1LjktNDR6Ii8+PHBhdGggZmlsbD0iIzFDNzhDMCIgZD0iTTI5MC40IDQ0NC44TDE2MiAzNzQuMVYyMzQuMmwxMjguNCA3NC4xdjEzNi41em0xOC40IDBsMTI4LjQtNzAuNnYtMTQwbC0xMjguNCA3NC4xdjEzNi41ek0yOTkuNiAzMDN6bS0xMjktODVsMTI5LTcwLjlMNDI4LjUgMjE4bC0xMjguOSA3NC40LTEyOS03NC40eiIvPjwvc3ZnPg==)");

test/configCases/asset-modules/bytes/test.config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,15 @@ module.exports = {
1414
scope.window.document.head.appendChild(link);
1515

1616
run++;
17+
},
18+
findBundle(i) {
19+
if (i === 2) {
20+
return ["bundle2.mjs"];
21+
}
22+
23+
return [
24+
`file_text_other.bundle${i}.${i === 2 ? "mjs" : "js"}`,
25+
`bundle${i}.${i === 2 ? "mjs" : "js"}`
26+
];
1727
}
1828
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"foo": "bar"
3+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
it("show override request", async () => {
2+
const decoder = new TextDecoder('utf-8');
3+
const mod = "file.ext";
4+
const loadedMod = (await import(`./files/${mod}`, { with: { type: "bytes" } })).default;
5+
const text = decoder.decode(loadedMod);
6+
7+
expect(JSON.parse(text)).toEqual({ foo: "bar" });
8+
9+
const otherLoadedMod = (await import(`./files/${mod}`, { with: { type: "json" } })).default;
10+
11+
expect(otherLoadedMod.foo).toBe("bar");
12+
});

0 commit comments

Comments
 (0)