@@ -54,6 +54,37 @@ const getExportsConvention = (
5454 return conventionMap [ convention ] ?? 'camel-case' ;
5555} ;
5656
57+ const LOCAL_IDENT_HASH_REGEX =
58+ / \[ (?: ( [ ^ : \] ] + ) : ) ? (?: ( h a s h | c o n t e n t h a s h | f u l l h a s h ) ) (?: : ( [ a - z ] + \d * ) ) ? (?: : ( \d + ) ) ? \] / i;
59+ const LOCAL_IDENT_HASH_REGEX_GLOBAL =
60+ / \[ (?: ( [ ^ : \] ] + ) : ) ? (?: h a s h | c o n t e n t h a s h | f u l l h a s h ) (?: : ( [ a - z ] + \d * ) ) ? (?: : ( \d + ) ) ? \] / gi;
61+
62+ const getLocalIdentOptions = (
63+ localIdentName : string | undefined ,
64+ ) : Partial < Rspack . CssModuleGeneratorOptions > => {
65+ if ( localIdentName === undefined ) {
66+ return { } ;
67+ }
68+
69+ const match = localIdentName . match ( LOCAL_IDENT_HASH_REGEX ) ;
70+ if ( ! match ) {
71+ return { localIdentName } ;
72+ }
73+
74+ const [ , hashFunction , , hashDigest , hashDigestLength ] = match ;
75+
76+ return {
77+ localIdentName : localIdentName . replace (
78+ LOCAL_IDENT_HASH_REGEX_GLOBAL ,
79+ ( _match , _hashFunction , currentHashName ) =>
80+ currentHashName === 'fullhash' ? '[fullhash]' : '[hash]' ,
81+ ) ,
82+ ...( hashFunction ? { localIdentHashFunction : hashFunction } : { } ) ,
83+ ...( hashDigest ? { localIdentHashDigest : hashDigest } : { } ) ,
84+ ...( hashDigestLength ? { localIdentHashDigestLength : Number ( hashDigestLength ) } : { } ) ,
85+ } ;
86+ } ;
87+
5788const getResolveConfig = ( rule : RspackChain . Rule < unknown > ) =>
5889 (
5990 rule . resolve as RspackChain . Rule < unknown > [ 'resolve' ] & {
@@ -111,11 +142,17 @@ const applyCssModuleConfig = ({
111142 } ;
112143 const generatorOptions : Rspack . CssModuleGeneratorOptions = {
113144 ...options . generator ,
145+ // Rspack currently generates an invalid `exports` reference when style exports
146+ // with default CSS Module exports are concatenated. CommonJS output prevents
147+ // concatenation for these modules while preserving Rsbuild's default exports.
148+ ...( config . output . injectStyles &&
149+ ! cssModules . namedExport &&
150+ options . generator ?. esModule === undefined
151+ ? { esModule : false }
152+ : { } ) ,
114153 exportsOnly : ! emitCss ,
115154 exportsConvention : getExportsConvention ( cssModules . exportLocalsConvention ) ,
116- ...( cssModules . localIdentName === undefined
117- ? { }
118- : { localIdentName : cssModules . localIdentName } ) ,
155+ ...getLocalIdentOptions ( cssModules . localIdentName ) ,
119156 } ;
120157
121158 chain . module . parser . merge ( {
0 commit comments