Skip to content

Commit 23e918e

Browse files
fix(css): keep an imported @value keyword as text even when its module scoped that name
1 parent 3e96f9c commit 23e918e

4 files changed

Lines changed: 54 additions & 15 deletions

File tree

‎lib/dependencies/css/CssIcssExportDependency.js‎

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -822,9 +822,17 @@ const resolveImportedEntry = (
822822
* @param {CssExportEntry} entry entry whose value is `[localName, importName, request?]`
823823
* @param {ResolutionCache} cache resolution cache
824824
* @param {CssModule[]=} visited collects each module the chain passes through
825+
* @param {boolean=} localized resolve to what the defining module scoped under that name, when it did
825826
* @returns {string | undefined} the trimmed plain value, if any
826827
*/
827-
const resolveImportedText = (moduleGraph, module, entry, cache, visited) => {
828+
const resolveImportedText = (
829+
moduleGraph,
830+
module,
831+
entry,
832+
cache,
833+
visited,
834+
localized = true
835+
) => {
828836
const value = /** @type {string[]} */ (entry.value);
829837
const target = resolveImportedEntry(
830838
moduleGraph,
@@ -835,14 +843,14 @@ const resolveImportedText = (moduleGraph, module, entry, cache, visited) => {
835843
new Set(),
836844
cache,
837845
visited,
838-
true
846+
localized
839847
);
840848
if (!target || target.entry.interpolate) return undefined;
841849
return /** @type {string} */ (target.entry.value).trim();
842850
};
843851

844852
/**
845-
* 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.
853+
* What a `RESOLVED_*` entry scopes once its import resolves to a plain value, as a local `@value` would: one ident, or for a selector one `.class` / `#id`. Anything else is substituted verbatim.
846854
* @param {ModuleGraph} moduleGraph module graph
847855
* @param {CssModule} module module owning the entry
848856
* @param {CssExportEntry} entry `RESOLVED_*` entry
@@ -861,16 +869,32 @@ const resolveDeclaration = (moduleGraph, module, entry, cache, visited) => {
861869
? { prefix, name: text.slice(1) }
862870
: undefined;
863871
}
864-
if (!isSimpleIdentifier(text)) return undefined;
865-
if (entry.exportMode === EXPORT_MODE.RESOLVED_REFERENCE) {
866-
const keywords = getCssData()
867-
.getCssModulesKeywords()
868-
.get(/** @type {string[]} */ (entry.value)[3]);
869-
if (keywords !== undefined && keywords.has(text.toLowerCase())) {
870-
return undefined;
871-
}
872-
}
873-
return { prefix: "", name: text };
872+
return isSimpleIdentifier(text) ? { prefix: "", name: text } : undefined;
873+
};
874+
875+
/**
876+
* Whether a `RESOLVED_REFERENCE` resolves to a keyword of its property (`linear` in `animation`), which stays text even where its defining module scoped that name.
877+
* @param {ModuleGraph} moduleGraph module graph
878+
* @param {CssModule} module module owning the entry
879+
* @param {CssExportEntry} entry entry
880+
* @param {ResolutionCache} cache resolution cache
881+
* @returns {boolean} true for a keyword reference
882+
*/
883+
const isReferenceKeyword = (moduleGraph, module, entry, cache) => {
884+
if (entry.exportMode !== EXPORT_MODE.RESOLVED_REFERENCE) return false;
885+
const keywords = getCssData()
886+
.getCssModulesKeywords()
887+
.get(/** @type {string[]} */ (entry.value)[3]);
888+
if (keywords === undefined) return false;
889+
const text = resolveImportedText(
890+
moduleGraph,
891+
module,
892+
entry,
893+
cache,
894+
undefined,
895+
false
896+
);
897+
return text !== undefined && keywords.has(text.toLowerCase());
874898
};
875899

876900
CssIcssExportDependency.Template = class CssIcssExportDependencyTemplate extends (
@@ -1097,7 +1121,10 @@ CssIcssExportDependency.Template = class CssIcssExportDependencyTemplate extends
10971121
cache
10981122
).join(" ");
10991123
} else if (isResolvedMode(entry.exportMode)) {
1100-
declaration = resolveDeclaration(moduleGraph, module, entry, cache);
1124+
const keyword = isReferenceKeyword(moduleGraph, module, entry, cache);
1125+
declaration = keyword
1126+
? undefined
1127+
: resolveDeclaration(moduleGraph, module, entry, cache);
11011128
value =
11021129
declaration !== undefined
11031130
? CssIcssExportDependencyTemplate.getIdentifier(
@@ -1116,7 +1143,7 @@ CssIcssExportDependency.Template = class CssIcssExportDependencyTemplate extends
11161143
entry.exportMode === EXPORT_MODE.RESOLVED_NAME
11171144
? entry
11181145
: undefined,
1119-
true
1146+
!keyword
11201147
) || /** @type {string[]} */ (entry.value)[0];
11211148
} else if (isReference) {
11221149
const resolved = CssIcssExportDependencyTemplate.resolve(

‎test/configCases/css/css-modules-value-at-rule-prelude/__snapshots__/ConfigCacheTest.snap‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ exports[`ConfigCacheTestCases css css-modules-value-at-rule-prelude exported tes
2222
from { opacity: 0; }
2323
}
2424
25+
.value-at-rule-prelude-values_module_css-linear {
26+
color: black;
27+
}
28+
2529
/*!******************************!*\\\\
2630
!*** css ./style.module.css ***!
2731
\\\\******************************/

‎test/configCases/css/css-modules-value-at-rule-prelude/__snapshots__/ConfigTest.snap‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ exports[`ConfigTestCases css css-modules-value-at-rule-prelude exported tests sh
2222
from { opacity: 0; }
2323
}
2424
25+
.value-at-rule-prelude-values_module_css-linear {
26+
color: black;
27+
}
28+
2529
/*!******************************!*\\\\
2630
!*** css ./style.module.css ***!
2731
\\\\******************************/

‎test/configCases/css/css-modules-value-at-rule-prelude/values.module.css‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@
1515
@keyframes definedAnimName {
1616
from { opacity: 0; }
1717
}
18+
19+
.linear {
20+
color: black;
21+
}

0 commit comments

Comments
 (0)