Skip to content

Commit f123ce5

Browse files
fix: respect output.charset everywhere (#19202)
1 parent af20c7b commit f123ce5

File tree

11 files changed

+64
-20
lines changed

11 files changed

+64
-20
lines changed

lib/config/defaults.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,7 @@ const applyOutputDefaults = (
10841084
D(output, "assetModuleFilename", "[hash][ext][query]");
10851085
D(output, "webassemblyModuleFilename", "[hash].module.wasm");
10861086
D(output, "compareBeforeEmit", true);
1087-
D(output, "charset", true);
1087+
D(output, "charset", !futureDefaults);
10881088
const uniqueNameId = Template.toIdentifier(
10891089
/** @type {NonNullable<Output["uniqueName"]>} */ (output.uniqueName)
10901090
);

lib/css/CssLoadingRuntimeModule.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ class CssLoadingRuntimeModule extends RuntimeModule {
7474
outputOptions: {
7575
crossOriginLoading,
7676
uniqueName,
77-
chunkLoadTimeout: loadTimeout
77+
chunkLoadTimeout: loadTimeout,
78+
charset
7879
}
7980
} = compilation;
8081
const fn = RuntimeGlobals.ensureChunkHandlers;
@@ -138,6 +139,7 @@ class CssLoadingRuntimeModule extends RuntimeModule {
138139

139140
const code = Template.asString([
140141
"link = document.createElement('link');",
142+
charset ? "link.charset = 'utf-8';" : "",
141143
`if (${RuntimeGlobals.scriptNonce}) {`,
142144
Template.indent(
143145
`link.setAttribute("nonce", ${RuntimeGlobals.scriptNonce});`
@@ -351,6 +353,7 @@ class CssLoadingRuntimeModule extends RuntimeModule {
351353
linkPrefetch.call(
352354
Template.asString([
353355
"var link = document.createElement('link');",
356+
charset ? "link.charset = 'utf-8';" : "",
354357
crossOriginLoading
355358
? `link.crossOrigin = ${JSON.stringify(
356359
crossOriginLoading
@@ -390,7 +393,7 @@ class CssLoadingRuntimeModule extends RuntimeModule {
390393
linkPreload.call(
391394
Template.asString([
392395
"var link = document.createElement('link');",
393-
"link.charset = 'utf-8';",
396+
charset ? "link.charset = 'utf-8';" : "",
394397
`if (${RuntimeGlobals.scriptNonce}) {`,
395398
Template.indent(
396399
`link.setAttribute("nonce", ${RuntimeGlobals.scriptNonce});`

lib/esm/ModuleChunkLoadingRuntimeModule.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class ModuleChunkLoadingRuntimeModule extends RuntimeModule {
9393
(compilation.outputOptions.environment);
9494
const {
9595
runtimeTemplate,
96-
outputOptions: { importFunctionName, crossOriginLoading }
96+
outputOptions: { importFunctionName, crossOriginLoading, charset }
9797
} = compilation;
9898
const fn = RuntimeGlobals.ensureChunkHandlers;
9999
const withBaseURI = this._runtimeRequirements.has(RuntimeGlobals.baseURI);
@@ -261,6 +261,7 @@ class ModuleChunkLoadingRuntimeModule extends RuntimeModule {
261261
linkPrefetch.call(
262262
Template.asString([
263263
"var link = document.createElement('link');",
264+
charset ? "link.charset = 'utf-8';" : "",
264265
crossOriginLoading
265266
? `link.crossOrigin = ${JSON.stringify(
266267
crossOriginLoading
@@ -300,7 +301,7 @@ class ModuleChunkLoadingRuntimeModule extends RuntimeModule {
300301
linkPreload.call(
301302
Template.asString([
302303
"var link = document.createElement('link');",
303-
"link.charset = 'utf-8';",
304+
charset ? "link.charset = 'utf-8';" : "",
304305
`if (${RuntimeGlobals.scriptNonce}) {`,
305306
Template.indent(
306307
`link.setAttribute("nonce", ${RuntimeGlobals.scriptNonce});`

lib/web/JsonpChunkLoadingRuntimeModule.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ class JsonpChunkLoadingRuntimeModule extends RuntimeModule {
8080
chunkLoadingGlobal,
8181
hotUpdateGlobal,
8282
crossOriginLoading,
83-
scriptType
83+
scriptType,
84+
charset
8485
}
8586
} = compilation;
8687
const globalObject = runtimeTemplate.globalObject;
@@ -229,6 +230,7 @@ class JsonpChunkLoadingRuntimeModule extends RuntimeModule {
229230
linkPrefetch.call(
230231
Template.asString([
231232
"var link = document.createElement('link');",
233+
charset ? "link.charset = 'utf-8';" : "",
232234
crossOriginLoading
233235
? `link.crossOrigin = ${JSON.stringify(
234236
crossOriginLoading
@@ -268,7 +270,7 @@ class JsonpChunkLoadingRuntimeModule extends RuntimeModule {
268270
scriptType && scriptType !== "module"
269271
? `link.type = ${JSON.stringify(scriptType)};`
270272
: "",
271-
"link.charset = 'utf-8';",
273+
charset ? "link.charset = 'utf-8';" : "",
272274
`if (${RuntimeGlobals.scriptNonce}) {`,
273275
Template.indent(
274276
`link.setAttribute("nonce", ${RuntimeGlobals.scriptNonce});`

test/Defaults.unittest.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2331,8 +2331,9 @@ describe("snapshots", () => {
23312331
+ "resolve": Object {
23322332
+ "fullySpecified": true,
23332333
+ "preferRelative": true,
2334-
+ },
2334+
@@ ... @@
23352335
+ "type": "css",
2336+
+ },
23362337
@@ ... @@
23372338
- "generator": Object {},
23382339
+ "generator": Object {
@@ -2354,11 +2355,12 @@ describe("snapshots", () => {
23542355
+ },
23552356
+ },
23562357
@@ ... @@
2358+
+ },
2359+
@@ ... @@
23572360
+ "css": Object {
23582361
+ "import": true,
23592362
+ "namedExports": true,
23602363
+ "url": true,
2361-
+ },
23622364
@@ ... @@
23632365
+ "exportsPresence": "error",
23642366
@@ ... @@
@@ -2371,6 +2373,9 @@ describe("snapshots", () => {
23712373
@@ ... @@
23722374
+ "css",
23732375
@@ ... @@
2376+
- "charset": true,
2377+
+ "charset": false,
2378+
@@ ... @@
23742379
- "hashDigestLength": 20,
23752380
- "hashFunction": "md4",
23762381
+ "hashDigestLength": 16,
@@ -2425,9 +2430,10 @@ describe("snapshots", () => {
24252430
+ "css": false,
24262431
+ "futureDefaults": true,
24272432
@@ ... @@
2433+
+ },
24282434
+ Object {
24292435
+ "rules": Array [
2430-
@@ ... @@
2436+
+ Object {
24312437
+ "descriptionData": Object {
24322438
+ "type": "module",
24332439
+ },
@@ -2438,8 +2444,7 @@ describe("snapshots", () => {
24382444
+ ],
24392445
+ "test": /\\.wasm$/i,
24402446
+ "type": "webassembly/async",
2441-
+ },
2442-
+ Object {
2447+
@@ ... @@
24432448
+ "mimetype": "application/wasm",
24442449
+ "rules": Array [
24452450
+ Object {
@@ -2464,6 +2469,9 @@ describe("snapshots", () => {
24642469
+ "__filename": "warn-mock",
24652470
+ "global": "warn",
24662471
@@ ... @@
2472+
- "charset": true,
2473+
+ "charset": false,
2474+
@@ ... @@
24672475
- "hashDigestLength": 20,
24682476
- "hashFunction": "md4",
24692477
+ "hashDigestLength": 16,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
a {
2+
color: red;
3+
}
Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
1-
__webpack_public_path__ = "https://example.com/public/path/";
2-
const doImport = () => import(/* webpackChunkName: "chunk1" */ "./chunk1");
3-
it("should not add charset attribute", () => {
4-
const promise = doImport();
5-
expect(document.head._children).toHaveLength(1);
1+
__webpack_public_path__ = "https://test.cases/path/";
62

7-
const script = document.head._children[0];
3+
const doJsImport = () => import(/* webpackChunkName: "chunk1" */ "./chunk1.js");
4+
const doCssImport = () => import( /* webpackChunkName: "chunk1" */ "./chunk1.css" );
5+
6+
it("should not add charset attribute", async () => {
7+
const promise = doJsImport();
8+
expect(document.head._children).toHaveLength(3);
9+
10+
const link = document.head._children[0];
11+
12+
expect(link._type).toBe("link");
13+
expect(link.href).toBe("https://test.cases/path/chunk1.css");
14+
expect(link.rel).toBe("stylesheet");
15+
expect(link.getAttribute("charset")).toBeUndefined();
16+
17+
const script = document.head._children[document.head._children.length - 1];
818

919
__non_webpack_require__("./chunk1.js");
1020
script.onload();
1121

1222
expect(script._type).toBe("script");
13-
expect(script.src).toBe("https://example.com/public/path/chunk1.js");
23+
expect(script.src).toBe("https://test.cases/path/chunk1.js");
1424
expect(script.getAttribute("charset")).toBeUndefined();
15-
return promise;
25+
26+
return promise.then(() => doCssImport);
1627
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
moduleScope(scope, options) {
3+
const link = scope.window.document.createElement("link");
4+
link.rel = "stylesheet";
5+
link.href = "chunk1.css";
6+
scope.window.document.head.appendChild(link);
7+
}
8+
};

test/configCases/output/charset/webpack.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ module.exports = {
88
performance: {
99
hints: false
1010
},
11+
experiments: {
12+
css: true
13+
},
1114
optimization: {
1215
chunkIds: "named",
1316
minimize: false

test/configCases/web/prefetch-preload-module/index.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ it("should prefetch and preload child chunks on chunk load", () => {
1313
expect(link.rel).toBe("prefetch");
1414
expect(link.as).toBe("script");
1515
expect(link.href).toBe("https://example.com/public/path/chunk1.mjs");
16+
expect(link.charset).toBe("utf-8");
1617

1718
link = document.head._children[1];
1819
expect(link._type).toBe("link");
1920
expect(link.rel).toBe("prefetch");
2021
expect(link.as).toBe("style");
2122
expect(link.href).toBe("https://example.com/public/path/chunk2-css.css");
23+
expect(link.charset).toBe("utf-8");
2224

2325
link = document.head._children[2];
2426
expect(link._type).toBe("link");
@@ -38,6 +40,7 @@ it("should prefetch and preload child chunks on chunk load", () => {
3840
expect(link.rel).toBe("preload");
3941
expect(link.as).toBe("style");
4042
expect(link.href).toBe("https://example.com/public/path/chunk1-a-css.css");
43+
expect(link.charset).toBe("utf-8");
4144
expect(link.getAttribute("nonce")).toBe("nonce");
4245
expect(link.crossOrigin).toBe("anonymous");
4346

test/configCases/web/prefetch-preload/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ it("should prefetch and preload child chunks on chunk load", () => {
1212
expect(link._type).toBe("link");
1313
expect(link.rel).toBe("prefetch");
1414
expect(link.as).toBe("script");
15+
expect(link.charset).toBe("utf-8");
1516
expect(link.href).toBe("https://example.com/public/path/chunk1.js");
1617

1718
link = document.head._children[1];
@@ -80,6 +81,7 @@ it("should prefetch and preload child chunks on chunk load", () => {
8081
expect(link.rel).toBe("prefetch");
8182
expect(link.as).toBe("script");
8283
expect(link.href).toBe("https://example.com/public/path/chunk1-c.js");
84+
expect(link.charset).toBe("utf-8");
8385
expect(link.crossOrigin).toBe("anonymous");
8486

8587
link = document.head._children[7];

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy