@@ -4,7 +4,7 @@ import { endOfLine, generateSfcBlockSection, newLine } from '../common';
4
4
import { generateComponent , generateEmitsOption } from './component' ;
5
5
import type { ScriptCodegenContext } from './context' ;
6
6
import { ScriptCodegenOptions , codeFeatures } from './index' ;
7
- import { generateTemplate } from './template' ;
7
+ import { generateCssClassProperty , generateTemplate } from './template' ;
8
8
9
9
export function * generateScriptSetupImports (
10
10
scriptSetup : NonNullable < Sfc [ 'scriptSetup' ] > ,
@@ -211,6 +211,34 @@ function* generateSetupFunction(
211
211
] ) ;
212
212
}
213
213
}
214
+ if ( scriptSetupRanges . cssModules . length ) {
215
+ for ( const { exp, arg } of scriptSetupRanges . cssModules ) {
216
+ if ( arg ) {
217
+ setupCodeModifies . push ( [
218
+ [
219
+ ` as Omit<__VLS_StyleModules, '$style'>[` ,
220
+ generateSfcBlockSection ( scriptSetup , arg . start , arg . end , codeFeatures . all ) ,
221
+ `]`
222
+ ] ,
223
+ exp . end ,
224
+ exp . end
225
+ ] ) ;
226
+ }
227
+ else {
228
+ setupCodeModifies . push ( [
229
+ [
230
+ ` as __VLS_StyleModules[` ,
231
+ [ '' , scriptSetup . name , exp . start , codeFeatures . verification ] ,
232
+ `'$style'` ,
233
+ [ '' , scriptSetup . name , exp . end , codeFeatures . verification ] ,
234
+ `]`
235
+ ] ,
236
+ exp . end ,
237
+ exp . end
238
+ ] ) ;
239
+ }
240
+ }
241
+ }
214
242
for ( const { define } of scriptSetupRanges . templateRefs ) {
215
243
if ( define ?. arg ) {
216
244
setupCodeModifies . push ( [ [ `<__VLS_Refs[${ scriptSetup . content . slice ( define . arg . start , define . arg . end ) } ], keyof __VLS_Refs>` ] , define . arg . start - 1 , define . arg . start - 1 ] ) ;
@@ -247,6 +275,7 @@ function* generateSetupFunction(
247
275
248
276
yield * generateComponentProps ( options , ctx , scriptSetup , scriptSetupRanges , definePropMirrors ) ;
249
277
yield * generateModelEmits ( options , scriptSetup , scriptSetupRanges ) ;
278
+ yield * generateStyleModules ( options , ctx ) ;
250
279
yield * generateTemplate ( options , ctx , false ) ;
251
280
yield `type __VLS_Refs = ReturnType<typeof __VLS_template>['refs']${ endOfLine } ` ;
252
281
yield `type __VLS_Slots = ReturnType<typeof __VLS_template>['slots']${ endOfLine } ` ;
@@ -417,6 +446,45 @@ function* generateModelEmits(
417
446
yield endOfLine ;
418
447
}
419
448
449
+ function * generateStyleModules (
450
+ options : ScriptCodegenOptions ,
451
+ ctx : ScriptCodegenContext
452
+ ) : Generator < Code > {
453
+ const styles = options . sfc . styles . filter ( style => style . module ) ;
454
+ if ( ! styles . length ) {
455
+ return ;
456
+ }
457
+ yield `type __VLS_StyleModules = {${ newLine } ` ;
458
+ for ( let i = 0 ; i < styles . length ; i ++ ) {
459
+ const style = styles [ i ] ;
460
+ const { name, offset } = style . module ! ;
461
+ if ( offset ) {
462
+ yield [
463
+ name ,
464
+ 'main' ,
465
+ offset + 1 ,
466
+ codeFeatures . all
467
+ ] ;
468
+ }
469
+ else {
470
+ yield name ;
471
+ }
472
+ yield `: Record<string, string> & ${ ctx . helperTypes . Prettify . name } <{}` ;
473
+ for ( const className of style . classNames ) {
474
+ yield * generateCssClassProperty (
475
+ i ,
476
+ className . text ,
477
+ className . offset ,
478
+ 'string' ,
479
+ false
480
+ ) ;
481
+ }
482
+ yield `>${ endOfLine } ` ;
483
+ }
484
+ yield `}` ;
485
+ yield endOfLine ;
486
+ }
487
+
420
488
function * generateDefinePropType (
421
489
scriptSetup : NonNullable < Sfc [ 'scriptSetup' ] > ,
422
490
propName : string | undefined ,
@@ -453,7 +521,7 @@ function getPropAndLocalName(
453
521
? 'modelValue'
454
522
: localName ;
455
523
if ( defineProp . name ) {
456
- propName = propName ! . replace ( / [ ' " ] + / g, '' )
524
+ propName = propName ! . replace ( / [ ' " ] + / g, '' ) ;
457
525
}
458
526
return [ propName , localName ] ;
459
527
}
0 commit comments