Skip to content

Commit ee47dc2

Browse files
fix(css): resolve an imported @value, then scope it like a local one (#22443)
* fix(css): export a @value-named at-rule under its resolved name * fix(css): export a @value-named at-rule only under a single resolved ident * fix(css): resolve an imported @value, then scope it like a local one * refactor(css): resolve imported @value declarations through one path * chore(changeset): follow the changeset naming and length rules * fix(css): point an imported @value at the name its defining module scoped * fix(css): keep an imported @value keyword as text even when its module scoped that name
1 parent 9f94bf3 commit ee47dc2

16 files changed

Lines changed: 822 additions & 113 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Scope and export a CSS Modules `@value` the same way whether local or imported.

‎lib/css/CssParser.js‎

Lines changed: 165 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const {
3535
buildSkipSet,
3636
equalsLowerCase,
3737
isDashedIdentifier,
38+
isSimpleIdentifier,
3839
isWhitespace,
3940
normalizeUrl,
4041
rangeEquals,
@@ -286,10 +287,6 @@ const VALUE_AS_ALIAS = /\s+as\s+/;
286287
// `@value name value`: end of the name run (first non-space followed by space).
287288
const VALUE_NAME_BOUNDARY = /\S\s/;
288289
const ONLY_WHITESPACE = /^\s+$/;
289-
// A `@value` whose whole value is one `.class` / `#id`, the only shape whose
290-
// localizable ident is knowable without re-parsing the substitution.
291-
const SIMPLE_CLASS_OR_ID_VALUE =
292-
/^([.#])(-?(?:[A-Za-z_]|[\u0080-\uFFFF])(?:[\w-]|[\u0080-\uFFFF])*)$/;
293290
// Relative request prefix (`./` or `../`) — `isSelfReferenceRequest` per `from`.
294291
const RELATIVE_REQUEST = /^\.{1,2}\//;
295292

@@ -1693,9 +1690,13 @@ class CssParser extends Parser {
16931690
if (!def || def.importName || typeof def.value !== "string") {
16941691
return undefined;
16951692
}
1696-
const match = SIMPLE_CLASS_OR_ID_VALUE.exec(def.value.trim());
1697-
return match
1698-
? [/** @type {"." | "#"} */ (match[1]), match[2]]
1693+
// Only one `.class` / `#id` has a localizable ident knowable without
1694+
// re-parsing the substitution.
1695+
const value = def.value.trim();
1696+
const prefix = value.charAt(0);
1697+
return (prefix === "." || prefix === "#") &&
1698+
isSimpleIdentifier(value.slice(1))
1699+
? [prefix, value.slice(1)]
16991700
: undefined;
17001701
};
17011702

@@ -1714,6 +1715,100 @@ class CssParser extends Parser {
17141715
return isDashedIdentifier(value) ? value.slice(2) : undefined;
17151716
};
17161717

1718+
/**
1719+
* Add the entry for an ident naming an imported `@value`, which `CssIcssExportDependency` scopes and names once the import resolves.
1720+
* @param {string} identifier `@value` name as written
1721+
* @param {number} start ident start offset
1722+
* @param {number} end ident end offset
1723+
* @param {import("../dependencies/css/CssIcssExportDependency").ExportMode} exportMode one of the `RESOLVED_*` modes
1724+
* @param {import("../dependencies/css/CssIcssExportDependency").Value=} value the reference, when it carries more than `getReexport` gives
1725+
*/
1726+
const addResolvedExport = (
1727+
identifier,
1728+
start,
1729+
end,
1730+
exportMode,
1731+
value = getReexport(identifier)
1732+
) => {
1733+
const { line: sl, column: sc } = locConverter.get(start);
1734+
const { line: el, column: ec } = locConverter.get(end);
1735+
addCssExport(
1736+
sl,
1737+
sc,
1738+
el,
1739+
ec,
1740+
identifier,
1741+
value,
1742+
[start, end],
1743+
true,
1744+
exportMode
1745+
);
1746+
};
1747+
1748+
/**
1749+
* Add the export entry for an ident a declaration site (an at-rule prelude, `local()`) localizes. A `@value` is resolved first: a local one exports under the ident it names, an imported one is left for `CssIcssExportDependency` to name once the import resolves.
1750+
* @param {string} identifier ident as written in the source
1751+
* @param {number} start ident start offset
1752+
* @param {number} end ident end offset
1753+
* @param {string | undefined} kind export kind for conflict tracking, undefined for a usage
1754+
* @param {import("../dependencies/css/CssIcssExportDependency").ExportType} exportType export type
1755+
*/
1756+
const addLocalizedExport = (identifier, start, end, kind, exportType) => {
1757+
const def =
1758+
icssDefinitions.size === 0
1759+
? undefined
1760+
: icssDefinitions.get(identifier);
1761+
if (def !== undefined && def.importName) {
1762+
addResolvedExport(
1763+
identifier,
1764+
start,
1765+
end,
1766+
CssIcssExportDependency.EXPORT_MODE.RESOLVED_NAME
1767+
);
1768+
return;
1769+
}
1770+
const { line: sl, column: sc } = locConverter.get(start);
1771+
const { line: el, column: ec } = locConverter.get(end);
1772+
let name = identifier;
1773+
let exportMode = CssIcssExportDependency.EXPORT_MODE.ONCE;
1774+
if (def !== undefined) {
1775+
const value = typeof def.value === "string" ? def.value.trim() : "";
1776+
if (isSimpleIdentifier(value)) {
1777+
name = value;
1778+
} else {
1779+
exportMode = CssIcssExportDependency.EXPORT_MODE.NONE;
1780+
this._emitWarning(
1781+
state,
1782+
CssIcssExportDependency.notSimpleIdentifierMessage(
1783+
identifier,
1784+
value
1785+
),
1786+
locConverter,
1787+
start,
1788+
end
1789+
);
1790+
}
1791+
}
1792+
if (
1793+
kind !== undefined &&
1794+
exportMode === CssIcssExportDependency.EXPORT_MODE.ONCE
1795+
) {
1796+
recordDeclaration(name, kind, sl, sc);
1797+
}
1798+
addCssExport(
1799+
sl,
1800+
sc,
1801+
el,
1802+
ec,
1803+
name,
1804+
getReexport(identifier),
1805+
[start, end],
1806+
true,
1807+
exportMode,
1808+
exportType
1809+
);
1810+
};
1811+
17171812
/**
17181813
* Process a `local(...)` / `global(...)` pseudo-function: strip the call (and a leading legacy `:`) via a presentational dep, then emit `local()`'s inner top-level idents as ICSS exports.
17191814
* @param {FunctionNode} fn parsed local/global function node
@@ -1733,18 +1828,22 @@ class CssParser extends Parser {
17331828
for (const cv of A.children(fn)) {
17341829
if (A.type(cv) !== NodeType.Ident) continue;
17351830
let identifier = A.unescaped(cv);
1831+
if (!isDashedIdentifier(identifier)) {
1832+
addLocalizedExport(
1833+
identifier,
1834+
A.start(cv),
1835+
A.end(cv),
1836+
declarationKind,
1837+
CssIcssExportDependency.EXPORT_TYPE.NORMAL
1838+
);
1839+
continue;
1840+
}
17361841
// Cursor reads instead of `A.loc` — no location objects allocated.
17371842
const { line: sl, column: sc } = locConverter.get(A.start(cv));
17381843
const { line: el, column: ec } = locConverter.get(A.end(cv));
1739-
const isDashedIdent = isDashedIdentifier(identifier);
1740-
if (isDashedIdent) identifier = identifier.slice(2);
1844+
identifier = identifier.slice(2);
17411845
if (declarationKind !== undefined) {
1742-
recordDeclaration(
1743-
identifier,
1744-
isDashedIdent ? "custom property" : declarationKind,
1745-
sl,
1746-
sc
1747-
);
1846+
recordDeclaration(identifier, "custom property", sl, sc);
17481847
}
17491848
addCssExport(
17501849
sl,
@@ -1756,9 +1855,7 @@ class CssParser extends Parser {
17561855
[A.start(cv), A.end(cv)],
17571856
true,
17581857
CssIcssExportDependency.EXPORT_MODE.ONCE,
1759-
isDashedIdent
1760-
? CssIcssExportDependency.EXPORT_TYPE.CUSTOM_VARIABLE
1761-
: CssIcssExportDependency.EXPORT_TYPE.NORMAL
1858+
CssIcssExportDependency.EXPORT_TYPE.CUSTOM_VARIABLE
17621859
);
17631860
}
17641861
}
@@ -1814,19 +1911,11 @@ class CssParser extends Parser {
18141911
) {
18151912
continue;
18161913
}
1817-
const { line: sl, column: sc } = locConverter.get(A.start(cv));
1818-
const { line: el, column: ec } = locConverter.get(A.end(cv));
1819-
recordDeclaration(identifier, kind, sl, sc);
1820-
addCssExport(
1821-
sl,
1822-
sc,
1823-
el,
1824-
ec,
1914+
addLocalizedExport(
18251915
identifier,
1826-
getReexport(identifier),
1827-
[A.start(cv), A.end(cv)],
1828-
true,
1829-
CssIcssExportDependency.EXPORT_MODE.ONCE,
1916+
A.start(cv),
1917+
A.end(cv),
1918+
kind,
18301919
CssIcssExportDependency.EXPORT_TYPE.NORMAL
18311920
);
18321921
found = true;
@@ -2522,6 +2611,19 @@ class CssParser extends Parser {
25222611
? localizableSelectorValue(ident)
25232612
: undefined;
25242613
if (localizable === undefined) {
2614+
if (
2615+
segmentMode === "local" &&
2616+
/** @type {IcssDefinition} */ (icssDefinitions.get(ident))
2617+
.importName
2618+
) {
2619+
addResolvedExport(
2620+
ident,
2621+
identStart,
2622+
identEnd,
2623+
CssIcssExportDependency.EXPORT_MODE.RESOLVED_SELECTOR
2624+
);
2625+
break;
2626+
}
25252627
emitICSSSymbol(ident, identStart, identEnd);
25262628
break;
25272629
}
@@ -4270,14 +4372,46 @@ class CssParser extends Parser {
42704372
}
42714373
break;
42724374
}
4375+
const def =
4376+
isGridProperty || icssDefinitions.size === 0
4377+
? undefined
4378+
: icssDefinitions.get(identifier);
4379+
let resolved = identifier;
4380+
if (def !== undefined) {
4381+
if (def.importName) {
4382+
// The property rides along: its keywords stay unscoped.
4383+
const reference = /** @type {string[]} */ (
4384+
getReexport(identifier)
4385+
);
4386+
addResolvedExport(
4387+
identifier,
4388+
A.start(cv),
4389+
A.end(cv),
4390+
CssIcssExportDependency.EXPORT_MODE.RESOLVED_REFERENCE,
4391+
[reference[0], reference[1], reference[2], declPropertyName]
4392+
);
4393+
break;
4394+
}
4395+
// `@value` resolves first: what isn't still a name, or is now a
4396+
// keyword, is substituted as is rather than scoped.
4397+
resolved =
4398+
typeof def.value === "string" ? def.value.trim() : "";
4399+
if (!isSimpleIdentifier(resolved)) {
4400+
emitICSSSymbol(identifier, A.start(cv), A.end(cv));
4401+
break;
4402+
}
4403+
}
42734404
// Values are almost always lowercase already — avoid the copy.
4274-
const keyword = toLowerCaseIfNeeded(identifier);
4405+
const keyword = toLowerCaseIfNeeded(resolved);
42754406
parsedKeywords[keyword] =
42764407
typeof parsedKeywords[keyword] !== "undefined"
42774408
? parsedKeywords[keyword] + 1
42784409
: 0;
42794410
const limit = keywords.get(keyword);
42804411
if (limit !== undefined && parsedKeywords[keyword] < limit) {
4412+
if (def !== undefined) {
4413+
emitICSSSymbol(identifier, A.start(cv), A.end(cv));
4414+
}
42814415
break;
42824416
}
42834417
emit(A.start(cv), A.end(cv));

‎lib/css/syntax-parser.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,17 @@ const asciiLowerCaseName = (s) => {
570570
const isDashedIdentifier = (identifier) =>
571571
identifier.startsWith("--") && identifier.length >= 3;
572572

573+
// One unescaped `<ident-token>` spelling: no escapes, whitespace or punctuation.
574+
const SIMPLE_IDENTIFIER =
575+
/^-?(?:[A-Za-z_]|[\u0080-\uFFFF])(?:[\w-]|[\u0080-\uFFFF])*$/;
576+
577+
/**
578+
* Whether a string is one plain identifier, as a `@value` must be to name a class, at-rule or JS export.
579+
* @param {string} value candidate text, already trimmed
580+
* @returns {boolean} true when the whole string is one unescaped identifier
581+
*/
582+
const isSimpleIdentifier = (value) => SIMPLE_IDENTIFIER.test(value);
583+
573584
/**
574585
* Consume an escaped code point.
575586
* @param {string} input input
@@ -16988,6 +16999,7 @@ module.exports.escapeIdentifier = escapeIdentifier;
1698816999
module.exports.escapeName = escapeName;
1698917000
module.exports.grammar = grammar;
1699017001
module.exports.isDashedIdentifier = isDashedIdentifier;
17002+
module.exports.isSimpleIdentifier = isSimpleIdentifier;
1699117003
module.exports.isWhitespace = isWhitespace;
1699217004
module.exports.normalizeUrl = normalizeUrl;
1699317005
module.exports.parseABlocksContents = parseABlocksContents;

0 commit comments

Comments
 (0)