1
1
import MagicString from 'magic-string' ;
2
- import { variants } from 'windicss/src/processor/variants' ;
3
-
4
2
import { walk , parse } from 'svelte/compiler' ;
5
- import { StyleSheet } from 'windicss/src/utils/style' ;
6
- import { compile , interpret , preflight } from 'windicss/src' ;
7
- import { CSSParser } from 'windicss/src/utils/css' ;
8
- import { optimizeBuild } from 'windicss/src/utils/algorithm' ;
9
-
3
+ import { StyleSheet } from 'windicss/utils/style' ;
4
+ import { Processor } from 'windicss/lib' ;
5
+ import { CSSParser } from 'windicss/utils/parser' ;
6
+ import type { Config } from 'windicss/types/interfaces' ;
10
7
import type { TemplateNode } from 'svelte/types/compiler/interfaces'
11
8
import type { Preprocessor } from 'svelte/types/compiler/preprocess' ;
12
9
@@ -17,21 +14,27 @@ interface ChildNode {
17
14
name ?:string ;
18
15
}
19
16
20
- const mode = process . env . NODE_ENV ;
21
- const dev = mode === 'development' ;
22
- const variantKeys = [ ...Object . keys ( variants ) , 'xxl' ] ;
17
+ const DEV = process . env . NODE_ENV === 'development' ;
23
18
19
+ let PROCESSOR :Processor ;
20
+ let VARIANTS :string [ ] = [ ] ;
24
21
let IGNORED_CLASSES :string [ ] = [ ] ;
25
22
let STYLESHEETS :StyleSheet [ ] = [ ] ;
26
23
let DIRECTIVES :StyleSheet [ ] = [ ] ;
27
24
let FILES :( string | undefined ) [ ] = [ ] ;
28
25
29
26
let TAGNAMES :{ [ key :string ] :string | undefined } = { } ;
30
- let OPTIONS = {
27
+ let OPTIONS :{
28
+ config ?: Config | string ,
29
+ compile ?: boolean ,
30
+ prefix ?: string ,
31
+ globalPreflight ?: boolean ,
32
+ globalUtility ?: boolean ,
33
+ } = {
31
34
compile : true ,
32
- globalPreflight : true ,
33
- globalUtility : true ,
34
35
prefix : 'windi-' ,
36
+ globalPreflight : true ,
37
+ globalUtility : true ,
35
38
} ;
36
39
37
40
function combineStyleList ( stylesheets :StyleSheet [ ] ) {
@@ -49,14 +52,14 @@ function globalStyleSheet(styleSheet:StyleSheet) {
49
52
}
50
53
51
54
function compilation ( classNames :string ) {
52
- const utility = compile ( classNames , OPTIONS . prefix , false ) ;
55
+ const utility = PROCESSOR . compile ( classNames , OPTIONS . prefix , false ) ;
53
56
IGNORED_CLASSES = [ ...IGNORED_CLASSES , ...utility . ignored ] ;
54
57
STYLESHEETS . push ( OPTIONS . globalUtility ?globalStyleSheet ( utility . styleSheet ) :utility . styleSheet ) ;
55
58
return utility . className ? [ utility . className , ...utility . ignored ] . join ( ' ' ) : utility . ignored . join ( ' ' ) ; // return new className
56
59
}
57
60
58
61
function interpretation ( classNames :string ) {
59
- const utility = interpret ( classNames ) ;
62
+ const utility = PROCESSOR . interpret ( classNames ) ;
60
63
IGNORED_CLASSES = [ ...IGNORED_CLASSES , ...utility . ignored ] ;
61
64
let styleSheet = utility . styleSheet ;
62
65
STYLESHEETS . push ( OPTIONS . globalUtility ?globalStyleSheet ( styleSheet ) :styleSheet ) ;
@@ -85,7 +88,7 @@ const _preprocess:Preprocessor = ({content, filename}) => {
85
88
if ( style ) {
86
89
// handle tailwind directives ...
87
90
style = style . replace ( / < \/ ? s t y l e [ ^ > ] * > / g, '' ) ;
88
- STYLESHEETS . push ( new CSSParser ( style ) . parse ( undefined , true ) ) ;
91
+ STYLESHEETS . push ( new CSSParser ( style , PROCESSOR ) . parse ( undefined ) ) ;
89
92
content = content . replace ( styleRegex , '' ) ;
90
93
}
91
94
const parsed = parse ( content ) ;
@@ -107,7 +110,7 @@ const _preprocess:Preprocessor = ({content, filename}) => {
107
110
classStart = i . start ;
108
111
classes = [ ...classes , ...i . value . map ( ( { data} :ChildNode ) => data ) ] ;
109
112
code . overwrite ( i . start , i . end , '' ) ;
110
- } else if ( variantKeys . includes ( i . name ) ) {
113
+ } else if ( VARIANTS . includes ( i . name ) ) {
111
114
// handle variant properties
112
115
classStart = i . start ;
113
116
classes = [ ...classes , ...i . value . map ( ( { data} :ChildNode ) => addVariant ( data , i . name === 'xxl' ? '2xl' : i . name ) ) ] ;
@@ -127,15 +130,15 @@ const _preprocess:Preprocessor = ({content, filename}) => {
127
130
128
131
if ( node . type === 'Class' ) {
129
132
// handle class directive
130
- const utility = interpret ( node . name ) ;
133
+ const utility = PROCESSOR . interpret ( node . name ) ;
131
134
IGNORED_CLASSES = [ ...IGNORED_CLASSES , ...utility . ignored ] ;
132
135
DIRECTIVES . push ( utility . styleSheet ) ;
133
136
// make sure directives add after all classes.
134
137
}
135
138
136
139
if ( node . type === "ConditionalExpression" ) {
137
140
// handle inline conditional expression
138
- const utility = interpret ( `${ getReduceValue ( node , 'consequent' ) } ${ getReduceValue ( node , 'alternate' ) } ` ) ;
141
+ const utility = PROCESSOR . interpret ( `${ getReduceValue ( node , 'consequent' ) } ${ getReduceValue ( node , 'alternate' ) } ` ) ;
139
142
IGNORED_CLASSES = [ ...IGNORED_CLASSES , ...utility . ignored ] ;
140
143
DIRECTIVES . push ( utility . styleSheet ) ;
141
144
}
@@ -151,13 +154,12 @@ const _preprocess:Preprocessor = ({content, filename}) => {
151
154
} ;
152
155
153
156
// preflights might lost when refresh
154
- const preflights = preflight ( dev ?Object . keys ( TAGNAMES ) :updatedTags , FILES . length === 0 || FILES . indexOf ( filename ) === 0 ) ; // only enable global styles for first file
157
+ const preflights = PROCESSOR . preflight ( DEV ?Object . keys ( TAGNAMES ) :updatedTags , FILES . length === 0 || FILES . indexOf ( filename ) === 0 ) ; // only enable global styles for first file
155
158
156
- const outputCSS = optimizeBuild (
157
- ( OPTIONS . globalPreflight ? globalStyleSheet ( preflights ) : preflights )
159
+ const outputCSS = ( OPTIONS . globalPreflight ? globalStyleSheet ( preflights ) : preflights )
158
160
. extend ( combineStyleList ( STYLESHEETS ) )
159
161
. extend ( combineStyleList ( DIRECTIVES ) )
160
- ) ;
162
+ . build ( ) ;
161
163
162
164
code . trimEnd ( ) . append ( `\n\n<style>\n${ outputCSS } </style>` ) ;
163
165
@@ -170,7 +172,9 @@ const _preprocess:Preprocessor = ({content, filename}) => {
170
172
return { code : code . toString ( ) } ;
171
173
}
172
174
173
- export function preprocess ( options = { } ) {
175
+ export function preprocess ( options : typeof OPTIONS = { } ) {
174
176
OPTIONS = { ...OPTIONS , ...options } ; // change global settings here;
177
+ PROCESSOR = new Processor ( OPTIONS . config ) ;
178
+ VARIANTS = [ ...Object . keys ( PROCESSOR . resolveVariants ( ) ) , 'xxl' ] ;
175
179
return _preprocess ;
176
180
}
0 commit comments