Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/html-css-modules-followups.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"webpack": patch
---

Give an inline `<style module>` its own page's names when another page holds the same text, and scope the fragment of an unquoted link to the page's own file.
52 changes: 38 additions & 14 deletions lib/dependencies/html/HtmlCssModuleNamesDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const getSourceProcessor = memoize(
* from the page when generating, so a page keeps only offsets), then its kind
* (a `CSS_MODULE_NAME_*` constant), whether it is inside `<template>` content
* (`CSS_MODULE_NAME_IN_TEMPLATE`), and the quote it is written in shifted
* above both (a char code, 0 when unquoted).
* above both (a char code, 0 when unquoted, 1 when unquoted and starting within its value).
* @typedef {number[]} CssModuleNames
*/

Expand Down Expand Up @@ -71,6 +71,9 @@ const CSS_MODULE_NAME_KIND_MASK = 7;
// A value inside `<template>` content, which is not in the document tree.
const CSS_MODULE_NAME_IN_TEMPLATE = 8;
const CSS_MODULE_NAME_QUOTE_SHIFT = 4;
// The quote of an unquoted span that starts within its value, as a self-link's
// fragment does: no quote can be opened there.
const CSS_MODULE_NAME_MID_VALUE = 1;

/** @type {Record<HtmlCssModulesAttributeKind, number>} */
const KIND_BY_NAME = {
Expand Down Expand Up @@ -145,6 +148,13 @@ const getCssModuleNameAttributes = (extra) => {
const WHITESPACE_SPLIT_REGEXP = /([\t\n\f\r ]+)/;
// What an unquoted attribute value cannot hold (§13.2.5.36).
const UNQUOTED_UNSAFE_REGEXP = /[\t\n\f\r "'=<>`]/;
const UNQUOTED_UNSAFE_GLOBAL_REGEXP = /[\t\n\f\r "'=<>`]/g;
/**
* The character reference an unquoted value writes for one it cannot hold.
* @param {string} char the character
* @returns {string} its reference
*/
const escapeUnquotedChar = (char) => `&#${char.charCodeAt(0)};`;
// A `url(#id)` reference; the id is what sits between the `#` and the close.
const URL_FRAGMENT_REGEXP = /(url\(\s*)(["']?)#([^"')\s]+)\2(\s*\))/gi;
const URL_REGEXP = /url\(/i;
Expand Down Expand Up @@ -282,10 +292,11 @@ const collectCssModuleSelfLink = (
if (resolved !== self.file && !(self.index && resolved === self.dir)) {
return;
}
// The fragment is rewritten in place, inside a quoted value only: an
// unquoted one would be quoted from its middle.
const quote = source.charCodeAt(start - 1);
if (quote !== 34 && quote !== 39) return;
// The fragment is rewritten in place; in an unquoted value, what the new
// name holds that the value cannot is written as a character reference.
const before = source.charCodeAt(start - 1);
const quote =
before === 34 || before === 39 ? before : CSS_MODULE_NAME_MID_VALUE;
if (isExtracted !== undefined && isExtracted()) return;
names.push(
hash,
Expand Down Expand Up @@ -636,16 +647,29 @@ HtmlCssModuleNamesDependency.Template = class HtmlCssModuleNamesDependencyTempla
}
}
if (result === undefined) continue;
const escaped = escapeAttribute(result, quote === 0 ? undefined : quote);
// An unquoted value is quoted once it holds what one cannot: the space
// `composes` adds, or a `>` a scoped name decodes to.
source.replace(
start,
end - 1,
quote !== 0 || (escaped !== "" && !UNQUOTED_UNSAFE_REGEXP.test(escaped))
? escaped
: `"${escaped}"`
const escaped = escapeAttribute(
result,
quote === 34 || quote === 39 ? quote : undefined
);
if (
quote === 34 ||
quote === 39 ||
(escaped !== "" && !UNQUOTED_UNSAFE_REGEXP.test(escaped))
) {
source.replace(start, end - 1, escaped);
} else if (quote === 0) {
// An unquoted value is quoted once it holds what one cannot: the
// space `composes` adds, or a `>` a scoped name decodes to.
source.replace(start, end - 1, `"${escaped}"`);
} else {
// A span from the value's middle, as a self-link's fragment is,
// can't open a quote: it writes character references instead.
source.replace(
start,
end - 1,
escaped.replace(UNQUOTED_UNSAFE_GLOBAL_REGEXP, escapeUnquotedChar)
);
}
}
}
};
Expand Down
31 changes: 24 additions & 7 deletions lib/html/HtmlParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1429,7 +1429,9 @@ class HtmlParser extends Parser {
module.addPresentationalDependency(
new ConstDependency(
replacement.length > 0
? `${spaceTaken ? " " : ""}${replacement.join(" ")}${separated ? "" : " "}`
? `${spaceTaken ? " " : ""}${replacement.join(" ")}${
separated ? "" : " "
}`
: "",
[attrStart, attrEnd]
)
Expand All @@ -1440,10 +1442,13 @@ class HtmlParser extends Parser {
/**
* @param {string} mime mime type
* @param {string} text inline text
* @param {string=} page a page key, for a body scoped to its page
* @returns {string} a `data:` request for the text
*/
const dataUri = (mime, text) =>
`data:${mime};base64,${Buffer.from(text, "utf8").toString("base64")}`;
const dataUri = (mime, text, page) =>
`data:${mime}${
page === undefined ? "" : `;page=${page}`
};base64,${Buffer.from(text, "utf8").toString("base64")}`;

/**
* @param {ConstDependency | HtmlSourceDependency | HtmlEntryDependency | HtmlInlineStyleDependency | HtmlInlineScriptDependency | HtmlInlineHtmlDependency | HtmlCssModuleDependency} dep dependency
Expand Down Expand Up @@ -1591,7 +1596,9 @@ class HtmlParser extends Parser {
const { line: el, column: ec } = locConverter.get(end);
module.addWarning(
new (getUnsupportedFeatureWarning())(
`\`webpackIgnore\` expected a boolean, but received: ${String(options.webpackIgnore)}.`,
`\`webpackIgnore\` expected a boolean, but received: ${String(
options.webpackIgnore
)}.`,
{
start: { line: sl, column: sc },
end: { line: el, column: ec }
Expand All @@ -1612,7 +1619,9 @@ class HtmlParser extends Parser {
const { line: el, column: ec } = locConverter.get(end);
module.addWarning(
new (getUnsupportedFeatureWarning())(
`\`webpackInline\` expected a boolean, but received: ${String(options.webpackInline)}.`,
`\`webpackInline\` expected a boolean, but received: ${String(
options.webpackInline
)}.`,
{
start: { line: sl, column: sc },
end: { line: el, column: ec }
Expand All @@ -1634,7 +1643,9 @@ class HtmlParser extends Parser {
const { line: el, column: ec } = locConverter.get(end);
module.addWarning(
new (getUnsupportedFeatureWarning())(
`\`webpackChunkName\` expected a non-empty string, but received: ${String(options.webpackChunkName)}.`,
`\`webpackChunkName\` expected a non-empty string, but received: ${String(
options.webpackChunkName
)}.`,
{
start: { line: sl, column: sc },
end: { line: el, column: ec }
Expand Down Expand Up @@ -2071,7 +2082,13 @@ class HtmlParser extends Parser {
sheetRange = foreign.textRanges[0];
}
const dep = new HtmlInlineStyleDependency(
dataUri(mediaType, cssText),
// A `<style module>` body is its page's: the same text on
// another page is another module, with that page's names.
dataUri(
mediaType,
cssText,
cssModule ? entryPrefix : undefined
),
sheetRange,
content ? false : type === "stylesheet-style-attribute",
!content,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ exports[`ConfigCacheTestCases html css-modules-icss exported tests should genera
\\\\************************************************/
.deep\\\\.module_deep { margin: 3px; }

/*!****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************!*\\\\
!*** css data:text/css;base64,CkB2YWx1ZSBicmFuZDogcmVkOwpAdmFsdWUgcHJpbWFyeUNvbG9yLCBnYXAgZnJvbSAiLi92YWx1ZXMubW9kdWxlLmNzcyI7CkB2YWx1ZSBzaGFyZWQgYXMgc2hhcmVkQWxpYXMgZnJvbSAiLi92YWx1ZXMubW9kdWxlLmNzcyI7CkB2YWx1ZSBzbWFsbCBmcm9tICIuL3ZhbHVlcy5tb2R1bGUuY3NzIjsKOmltcG9ydCgiLi9iYXNlLm1vZHVsZS5jc3MiKSB7IGltcG9ydGVkQmFzZTogYmFzZTsgfQo6ZXhwb3J0IHsgZXhwb3J0ZWRDb2xvcjogYmx1ZTsgZXhwb3J0ZWROYW1lOiBub3RBQ2xhc3M7IH0KLmxvY2FsIHsgY29sb3I6IGJyYW5kOyBtYXJnaW46IGdhcDsgfQouY29tcG9zZWQgeyBjb21wb3NlczogbG9jYWw7IH0KLm11bHRpIHsgY29tcG9zZXM6IGxvY2FsIGNvbXBvc2VkOyB9Ci5mcm9tRmlsZSB7IGNvbXBvc2VzOiBiYXNlIGZyb20gIi4vYmFzZS5tb2R1bGUuY3NzIjsgfQouZnJvbUZpbGVNYW55IHsgY29tcG9zZXM6IGJhc2Ugb3RoZXIgZnJvbSAiLi9iYXNlLm1vZHVsZS5jc3MiOyB9Ci5mcm9tR2xvYmFsIHsgY29tcG9zZXM6IGdsb2JhbFRoaW5nIGZyb20gZ2xvYmFsOyB9Ci52aWFJbXBvcnQgeyBjb21wb3NlczogaW1wb3J0ZWRCYXNlOyB9Ci5jaGFpbiB7IGNvbXBvc2VzOiBmcm9tRmlsZTsgY29sb3I6IHByaW1hcnlDb2xvcjsgfQouZGVlcENoYWluIHsgY29tcG9zZXM6IGRlZXAgZnJvbSAiLi9kZWVwLm1vZHVsZS5jc3MiOyB9Ci51c2VzVmFsdWUgeyBib3JkZXItd2lkdGg6IHNoYXJlZEFsaWFzOyB9CjpnbG9iYWwoLnNoZWxsKSB7IGNvbG9yOiBibGFjazsgfQo6bG9jYWwoLmV4cGxpY2l0KSB7IGNvbG9yOiBncmF5OyB9Ci53aXRoXDpjb2xvbiB7IGNvbG9yOiB0ZWFsOyB9Ci5kdXAgeyBjb2xvcjogbmF2eTsgfQojYW5jaG9yIHsgY29sb3I6IGJyYW5kOyB9CkBtZWRpYSBzbWFsbCB7IC5yZXNwb25zaXZlIHsgY29sb3I6IGJyYW5kOyB9IH0KQGtleWZyYW1lcyBzcGluIHsgdG8geyBvcGFjaXR5OiAwOyB9IH0KLnNwaW5uaW5nIHsgYW5pbWF0aW9uOiBzcGluIDFzOyB9Ci5hXD5iIHsgY29sb3I6IG9saXZlOyB9CltjbGFzc349ImJ5QXR0cmlidXRlIl0geyBjb2xvcjogY29yYWw7IH0K (exportType: text) ***!
\\\\****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************/
/*!**************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************!*\\\\
!*** css data:text/css;page=page;base64,CkB2YWx1ZSBicmFuZDogcmVkOwpAdmFsdWUgcHJpbWFyeUNvbG9yLCBnYXAgZnJvbSAiLi92YWx1ZXMubW9kdWxlLmNzcyI7CkB2YWx1ZSBzaGFyZWQgYXMgc2hhcmVkQWxpYXMgZnJvbSAiLi92YWx1ZXMubW9kdWxlLmNzcyI7CkB2YWx1ZSBzbWFsbCBmcm9tICIuL3ZhbHVlcy5tb2R1bGUuY3NzIjsKOmltcG9ydCgiLi9iYXNlLm1vZHVsZS5jc3MiKSB7IGltcG9ydGVkQmFzZTogYmFzZTsgfQo6ZXhwb3J0IHsgZXhwb3J0ZWRDb2xvcjogYmx1ZTsgZXhwb3J0ZWROYW1lOiBub3RBQ2xhc3M7IH0KLmxvY2FsIHsgY29sb3I6IGJyYW5kOyBtYXJnaW46IGdhcDsgfQouY29tcG9zZWQgeyBjb21wb3NlczogbG9jYWw7IH0KLm11bHRpIHsgY29tcG9zZXM6IGxvY2FsIGNvbXBvc2VkOyB9Ci5mcm9tRmlsZSB7IGNvbXBvc2VzOiBiYXNlIGZyb20gIi4vYmFzZS5tb2R1bGUuY3NzIjsgfQouZnJvbUZpbGVNYW55IHsgY29tcG9zZXM6IGJhc2Ugb3RoZXIgZnJvbSAiLi9iYXNlLm1vZHVsZS5jc3MiOyB9Ci5mcm9tR2xvYmFsIHsgY29tcG9zZXM6IGdsb2JhbFRoaW5nIGZyb20gZ2xvYmFsOyB9Ci52aWFJbXBvcnQgeyBjb21wb3NlczogaW1wb3J0ZWRCYXNlOyB9Ci5jaGFpbiB7IGNvbXBvc2VzOiBmcm9tRmlsZTsgY29sb3I6IHByaW1hcnlDb2xvcjsgfQouZGVlcENoYWluIHsgY29tcG9zZXM6IGRlZXAgZnJvbSAiLi9kZWVwLm1vZHVsZS5jc3MiOyB9Ci51c2VzVmFsdWUgeyBib3JkZXItd2lkdGg6IHNoYXJlZEFsaWFzOyB9CjpnbG9iYWwoLnNoZWxsKSB7IGNvbG9yOiBibGFjazsgfQo6bG9jYWwoLmV4cGxpY2l0KSB7IGNvbG9yOiBncmF5OyB9Ci53aXRoXDpjb2xvbiB7IGNvbG9yOiB0ZWFsOyB9Ci5kdXAgeyBjb2xvcjogbmF2eTsgfQojYW5jaG9yIHsgY29sb3I6IGJyYW5kOyB9CkBtZWRpYSBzbWFsbCB7IC5yZXNwb25zaXZlIHsgY29sb3I6IGJyYW5kOyB9IH0KQGtleWZyYW1lcyBzcGluIHsgdG8geyBvcGFjaXR5OiAwOyB9IH0KLnNwaW5uaW5nIHsgYW5pbWF0aW9uOiBzcGluIDFzOyB9Ci5hXD5iIHsgY29sb3I6IG9saXZlOyB9CltjbGFzc349ImJ5QXR0cmlidXRlIl0geyBjb2xvcjogY29yYWw7IH0K (exportType: text) ***!
\\\\**************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************/



Expand Down Expand Up @@ -59,9 +59,9 @@ exports[`ConfigCacheTestCases html css-modules-icss exported tests should genera
.base\\\\.module_base { padding: 1px; }
.base\\\\.module_other { padding: 2px; }

/*!*************************************************************************************************************************************************************************************************************************************************************!*\\\\
!*** css data:text/less;base64,CkBwYWQ6IDVweDsKLmxlc3NDb21wb3NlcyB7IGNvbXBvc2VzOiBvdGhlciBmcm9tICIuL2Jhc2UubW9kdWxlLmNzcyI7IHBhZGRpbmc6IEBwYWQ7IH0KLmxlc3NMb2NhbCB7IGNvbXBvc2VzOiBsZXNzQ29tcG9zZXM7IH0KLmxhdGVyIHsgY29sb3I6IGxpbWU7IH0K (exportType: text) ***!
\\\\*************************************************************************************************************************************************************************************************************************************************************/
/*!***********************************************************************************************************************************************************************************************************************************************************************!*\\\\
!*** css data:text/less;page=page;base64,CkBwYWQ6IDVweDsKLmxlc3NDb21wb3NlcyB7IGNvbXBvc2VzOiBvdGhlciBmcm9tICIuL2Jhc2UubW9kdWxlLmNzcyI7IHBhZGRpbmc6IEBwYWQ7IH0KLmxlc3NMb2NhbCB7IGNvbXBvc2VzOiBsZXNzQ29tcG9zZXM7IH0KLmxhdGVyIHsgY29sb3I6IGxpbWU7IH0K (exportType: text) ***!
\\\\***********************************************************************************************************************************************************************************************************************************************************************/
.m_lessComposes {
padding: 5px;
}
Expand Down Expand Up @@ -90,9 +90,9 @@ exports[`ConfigCacheTestCases html css-modules-icss exported tests should genera
\\\\************************************************/
.deep\\\\.module_deep { margin: 3px; }

/*!*******************************************************************************************************************************************************************************************************************************************************************************************!*\\\\
!*** css data:text/x-scss;base64,CkB2YWx1ZSBwcmltYXJ5Q29sb3IgZnJvbSAiLi92YWx1ZXMubW9kdWxlLmNzcyI7CiRzaXplOiA2cHg7Ci5zY3NzVmFsdWUgeyBjb2xvcjogcHJpbWFyeUNvbG9yOyB3aWR0aDogJHNpemU7IH0KLnNjc3NDb21wb3NlcyB7IGNvbXBvc2VzOiBkZWVwIGZyb20gIi4vZGVlcC5tb2R1bGUuY3NzIjsgfQo= (exportType: text) ***!
\\\\*******************************************************************************************************************************************************************************************************************************************************************************************/
/*!*****************************************************************************************************************************************************************************************************************************************************************************************************!*\\\\
!*** css data:text/x-scss;page=page;base64,CkB2YWx1ZSBwcmltYXJ5Q29sb3IgZnJvbSAiLi92YWx1ZXMubW9kdWxlLmNzcyI7CiRzaXplOiA2cHg7Ci5zY3NzVmFsdWUgeyBjb2xvcjogcHJpbWFyeUNvbG9yOyB3aWR0aDogJHNpemU7IH0KLnNjc3NDb21wb3NlcyB7IGNvbXBvc2VzOiBkZWVwIGZyb20gIi4vZGVlcC5tb2R1bGUuY3NzIjsgfQo= (exportType: text) ***!
\\\\*****************************************************************************************************************************************************************************************************************************************************************************************************/



Expand Down
Loading
Loading