@@ -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 */
837832const 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
851855CssIcssExportDependency . 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