@@ -7,11 +7,12 @@ import { EmbeddedFileSourceMap } from '../utils/sourceMaps';
7
7
import { SearchTexts } from '../utils/string' ;
8
8
import { getSlotsPropertyName , getVueLibraryName , TextRange } from '@volar/vue-code-gen' ;
9
9
import { Embedded , EmbeddedFile , Sfc } from '../vueFile' ;
10
- import { useSfcStyles } from './useSfcStyles' ;
11
10
import { EmbeddedFileMappingData } from '@volar/vue-code-gen' ;
12
11
import * as SourceMaps from '@volar/source-map' ;
13
12
import * as path from 'path' ;
14
13
import { walkInterpolationFragment } from '@volar/vue-code-gen/out/transform' ;
14
+ import { parseCssVars } from '../utils/parseCssVars' ;
15
+ import { parseCssClassNames } from '../utils/parseCssClassNames' ;
15
16
16
17
export function useSfcTemplateScript (
17
18
ts : typeof import ( 'typescript/lib/tsserverlibrary' ) ,
@@ -21,17 +22,13 @@ export function useSfcTemplateScript(
21
22
scriptSetup : Ref < Sfc [ 'scriptSetup' ] > ,
22
23
scriptSetupRanges : Ref < ReturnType < typeof parseScriptSetupRanges > | undefined > ,
23
24
styles : Ref < Sfc [ 'styles' ] > ,
24
- styleFiles : ReturnType < typeof useSfcStyles > [ 'files' ] ,
25
25
templateData : Ref < {
26
26
lang : string ,
27
27
htmlToTemplate : ( start : number , end : number ) => { start : number , end : number ; } | undefined ,
28
28
} | undefined > ,
29
29
sfcTemplateCompileResult : Ref < ReturnType < ( typeof import ( '@volar/vue-code-gen' ) ) [ 'compileSFCTemplate' ] > | undefined > ,
30
- sfcStyles : ReturnType < ( typeof import ( './useSfcStyles' ) ) [ 'useSfcStyles' ] > [ 'files' ] ,
31
30
scriptLang : Ref < string > ,
32
31
compilerOptions : VueCompilerOptions ,
33
- getCssVBindRanges : ( cssEmbeddeFile : EmbeddedFile ) => TextRange [ ] ,
34
- getCssClasses : ( cssEmbeddeFile : EmbeddedFile ) => TextRange [ ] ,
35
32
disableTemplateScript : boolean ,
36
33
) {
37
34
const baseFileName = path . basename ( fileName ) ;
@@ -50,34 +47,55 @@ export function useSfcTemplateScript(
50
47
return comments . join ( '\n' ) ;
51
48
} ) ;
52
49
const cssModuleClasses = computed ( ( ) => {
53
- const result : { style : typeof styleFiles [ 'value' ] [ number ] , index : number , classNameRanges : TextRange [ ] ; } [ ] = [ ] ;
54
- for ( let i = 0 ; i < styleFiles . value . length ; i ++ ) {
55
- const style = styleFiles . value [ i ] ;
56
- if ( style . data . module ) {
50
+ const result : { style : typeof styles [ 'value' ] [ number ] , index : number , classNameRanges : TextRange [ ] ; } [ ] = [ ] ;
51
+ for ( let i = 0 ; i < styles . value . length ; i ++ ) {
52
+ const style = styles . value [ i ] ;
53
+ if ( style . module ) {
57
54
result . push ( {
58
55
style : style ,
59
56
index : i ,
60
- classNameRanges : getCssClasses ( style ) ,
57
+ classNameRanges : [ ... parseCssClassNames ( style . content ) ] ,
61
58
} ) ;
62
59
}
63
60
}
64
61
return result ;
65
62
} ) ;
66
63
const cssScopedClasses = computed ( ( ) => {
67
- const result : { style : typeof styleFiles [ 'value' ] [ number ] , index : number , classNameRanges : TextRange [ ] ; } [ ] = [ ] ;
64
+ const result : { style : typeof styles [ 'value' ] [ number ] , index : number , classNameRanges : TextRange [ ] ; } [ ] = [ ] ;
68
65
const setting = compilerOptions . experimentalResolveStyleCssClasses ?? 'scoped' ;
69
- for ( let i = 0 ; i < styleFiles . value . length ; i ++ ) {
70
- const style = styleFiles . value [ i ] ;
71
- if ( ( setting === 'scoped' && style . data . scoped ) || setting === 'always' ) {
66
+ for ( let i = 0 ; i < styles . value . length ; i ++ ) {
67
+ const style = styles . value [ i ] ;
68
+ if ( ( setting === 'scoped' && style . scoped ) || setting === 'always' ) {
72
69
result . push ( {
73
70
style : style ,
74
71
index : i ,
75
- classNameRanges : getCssClasses ( style ) ,
72
+ classNameRanges : [ ... parseCssClassNames ( style . content ) ] ,
76
73
} ) ;
77
74
}
78
75
}
79
76
return result ;
80
77
} ) ;
78
+ const cssVars = computed ( ( ) => {
79
+ const result : { style : typeof styles [ 'value' ] [ number ] , index : number , ranges : TextRange [ ] ; } [ ] = [ ] ;
80
+ for ( let i = 0 ; i < styles . value . length ; i ++ ) {
81
+ const style = styles . value [ i ] ;
82
+ result . push ( {
83
+ style : style ,
84
+ index : i ,
85
+ ranges : [ ...parseCssVars ( style . content ) ] ,
86
+ } ) ;
87
+ }
88
+ return result ;
89
+ } ) ;
90
+ const cssVarTexts = computed ( ( ) => {
91
+ const result : string [ ] = [ ] ;
92
+ for ( const { style, ranges } of cssVars . value ) {
93
+ for ( const range of ranges ) {
94
+ result . push ( style . content . substring ( range . start , range . end ) ) ;
95
+ }
96
+ }
97
+ return result ;
98
+ } )
81
99
const templateCodeGens = computed ( ( ) => {
82
100
83
101
if ( ! templateData . value )
@@ -123,7 +141,7 @@ export function useSfcTemplateScript(
123
141
codeGen . addText ( `declare var __VLS_ctx: InstanceType<typeof __VLS_component> & {\n` ) ;
124
142
/* CSS Module */
125
143
for ( const cssModule of cssModuleClasses . value ) {
126
- codeGen . addText ( `${ cssModule . style . data . module } : Record<string, string>` ) ;
144
+ codeGen . addText ( `${ cssModule . style . module } : Record<string, string>` ) ;
127
145
for ( const classNameRange of cssModule . classNameRanges ) {
128
146
writeCssClassProperty (
129
147
cssModule . index ,
@@ -242,16 +260,11 @@ export function useSfcTemplateScript(
242
260
const emptyLocalVars : Record < string , number > = { } ;
243
261
const identifiers = new Set < string > ( ) ;
244
262
245
- for ( let i = 0 ; i < sfcStyles . value . length ; i ++ ) {
246
-
247
- const style = sfcStyles . value [ i ] ;
248
- const binds = getCssVBindRanges ( style ) ;
249
-
250
- for ( const cssBind of binds ) {
251
- const bindText = style . content . substring ( cssBind . start , cssBind . end ) ;
263
+ for ( const cssVar of cssVars . value ) {
264
+ for ( const cssBind of cssVar . ranges ) {
252
265
walkInterpolationFragment (
253
266
ts ,
254
- bindText ,
267
+ cssVar . style . content . substring ( cssBind . start , cssBind . end ) ,
255
268
( frag , fragOffset , isJustForErrorMapping ) => {
256
269
if ( fragOffset === undefined ) {
257
270
codeGen . addText ( frag ) ;
@@ -266,7 +279,7 @@ export function useSfcTemplateScript(
266
279
SourceMaps . Mode . Offset ,
267
280
{
268
281
vueTag : 'style' ,
269
- vueTagIndex : i ,
282
+ vueTagIndex : cssVar . index ,
270
283
capabilities : isJustForErrorMapping ? {
271
284
diagnostic : true ,
272
285
} : {
@@ -409,6 +422,7 @@ export function useSfcTemplateScript(
409
422
formatEmbedded,
410
423
inlineCssFile,
411
424
inlineCssEmbedded,
425
+ cssVarTexts,
412
426
} ;
413
427
414
428
function parseMappingSourceRange ( data : EmbeddedFileMappingData , range : SourceMaps . Range ) {
0 commit comments