Skip to content
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
2 changes: 1 addition & 1 deletion lib/config/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,7 @@ const applyOutputDefaults = (
D(output, "assetModuleFilename", "[hash][ext][query]");
D(output, "webassemblyModuleFilename", "[hash].module.wasm");
D(output, "compareBeforeEmit", true);
D(output, "charset", true);
D(output, "charset", !futureDefaults);
const uniqueNameId = Template.toIdentifier(
/** @type {NonNullable<Output["uniqueName"]>} */ (output.uniqueName)
);
Expand Down
7 changes: 5 additions & 2 deletions lib/css/CssLoadingRuntimeModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ class CssLoadingRuntimeModule extends RuntimeModule {
outputOptions: {
crossOriginLoading,
uniqueName,
chunkLoadTimeout: loadTimeout
chunkLoadTimeout: loadTimeout,
charset
}
} = compilation;
const fn = RuntimeGlobals.ensureChunkHandlers;
Expand Down Expand Up @@ -138,6 +139,7 @@ class CssLoadingRuntimeModule extends RuntimeModule {

const code = Template.asString([
"link = document.createElement('link');",
charset ? "link.charset = 'utf-8';" : "",
`if (${RuntimeGlobals.scriptNonce}) {`,
Template.indent(
`link.setAttribute("nonce", ${RuntimeGlobals.scriptNonce});`
Expand Down Expand Up @@ -351,6 +353,7 @@ class CssLoadingRuntimeModule extends RuntimeModule {
linkPrefetch.call(
Template.asString([
"var link = document.createElement('link');",
charset ? "link.charset = 'utf-8';" : "",
crossOriginLoading
? `link.crossOrigin = ${JSON.stringify(
crossOriginLoading
Expand Down Expand Up @@ -390,7 +393,7 @@ class CssLoadingRuntimeModule extends RuntimeModule {
linkPreload.call(
Template.asString([
"var link = document.createElement('link');",
"link.charset = 'utf-8';",
charset ? "link.charset = 'utf-8';" : "",
`if (${RuntimeGlobals.scriptNonce}) {`,
Template.indent(
`link.setAttribute("nonce", ${RuntimeGlobals.scriptNonce});`
Expand Down
5 changes: 3 additions & 2 deletions lib/esm/ModuleChunkLoadingRuntimeModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class ModuleChunkLoadingRuntimeModule extends RuntimeModule {
(compilation.outputOptions.environment);
const {
runtimeTemplate,
outputOptions: { importFunctionName, crossOriginLoading }
outputOptions: { importFunctionName, crossOriginLoading, charset }
} = compilation;
const fn = RuntimeGlobals.ensureChunkHandlers;
const withBaseURI = this._runtimeRequirements.has(RuntimeGlobals.baseURI);
Expand Down Expand Up @@ -261,6 +261,7 @@ class ModuleChunkLoadingRuntimeModule extends RuntimeModule {
linkPrefetch.call(
Template.asString([
"var link = document.createElement('link');",
charset ? "link.charset = 'utf-8';" : "",
crossOriginLoading
? `link.crossOrigin = ${JSON.stringify(
crossOriginLoading
Expand Down Expand Up @@ -300,7 +301,7 @@ class ModuleChunkLoadingRuntimeModule extends RuntimeModule {
linkPreload.call(
Template.asString([
"var link = document.createElement('link');",
"link.charset = 'utf-8';",
charset ? "link.charset = 'utf-8';" : "",
`if (${RuntimeGlobals.scriptNonce}) {`,
Template.indent(
`link.setAttribute("nonce", ${RuntimeGlobals.scriptNonce});`
Expand Down
6 changes: 4 additions & 2 deletions lib/web/JsonpChunkLoadingRuntimeModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ class JsonpChunkLoadingRuntimeModule extends RuntimeModule {
chunkLoadingGlobal,
hotUpdateGlobal,
crossOriginLoading,
scriptType
scriptType,
charset
}
} = compilation;
const globalObject = runtimeTemplate.globalObject;
Expand Down Expand Up @@ -229,6 +230,7 @@ class JsonpChunkLoadingRuntimeModule extends RuntimeModule {
linkPrefetch.call(
Template.asString([
"var link = document.createElement('link');",
charset ? "link.charset = 'utf-8';" : "",
crossOriginLoading
? `link.crossOrigin = ${JSON.stringify(
crossOriginLoading
Expand Down Expand Up @@ -268,7 +270,7 @@ class JsonpChunkLoadingRuntimeModule extends RuntimeModule {
scriptType && scriptType !== "module"
? `link.type = ${JSON.stringify(scriptType)};`
: "",
"link.charset = 'utf-8';",
charset ? "link.charset = 'utf-8';" : "",
`if (${RuntimeGlobals.scriptNonce}) {`,
Template.indent(
`link.setAttribute("nonce", ${RuntimeGlobals.scriptNonce});`
Expand Down
18 changes: 13 additions & 5 deletions test/Defaults.unittest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2331,8 +2331,9 @@ describe("snapshots", () => {
+ "resolve": Object {
+ "fullySpecified": true,
+ "preferRelative": true,
+ },
@@ ... @@
+ "type": "css",
+ },
@@ ... @@
- "generator": Object {},
+ "generator": Object {
Expand All @@ -2354,11 +2355,12 @@ describe("snapshots", () => {
+ },
+ },
@@ ... @@
+ },
@@ ... @@
+ "css": Object {
+ "import": true,
+ "namedExports": true,
+ "url": true,
+ },
@@ ... @@
+ "exportsPresence": "error",
@@ ... @@
Expand All @@ -2371,6 +2373,9 @@ describe("snapshots", () => {
@@ ... @@
+ "css",
@@ ... @@
- "charset": true,
+ "charset": false,
@@ ... @@
- "hashDigestLength": 20,
- "hashFunction": "md4",
+ "hashDigestLength": 16,
Expand Down Expand Up @@ -2425,9 +2430,10 @@ describe("snapshots", () => {
+ "css": false,
+ "futureDefaults": true,
@@ ... @@
+ },
+ Object {
+ "rules": Array [
@@ ... @@
+ Object {
+ "descriptionData": Object {
+ "type": "module",
+ },
Expand All @@ -2438,8 +2444,7 @@ describe("snapshots", () => {
+ ],
+ "test": /\\.wasm$/i,
+ "type": "webassembly/async",
+ },
+ Object {
@@ ... @@
+ "mimetype": "application/wasm",
+ "rules": Array [
+ Object {
Expand All @@ -2464,6 +2469,9 @@ describe("snapshots", () => {
+ "__filename": "warn-mock",
+ "global": "warn",
@@ ... @@
- "charset": true,
+ "charset": false,
@@ ... @@
- "hashDigestLength": 20,
- "hashFunction": "md4",
+ "hashDigestLength": 16,
Expand Down
3 changes: 3 additions & 0 deletions test/configCases/output/charset/chunk1.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
a {
color: red;
}
27 changes: 19 additions & 8 deletions test/configCases/output/charset/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
__webpack_public_path__ = "https://example.com/public/path/";
const doImport = () => import(/* webpackChunkName: "chunk1" */ "./chunk1");
it("should not add charset attribute", () => {
const promise = doImport();
expect(document.head._children).toHaveLength(1);
__webpack_public_path__ = "https://test.cases/path/";

const script = document.head._children[0];
const doJsImport = () => import(/* webpackChunkName: "chunk1" */ "./chunk1.js");
const doCssImport = () => import( /* webpackChunkName: "chunk1" */ "./chunk1.css" );

it("should not add charset attribute", async () => {
const promise = doJsImport();
expect(document.head._children).toHaveLength(3);

const link = document.head._children[0];

expect(link._type).toBe("link");
expect(link.href).toBe("https://test.cases/path/chunk1.css");
expect(link.rel).toBe("stylesheet");
expect(link.getAttribute("charset")).toBeUndefined();

const script = document.head._children[document.head._children.length - 1];

__non_webpack_require__("./chunk1.js");
script.onload();

expect(script._type).toBe("script");
expect(script.src).toBe("https://example.com/public/path/chunk1.js");
expect(script.src).toBe("https://test.cases/path/chunk1.js");
expect(script.getAttribute("charset")).toBeUndefined();
return promise;

return promise.then(() => doCssImport);
});
8 changes: 8 additions & 0 deletions test/configCases/output/charset/test.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
moduleScope(scope, options) {
const link = scope.window.document.createElement("link");
link.rel = "stylesheet";
link.href = "chunk1.css";
scope.window.document.head.appendChild(link);
}
};
3 changes: 3 additions & 0 deletions test/configCases/output/charset/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ module.exports = {
performance: {
hints: false
},
experiments: {
css: true
},
optimization: {
chunkIds: "named",
minimize: false
Expand Down
3 changes: 3 additions & 0 deletions test/configCases/web/prefetch-preload-module/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ it("should prefetch and preload child chunks on chunk load", () => {
expect(link.rel).toBe("prefetch");
expect(link.as).toBe("script");
expect(link.href).toBe("https://example.com/public/path/chunk1.mjs");
expect(link.charset).toBe("utf-8");

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

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

Expand Down
2 changes: 2 additions & 0 deletions test/configCases/web/prefetch-preload/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ it("should prefetch and preload child chunks on chunk load", () => {
expect(link._type).toBe("link");
expect(link.rel).toBe("prefetch");
expect(link.as).toBe("script");
expect(link.charset).toBe("utf-8");
expect(link.href).toBe("https://example.com/public/path/chunk1.js");

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

link = document.head._children[7];
Expand Down
Loading