Skip to content

Commit 5b1a1e7

Browse files
refactor(css): resolve imported @value declarations through one path
1 parent 8caccc0 commit 5b1a1e7

2 files changed

Lines changed: 111 additions & 133 deletions

File tree

‎lib/css/CssParser.js‎

Lines changed: 69 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,6 +1715,36 @@ class CssParser extends Parser {
17151715
return isDashedIdentifier(value) ? value.slice(2) : undefined;
17161716
};
17171717

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+
17181748
/**
17191749
* 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.
17201750
* @param {string} identifier ident as written in the source
@@ -1724,34 +1754,39 @@ class CssParser extends Parser {
17241754
* @param {import("../dependencies/css/CssIcssExportDependency").ExportType} exportType export type
17251755
*/
17261756
const addLocalizedExport = (identifier, start, end, kind, exportType) => {
1727-
const { line: sl, column: sc } = locConverter.get(start);
1728-
const { line: el, column: ec } = locConverter.get(end);
17291757
const def =
17301758
icssDefinitions.size === 0
17311759
? undefined
17321760
: 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);
17331772
let name = identifier;
17341773
let exportMode = CssIcssExportDependency.EXPORT_MODE.ONCE;
17351774
if (def !== undefined) {
1736-
if (def.importName) {
1737-
exportMode = CssIcssExportDependency.EXPORT_MODE.RESOLVED_NAME;
1775+
const value = typeof def.value === "string" ? def.value.trim() : "";
1776+
if (isSimpleIdentifier(value)) {
1777+
name = value;
17381778
} else {
1739-
const value = typeof def.value === "string" ? def.value.trim() : "";
1740-
if (isSimpleIdentifier(value)) {
1741-
name = value;
1742-
} else {
1743-
exportMode = CssIcssExportDependency.EXPORT_MODE.NONE;
1744-
this._emitWarning(
1745-
state,
1746-
CssIcssExportDependency.notSimpleIdentifierMessage(
1747-
identifier,
1748-
value
1749-
),
1750-
locConverter,
1751-
start,
1752-
end
1753-
);
1754-
}
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+
);
17551790
}
17561791
}
17571792
if (
@@ -2581,18 +2616,10 @@ class CssParser extends Parser {
25812616
/** @type {IcssDefinition} */ (icssDefinitions.get(ident))
25822617
.importName
25832618
) {
2584-
// Whether it names one `.class` / `#id` is known once the import resolves.
2585-
const { line: sl, column: sc } = locConverter.get(identStart);
2586-
const { line: el, column: ec } = locConverter.get(identEnd);
2587-
addCssExport(
2588-
sl,
2589-
sc,
2590-
el,
2591-
ec,
2619+
addResolvedExport(
25922620
ident,
2593-
getReexport(ident),
2594-
[identStart, identEnd],
2595-
true,
2621+
identStart,
2622+
identEnd,
25962623
CssIcssExportDependency.EXPORT_MODE.RESOLVED_SELECTOR
25972624
);
25982625
break;
@@ -4318,29 +4345,6 @@ class CssParser extends Parser {
43184345
);
43194346
};
43204347
// Collect idents/strings to export — top-level only, except grid-template recurses (`[line-name]` blocks live in `repeat(…)`).
4321-
/**
4322-
* Emit a property-value ident naming an imported `@value`, whose text, and so whether it is a name or a keyword of this property, is known once the import resolves.
4323-
* @param {number} start ident start offset
4324-
* @param {number} end ident end offset
4325-
* @param {string} identifier `@value` name as written
4326-
* @returns {void}
4327-
*/
4328-
const emitResolvedReference = (start, end, identifier) => {
4329-
const { line: sl, column: sc } = locConverter.get(start);
4330-
const { line: el, column: ec } = locConverter.get(end);
4331-
const reexport = /** @type {string[]} */ (getReexport(identifier));
4332-
addCssExport(
4333-
sl,
4334-
sc,
4335-
el,
4336-
ec,
4337-
identifier,
4338-
[reexport[0], reexport[1], reexport[2], declPropertyName],
4339-
[start, end],
4340-
true,
4341-
CssIcssExportDependency.EXPORT_MODE.RESOLVED_REFERENCE
4342-
);
4343-
};
43444348
/** @type {(cvs: AstNode[]) => void} */
43454349
const walkExports = (cvs) => {
43464350
for (const cv of cvs) {
@@ -4375,7 +4379,17 @@ class CssParser extends Parser {
43754379
let resolved = identifier;
43764380
if (def !== undefined) {
43774381
if (def.importName) {
4378-
emitResolvedReference(A.start(cv), A.end(cv), identifier);
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+
);
43794393
break;
43804394
}
43814395
// `@value` resolves first: what isn't still a name, or is now a

‎lib/dependencies/css/CssIcssExportDependency.js‎

Lines changed: 42 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,9 @@ const EXPORT_MODE = {
8080
/**
8181
* Whether an entry is only resolved once its imported `@value` is, so its `name` is no export key.
8282
* @param {ExportMode} exportMode export mode
83-
* @returns {boolean} true for the `RESOLVED_*` modes
83+
* @returns {boolean} true for the `RESOLVED_*` modes, numbered last
8484
*/
85-
const isResolvedMode = (exportMode) =>
86-
exportMode === EXPORT_MODE.RESOLVED_NAME ||
87-
exportMode === EXPORT_MODE.RESOLVED_SELECTOR ||
88-
exportMode === EXPORT_MODE.RESOLVED_REFERENCE;
85+
const isResolvedMode = (exportMode) => exportMode >= EXPORT_MODE.RESOLVED_NAME;
8986

9087
/**
9188
* The warning for a `@value` standing in for a declared name (an at-rule prelude, `local()`) whose value is not one identifier.
@@ -468,18 +465,16 @@ class CssIcssExportDependency extends NullDependency {
468465
let module;
469466
let exportsInfo;
470467
let convention;
468+
/** @type {ResolutionCache | undefined} */
469+
let cache;
471470
for (const entry of this.entries) {
472471
if (entry.exportMode === EXPORT_MODE.RESOLVED_NAME) {
473472
const parent =
474473
/** @type {CssModule | undefined} */
475474
(moduleGraph.getParentModule(this));
476475
if (!parent) return warnings;
477-
const text = resolveImportedText(
478-
moduleGraph,
479-
parent,
480-
entry,
481-
createResolutionCache()
482-
);
476+
if (cache === undefined) cache = createResolutionCache();
477+
const text = resolveImportedText(moduleGraph, parent, entry, cache);
483478
if (text === undefined || getCssTokens().isSimpleIdentifier(text)) {
484479
continue;
485480
}
@@ -826,13 +821,13 @@ const resolveImportedText = (moduleGraph, module, entry, cache, visited) => {
826821
};
827822

828823
/**
829-
* What a `RESOLVED_*` entry declares once its import resolves to a plain value: one ident, or for a selector one `.class` / `#id`. Anything else is substituted verbatim and not exported, like a local `@value` of that shape.
824+
* What a `RESOLVED_*` entry scopes once its import resolves to a plain value, as a local `@value` would: one ident (not a keyword of a reference's property), or for a selector one `.class` / `#id`. Anything else is substituted verbatim.
830825
* @param {ModuleGraph} moduleGraph module graph
831826
* @param {CssModule} module module owning the entry
832827
* @param {CssExportEntry} entry `RESOLVED_*` entry
833828
* @param {ResolutionCache} cache resolution cache
834829
* @param {CssModule[]=} visited collects each module the chain passes through
835-
* @returns {{ prefix: string, name: string } | undefined} selector prefix (empty for a name) and the ident to scope and export
830+
* @returns {{ prefix: string, name: string } | undefined} selector prefix (empty for a name) and the ident to scope
836831
*/
837832
const resolveDeclaration = (moduleGraph, module, entry, cache, visited) => {
838833
const text = resolveImportedText(moduleGraph, module, entry, cache, visited);
@@ -845,7 +840,16 @@ const resolveDeclaration = (moduleGraph, module, entry, cache, visited) => {
845840
? { prefix, name: text.slice(1) }
846841
: undefined;
847842
}
848-
return isSimpleIdentifier(text) ? { prefix: "", name: text } : undefined;
843+
if (!isSimpleIdentifier(text)) return undefined;
844+
if (entry.exportMode === EXPORT_MODE.RESOLVED_REFERENCE) {
845+
const keywords = getCssData()
846+
.getCssModulesKeywords()
847+
.get(/** @type {string[]} */ (entry.value)[3]);
848+
if (keywords !== undefined && keywords.has(text.toLowerCase())) {
849+
return undefined;
850+
}
851+
}
852+
return { prefix: "", name: text };
849853
};
850854

851855
CssIcssExportDependency.Template = class CssIcssExportDependencyTemplate extends (
@@ -1052,27 +1056,23 @@ CssIcssExportDependency.Template = class CssIcssExportDependencyTemplate extends
10521056

10531057
for (const entry of dep.entries) {
10541058
if (!entry.range && !isJs) continue;
1059+
// Neither exported nor rewritten in JS, so not worth resolving there.
1060+
if (isJs && entry.exportMode === EXPORT_MODE.RESOLVED_REFERENCE) continue;
10551061
const isReference = Array.isArray(entry.value);
10561062

10571063
/** @type {string} */
10581064
let value;
1059-
// A resolved `.class` / `#id` keeps its prefix in the CSS only.
1060-
let selectorPrefix = "";
1065+
/** @type {{ prefix: string, name: string } | undefined} */
1066+
let declaration;
10611067
if (entry.exportType === EXPORT_TYPE.COMPOSES && isJs) {
10621068
value = CssIcssExportDependencyTemplate.resolveReferences(
10631069
entry,
10641070
templateContext,
10651071
new Set(),
10661072
cache
10671073
).join(" ");
1068-
} else if (entry.exportMode === EXPORT_MODE.RESOLVED_SELECTOR) {
1069-
const declaration = resolveDeclaration(
1070-
moduleGraph,
1071-
module,
1072-
entry,
1073-
cache
1074-
);
1075-
if (declaration !== undefined) selectorPrefix = declaration.prefix;
1074+
} else if (isResolvedMode(entry.exportMode)) {
1075+
declaration = resolveDeclaration(moduleGraph, module, entry, cache);
10761076
value =
10771077
declaration !== undefined
10781078
? CssIcssExportDependencyTemplate.getIdentifier(
@@ -1086,30 +1086,11 @@ CssIcssExportDependency.Template = class CssIcssExportDependencyTemplate extends
10861086
templateContext,
10871087
/** @type {string[]} */ (entry.value)[2],
10881088
new Set(),
1089-
cache
1090-
) || /** @type {string[]} */ (entry.value)[0];
1091-
} else if (entry.exportMode === EXPORT_MODE.RESOLVED_REFERENCE) {
1092-
const text = resolveImportedText(moduleGraph, module, entry, cache);
1093-
const property = /** @type {string[]} */ (entry.value)[3];
1094-
const keywords = getCssData().getCssModulesKeywords().get(property);
1095-
// Resolved first, then scoped like a local `@value`: only a name that
1096-
// isn't one of the property's keywords.
1097-
value =
1098-
text !== undefined &&
1099-
getCssTokens().isSimpleIdentifier(text) &&
1100-
!(keywords !== undefined && keywords.has(text.toLowerCase()))
1101-
? CssIcssExportDependencyTemplate.getIdentifier(
1102-
text,
1103-
entry,
1104-
templateContext
1105-
)
1106-
: CssIcssExportDependencyTemplate.resolve(
1107-
/** @type {string[]} */ (entry.value)[0],
1108-
/** @type {string[]} */ (entry.value)[1],
1109-
templateContext,
1110-
/** @type {string[]} */ (entry.value)[2],
1111-
new Set(),
1112-
cache
1089+
cache,
1090+
// A declared name is still scoped, as a local `@value` is.
1091+
entry.exportMode === EXPORT_MODE.RESOLVED_NAME
1092+
? entry
1093+
: undefined
11131094
) || /** @type {string[]} */ (entry.value)[0];
11141095
} else if (isReference) {
11151096
const resolved = CssIcssExportDependencyTemplate.resolve(
@@ -1142,35 +1123,15 @@ CssIcssExportDependency.Template = class CssIcssExportDependencyTemplate extends
11421123
}`;
11431124
}
11441125

1145-
if (
1146-
isJs &&
1147-
entry.exportMode !== EXPORT_MODE.NONE &&
1148-
entry.exportMode !== EXPORT_MODE.RESOLVED_REFERENCE
1149-
) {
1150-
/** @type {string[]} */
1151-
let names;
1152-
if (isResolvedMode(entry.exportMode)) {
1153-
const declaration = resolveDeclaration(
1154-
moduleGraph,
1155-
module,
1156-
entry,
1157-
cache
1158-
);
1159-
names =
1160-
declaration !== undefined
1161-
? cssExportConvention(
1162-
declaration.name,
1163-
/** @type {CssGeneratorExportsConvention} */
1164-
(generator.options.exportsConvention)
1165-
)
1166-
: [];
1167-
} else {
1168-
names = entryConventionNames(
1169-
entry,
1170-
/** @type {CssGeneratorExportsConvention} */
1171-
(generator.options.exportsConvention)
1172-
);
1173-
}
1126+
if (isJs && entry.exportMode !== EXPORT_MODE.NONE) {
1127+
const convention =
1128+
/** @type {CssGeneratorExportsConvention} */
1129+
(generator.options.exportsConvention);
1130+
const names = !isResolvedMode(entry.exportMode)
1131+
? entryConventionNames(entry, convention)
1132+
: declaration !== undefined
1133+
? cssExportConvention(declaration.name, convention)
1134+
: [];
11741135
/** @type {Set<string>} */
11751136
const allNames = new Set();
11761137
for (const name of names) {
@@ -1233,10 +1194,13 @@ CssIcssExportDependency.Template = class CssIcssExportDependencyTemplate extends
12331194
entry.exportMode !== EXPORT_MODE.APPEND &&
12341195
entry.exportMode !== EXPORT_MODE.SELF_REFERENCE
12351196
) {
1197+
// A resolved `.class` / `#id` keeps its prefix in the CSS only.
12361198
source.replace(
12371199
entry.range[0],
12381200
entry.range[1] - 1,
1239-
`${selectorPrefix}${value}`
1201+
declaration !== undefined && declaration.prefix !== ""
1202+
? declaration.prefix + value
1203+
: value
12401204
);
12411205
}
12421206
}

0 commit comments

Comments
 (0)