Skip to content

Commit 153c222

Browse files
refactor(html): drop with: { lang } for inline bodies
with is the import-attributes condition, so carrying an HTML lang there misled. A lang body is matched by its mimetype alone: known languages as their files' media type, any other as text/x-<lang>, which no built-in rule's prefix catches; a lang no media type can name stays CSS or JS.
1 parent 8b4cafd commit 153c222

9 files changed

Lines changed: 46 additions & 102 deletions

File tree

‎declarations/WebpackOptions.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2041,7 +2041,7 @@ export interface HtmlParserOptions {
20412041
*/
20422042
parseErrors?: boolean;
20432043
/**
2044-
* Configure extraction of URL-like attribute values (e.g. `<img src>`, `<link href>`, `<script src>`) as webpack dependencies. `true` (default) uses the built-in source list; `false` disables extraction entirely so attributes are left untouched and `<script src>` / `<link rel="modulepreload">` / `<link rel="stylesheet">` no longer become compilation entries; an array lets you customize which `tag`/`attribute` pairs are treated as URLs and how they are bundled. Use the string `"..."` inside the array to inline the defaults. Inline `<script>` and `<style>` bodies are always processed; one whose `lang` names another language is a `data:` module typed as that language's files (`<style lang="scss">` as `text/x-scss`, `<script lang="ts">` as `text/typescript`), so a `mimetype` rule runs its loader, and `with: { lang }` matches the attribute as written. Use `webpackIgnore` comments or `IgnorePlugin` to skip individual URLs.
2044+
* Configure extraction of URL-like attribute values (e.g. `<img src>`, `<link href>`, `<script src>`) as webpack dependencies. `true` (default) uses the built-in source list; `false` disables extraction entirely so attributes are left untouched and `<script src>` / `<link rel="modulepreload">` / `<link rel="stylesheet">` no longer become compilation entries; an array lets you customize which `tag`/`attribute` pairs are treated as URLs and how they are bundled. Use the string `"..."` inside the array to inline the defaults. Inline `<script>` and `<style>` bodies are always processed; one whose `lang` names another language is a `data:` module typed as that language's files (`<style lang="scss">` as `text/x-scss`, `<script lang="ts">` as `text/typescript`), and anything else as `text/x-<lang>`, so a `mimetype` rule runs its loader. Use `webpackIgnore` comments or `IgnorePlugin` to skip individual URLs.
20452045
*/
20462046
sources?:
20472047
| /** Sources to extract as webpack dependencies. Use `"..."` to inline the default source list. @minItems 1 */ Array<

‎lib/dependencies/html/HtmlInlineScriptDependency.js‎

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ const getHtmlGenerator = memoize(() => require("../../html/HtmlGenerator"));
1515
/** @import Dependency from "../../graph/Dependency" */
1616
/** @import { DependencyTemplateContext } from "../../template/DependencyTemplate" */
1717
/** @import { Range } from "../../javascript/JavascriptParser" */
18-
/** @typedef {import("../../serialization/ObjectMiddleware").ObjectDeserializerContext<[number, Range, string, string, Range | undefined, boolean, string | undefined]>} ObjectDeserializerContext */
19-
/** @typedef {import("../../serialization/ObjectMiddleware").ObjectSerializerContext<[number, Range, string, string, Range | undefined, boolean, string | undefined]>} ObjectSerializerContext */
18+
/** @typedef {import("../../serialization/ObjectMiddleware").ObjectDeserializerContext<[number, Range, string, string, Range | undefined, boolean]>} ObjectDeserializerContext */
19+
/** @typedef {import("../../serialization/ObjectMiddleware").ObjectSerializerContext<[number, Range, string, string, Range | undefined, boolean]>} ObjectSerializerContext */
2020

2121
/**
2222
* Represents an inline `<script>...</script>` block in an HTML module. The
@@ -35,7 +35,6 @@ class HtmlInlineScriptDependency extends ModuleDependency {
3535
* @param {string=} category dependency category used for resolving and grouping
3636
* @param {Range=} removeRange range of the whole element, when the tag is dropped
3737
* @param {boolean=} forceInline `<!-- webpackInline: true -->` was set before this tag
38-
* @param {string=} lang the `lang` the body is written in, when not JavaScript (e.g. `ts`)
3938
*/
4039
constructor(
4140
request,
@@ -44,8 +43,7 @@ class HtmlInlineScriptDependency extends ModuleDependency {
4443
entryName,
4544
category,
4645
removeRange,
47-
forceInline,
48-
lang
46+
forceInline
4947
) {
5048
super(request);
5149
/** @type {number} */
@@ -60,8 +58,6 @@ class HtmlInlineScriptDependency extends ModuleDependency {
6058
this.removeRange = removeRange;
6159
/** @type {boolean} */
6260
this.forceInline = forceInline === true;
63-
/** @type {string | undefined} */
64-
this.lang = lang;
6561
}
6662

6763
get type() {
@@ -72,14 +68,6 @@ class HtmlInlineScriptDependency extends ModuleDependency {
7268
return this._category;
7369
}
7470

75-
/**
76-
* The element's `lang` for a rule's `with` condition (`with: { lang: "ts" }`).
77-
* @returns {{ lang: string } | undefined} the body's attributes
78-
*/
79-
get attributes() {
80-
return this.lang === undefined ? undefined : { lang: this.lang };
81-
}
82-
8371
/**
8472
* Serializes this instance into the provided serializer context.
8573
* @param {ObjectSerializerContext} context context
@@ -91,8 +79,7 @@ class HtmlInlineScriptDependency extends ModuleDependency {
9179
.write(this.entryName)
9280
.write(this._category)
9381
.write(this.removeRange)
94-
.write(this.forceInline)
95-
.write(this.lang);
82+
.write(this.forceInline);
9683
super.serialize(context);
9784
}
9885

@@ -113,9 +100,7 @@ class HtmlInlineScriptDependency extends ModuleDependency {
113100
this.removeRange = c4.read();
114101
const c5 = c4.rest;
115102
this.forceInline = c5.read();
116-
const c6 = c5.rest;
117-
this.lang = c6.read();
118-
super.deserialize(c6.rest);
103+
super.deserialize(c5.rest);
119104
}
120105
}
121106

‎lib/dependencies/html/HtmlInlineStyleDependency.js‎

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ const ModuleDependency = require("../core/ModuleDependency");
1313
/** @import { DependencyTemplateContext } from "../../template/DependencyTemplate" */
1414
/** @import Module from "../../module/Module" */
1515
/** @import { Range } from "../../javascript/JavascriptParser" */
16-
/** @typedef {import("../../serialization/ObjectMiddleware").ObjectDeserializerContext<[boolean, boolean, boolean, string | undefined]>} ObjectDeserializerContext */
17-
/** @typedef {import("../../serialization/ObjectMiddleware").ObjectSerializerContext<[boolean, boolean, boolean, string | undefined]>} ObjectSerializerContext */
16+
/** @typedef {import("../../serialization/ObjectMiddleware").ObjectDeserializerContext<boolean[]>} ObjectDeserializerContext */
17+
/** @typedef {import("../../serialization/ObjectMiddleware").ObjectSerializerContext<boolean[]>} ObjectSerializerContext */
1818
/** @import Hash from "../../util/Hash" */
1919

2020
const TRAILING_WHITESPACE_REGEXP = /\s+$/;
@@ -59,15 +59,13 @@ class HtmlInlineStyleDependency extends ModuleDependency {
5959
* @param {boolean=} attribute true when the source is a `style="..."` attribute (a block's contents) rather than a `<style>` block (stylesheet)
6060
* @param {boolean=} inAttribute true when the range is an attribute value (decoded on parse, so the processed CSS must be re-escaped on write-back); defaults to `attribute` — differs only for attribute values parsed as full stylesheets
6161
* @param {boolean=} characterData true when the range is a `<style>` body in a foreign (SVG/MathML) subtree, where the content model is character data rather than raw text, so the processed CSS is escaped as text on write-back
62-
* @param {string=} lang the `lang` a `<style>` body is written in, when not CSS (e.g. `scss`)
6362
*/
6463
constructor(
6564
request,
6665
range,
6766
attribute = false,
6867
inAttribute = attribute,
69-
characterData = false,
70-
lang = undefined
68+
characterData = false
7169
) {
7270
super(request);
7371
this.range = range;
@@ -77,8 +75,6 @@ class HtmlInlineStyleDependency extends ModuleDependency {
7775
this.inAttribute = inAttribute;
7876
/** @type {boolean} */
7977
this.characterData = characterData;
80-
/** @type {string | undefined} */
81-
this.lang = lang;
8278
}
8379

8480
get type() {
@@ -89,14 +85,6 @@ class HtmlInlineStyleDependency extends ModuleDependency {
8985
return this.attribute ? "html-style-attribute" : "html-style";
9086
}
9187

92-
/**
93-
* The element's `lang` for a rule's `with` condition (`with: { lang: "scss" }`).
94-
* @returns {{ lang: string } | undefined} the body's attributes
95-
*/
96-
get attributes() {
97-
return this.lang === undefined ? undefined : { lang: this.lang };
98-
}
99-
10088
/**
10189
* Updates the hash with the data contributed by this instance.
10290
* @param {Hash} hash hash to be updated
@@ -117,11 +105,9 @@ class HtmlInlineStyleDependency extends ModuleDependency {
117105
*/
118106
serialize(context) {
119107
super.serialize(context);
120-
context
121-
.write(this.attribute)
122-
.write(this.inAttribute)
123-
.write(this.characterData)
124-
.write(this.lang);
108+
context.write(this.attribute);
109+
context.write(this.inAttribute);
110+
context.write(this.characterData);
125111
}
126112

127113
/**
@@ -131,11 +117,8 @@ class HtmlInlineStyleDependency extends ModuleDependency {
131117
deserialize(context) {
132118
super.deserialize(context);
133119
this.attribute = context.read();
134-
const c1 = context.rest;
135-
this.inAttribute = c1.read();
136-
const c2 = c1.rest;
137-
this.characterData = c2.read();
138-
this.lang = c2.rest.read();
120+
this.inAttribute = context.read();
121+
this.characterData = context.read();
139122
}
140123
}
141124

‎lib/html/HtmlModulesPlugin.js‎

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,13 @@ const getCssModulesPlugin = memoize(() => require("../css/CssModulesPlugin"));
7171
/** @typedef {import("../../declarations/WebpackOptions").OutputHtmlOptions["manifest"]} ManifestOption */
7272
/** @typedef {Exclude<HtmlFaviconIcon, string>} FaviconIcon */
7373
/** @import Chunk, { ChunkFilenameTemplate } from "../graph/Chunk" */
74-
/** @import EntryDependency from "../entry/EntryDependency" */
7574
/** @import ChunkGraph from "../graph/ChunkGraph" */
7675
/** @import Compiler from "../Compiler" */
7776
/** @import Module from "../module/Module" */
7877
/** @typedef {import("../Compilation").EntryData} EntryData */
7978
/** @import { BuiltHtmlModule, HtmlModuleBuildInfo } from "./HtmlModule" */
8079
/** @import HtmlParser from "./HtmlParser" */
81-
/** @typedef {{ request: string, entryName: string, index: number, type: "script" | "script-module" | "modulepreload" | "stylesheet" | "html" | "preload" | "prefetch", css?: boolean, chunkName?: string, lang?: string }} HtmlEntryInfo */
80+
/** @typedef {{ request: string, entryName: string, index: number, type: "script" | "script-module" | "modulepreload" | "stylesheet" | "html" | "preload" | "prefetch", css?: boolean, chunkName?: string }} HtmlEntryInfo */
8281
/**
8382
* What one extracted entry needs to be named. Recorded in page order once
8483
* every page is built, and resolved to filenames once chunks exist.
@@ -112,7 +111,7 @@ const getCssModulesPlugin = memoize(() => require("../css/CssModulesPlugin"));
112111
* @property {boolean=} remove set true to delete the whole element
113112
*/
114113
/** @typedef {{ outputName: string, html: string }} HtmlTransformTagsContext */
115-
/** @typedef {{ context: string, request: string, name: string, index: number, dependOn: string[] | undefined, page: boolean, lang: string | undefined }} HtmlEntrySpec */
114+
/** @typedef {{ context: string, request: string, name: string, index: number, dependOn: string[] | undefined, page: boolean }} HtmlEntrySpec */
116115

117116
const createCompilationHooks = () => ({
118117
/**
@@ -695,8 +694,7 @@ class HtmlModulesPlugin {
695694
name: entry.entryName,
696695
index: entry.index,
697696
dependOn,
698-
page: groupKind === "html",
699-
lang: entry.lang
697+
page: groupKind === "html"
700698
});
701699
}
702700
}
@@ -812,18 +810,9 @@ class HtmlModulesPlugin {
812810
/** @type {(spec: HtmlEntrySpec) => Promise<Module | null>} */
813811
const addEntry = (spec) =>
814812
new Promise((resolve, reject) => {
815-
const dependency = EntryPlugin.createDependency(spec.request, {
816-
name: spec.name
817-
});
818-
// The same `with: { lang }` the page's own dependency on the body
819-
// matches, so both resolve to one module.
820-
if (spec.lang !== undefined) {
821-
/** @type {EntryDependency & { attributes: { lang: string } }} */
822-
(dependency).attributes = { lang: spec.lang };
823-
}
824813
compilation.addEntry(
825814
spec.context,
826-
dependency,
815+
EntryPlugin.createDependency(spec.request, { name: spec.name }),
827816
{
828817
name: spec.name,
829818
// A placeholder `afterChunks` overwrites for every entry it

‎lib/html/HtmlParser.js‎

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -729,18 +729,20 @@ const LANG_MEDIA_TYPES = dict({
729729
});
730730

731731
/**
732-
* The `lang` an inline `<style>` / `<script>` body is written in, as written,
733-
* when it names a language other than the element's own.
732+
* The media type of an inline `<style>` / `<script>` body whose `lang` names a
733+
* language other than the element's own: that language's files' media type.
734734
* @param {Map<string, string>} attrs attributes
735735
* @param {string} ownLanguage the element's own language, `css` or `js`
736-
* @returns {string | undefined} the body's language, or undefined for the element's own
736+
* @returns {string | undefined} the body's media type, or undefined when it is in the element's own language
737737
*/
738-
const inlineBodyLang = (attrs, ownLanguage) => {
738+
const langMediaType = (attrs, ownLanguage) => {
739739
const lang = attrs.get("lang");
740740
if (lang === undefined) return;
741-
const value = lang.trim();
742-
if (value === "" || value.toLowerCase() === ownLanguage) return;
743-
return value;
741+
const language = lang.trim().toLowerCase();
742+
if (language === ownLanguage || !MEDIA_TYPE_LANG_REGEXP.test(language)) {
743+
return;
744+
}
745+
return LANG_MEDIA_TYPES[language] || `text/x-${language}`;
744746
};
745747

746748
// Element-body sources, independent of the `sources` option: a `<script>` body
@@ -1111,7 +1113,6 @@ class HtmlParser extends Parser {
11111113
* @property {"script" | "script-module" | "modulepreload" | "stylesheet" | "html" | "preload" | "prefetch"} type
11121114
* @property {boolean=} css entry emits a CSS chunk (`<link rel="preload" as="style">`), so it needs the CSS filename template
11131115
* @property {string=} chunkName name the page asked this tag's chunk to claim, which names its file too
1114-
* @property {string=} lang an inline `<script>` body's `lang`, when not JavaScript
11151116
*/
11161117
/** @type {HtmlEntryInfo[]} */
11171118
const scriptEntries = [];
@@ -1245,17 +1246,10 @@ class HtmlParser extends Parser {
12451246
/**
12461247
* @param {string} mime mime type
12471248
* @param {string} text inline text
1248-
* @param {string=} lang the language the text is written in, from its element's `lang`
1249-
* @returns {string} a `data:` request for the text, typed as its `lang`'s files when it names one
1249+
* @returns {string} a `data:` request for the text
12501250
*/
1251-
const dataUri = (mime, text, lang) => {
1252-
let type = mime;
1253-
if (lang !== undefined && MEDIA_TYPE_LANG_REGEXP.test(lang)) {
1254-
const language = lang.toLowerCase();
1255-
type = LANG_MEDIA_TYPES[language] || `text/x-${language}`;
1256-
}
1257-
return `data:${type};base64,${Buffer.from(text, "utf8").toString("base64")}`;
1258-
};
1251+
const dataUri = (mime, text) =>
1252+
`data:${mime};base64,${Buffer.from(text, "utf8").toString("base64")}`;
12591253

12601254
/**
12611255
* @param {ConstDependency | HtmlSourceDependency | HtmlEntryDependency | HtmlInlineStyleDependency | HtmlInlineScriptDependency | HtmlInlineHtmlDependency} dep dependency
@@ -1738,16 +1732,15 @@ class HtmlParser extends Parser {
17381732
: decodeEntities(value, true)
17391733
);
17401734
if (cssText.trim() === "") break;
1741-
const lang = content
1742-
? inlineBodyLang(getAttributesMap(), "css")
1743-
: undefined;
1735+
const mediaType =
1736+
(content && langMediaType(getAttributesMap(), "css")) ||
1737+
"text/css";
17441738
const dep = new HtmlInlineStyleDependency(
1745-
dataUri("text/css", cssText, lang),
1739+
dataUri(mediaType, cssText),
17461740
[start, end],
17471741
content ? false : type === "stylesheet-style-attribute",
17481742
!content,
1749-
characterData,
1750-
lang
1743+
characterData
17511744
);
17521745
setLoc(dep, start, end);
17531746
module.addDependency(dep);
@@ -1792,11 +1785,10 @@ class HtmlParser extends Parser {
17921785
const scriptText = isHtmlElement(path)
17931786
? value
17941787
: decodeEntities(value, false);
1795-
const lang = inlineBodyLang(getAttributesMap(), "js");
17961788
const request = dataUri(
1797-
"text/javascript",
1798-
scriptText,
1799-
lang
1789+
langMediaType(getAttributesMap(), "js") ||
1790+
"text/javascript",
1791+
scriptText
18001792
);
18011793
const index = nextEntryIndex++;
18021794
// Module output upgrades a classic tag, so every extracted
@@ -1856,8 +1848,7 @@ class HtmlParser extends Parser {
18561848
entryName,
18571849
scriptType === "script-module" ? "esm" : "commonjs",
18581850
removeRange,
1859-
forceInline,
1860-
lang
1851+
forceInline
18611852
);
18621853
if (joinsRun) {
18631854
if (removeRange === undefined) {
@@ -1892,8 +1883,7 @@ class HtmlParser extends Parser {
18921883
entryName,
18931884
index,
18941885
type: scriptType,
1895-
chunkName: requestedFileName(entryName),
1896-
lang
1886+
chunkName: requestedFileName(entryName)
18971887
});
18981888
break;
18991889
}

‎schemas/WebpackOptions.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2532,7 +2532,7 @@
25322532
"added": "5.113.0"
25332533
},
25342534
"sources": {
2535-
"description": "Configure extraction of URL-like attribute values (e.g. `<img src>`, `<link href>`, `<script src>`) as webpack dependencies. `true` (default) uses the built-in source list; `false` disables extraction entirely so attributes are left untouched and `<script src>` / `<link rel=\"modulepreload\">` / `<link rel=\"stylesheet\">` no longer become compilation entries; an array lets you customize which `tag`/`attribute` pairs are treated as URLs and how they are bundled. Use the string `\"...\"` inside the array to inline the defaults. Inline `<script>` and `<style>` bodies are always processed; one whose `lang` names another language is a `data:` module typed as that language's files (`<style lang=\"scss\">` as `text/x-scss`, `<script lang=\"ts\">` as `text/typescript`), so a `mimetype` rule runs its loader, and `with: { lang }` matches the attribute as written. Use `webpackIgnore` comments or `IgnorePlugin` to skip individual URLs.",
2535+
"description": "Configure extraction of URL-like attribute values (e.g. `<img src>`, `<link href>`, `<script src>`) as webpack dependencies. `true` (default) uses the built-in source list; `false` disables extraction entirely so attributes are left untouched and `<script src>` / `<link rel=\"modulepreload\">` / `<link rel=\"stylesheet\">` no longer become compilation entries; an array lets you customize which `tag`/`attribute` pairs are treated as URLs and how they are bundled. Use the string `\"...\"` inside the array to inline the defaults. Inline `<script>` and `<style>` bodies are always processed; one whose `lang` names another language is a `data:` module typed as that language's files (`<style lang=\"scss\">` as `text/x-scss`, `<script lang=\"ts\">` as `text/typescript`), and anything else as `text/x-<lang>`, so a `mimetype` rule runs its loader. Use `webpackIgnore` comments or `IgnorePlugin` to skip individual URLs.",
25362536
"anyOf": [
25372537
{
25382538
"description": "Sources to extract as webpack dependencies. Use `\"...\"` to inline the default source list.",

‎test/configCases/html/inline-body-lang/index.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ it("should type a <style lang> body as its language's files", () => {
4646
}
4747
});
4848

49-
it("should match a <style lang> no media type can name with `with: { lang }`", () => {
49+
it("should leave a <style lang> no media type can name in CSS", () => {
5050
const sheet = sheetOf(".attribute-lang");
5151
expect(sheet).toContain("css data:text/css;base64,");
52-
expect(sheet).toContain(".attribute-lang { color: navy; }");
53-
expect(sheet).toContain(".by-with-rule-from-page-directory {}");
52+
expect(sheet).toContain("$accent: navy;");
53+
expect(sheet).not.toContain("by-");
5454
});
5555

5656
it("should type a <script lang> body as its language's files", () => {

‎test/configCases/html/inline-body-lang/webpack.config.js‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ const variablesLoader = (marker) => ({
2121

2222
/**
2323
* A `<style lang>` / `<script lang>` body is a `data:` module typed as its
24-
* language's files, so a `mimetype` rule runs that language's loader on it;
25-
* `with: { lang }` matches the attribute as written.
24+
* language's files, so a `mimetype` rule runs that language's loader on it.
2625
* @type {import("../../../../").Configuration}
2726
*/
2827
module.exports = {
@@ -40,7 +39,6 @@ module.exports = {
4039
{ mimetype: "text/x-scss", use: variablesLoader("by-scss-rule") },
4140
{ mimetype: "text/stylus", use: variablesLoader("by-stylus-rule") },
4241
{ mimetype: "text/x-postcss", use: variablesLoader("by-postcss-rule") },
43-
{ with: { lang: "no media type" }, use: variablesLoader("by-with-rule") },
4442
{
4543
mimetype: "text/x-upper",
4644
use: path.resolve(__dirname, "upper-loader.js")

0 commit comments

Comments
 (0)