@@ -6,13 +6,15 @@ import windicssPreset from '@unocss/preset-wind'
6
6
import { parse as CSSParser , walk as CSSWalker } from 'css-tree'
7
7
import MagicString from 'magic-string'
8
8
import { bgRed , white } from 'nanocolors'
9
- import { preprocess } from 'svelte/compiler'
9
+ import { parse , preprocess } from 'svelte/compiler'
10
10
import type {
11
11
PreprocessorGroup ,
12
12
Processed ,
13
13
} from 'svelte/types/compiler/preprocess'
14
14
import { loadConfig } from 'unconfig'
15
15
import { FileHandler , SetObject } from './utils'
16
+ import fg from 'fast-glob'
17
+ import { readFileSync } from 'fs'
16
18
17
19
export interface BaseConfig {
18
20
silent ?: boolean
@@ -36,31 +38,74 @@ let windiConfiguration: any
36
38
let windiGenerator : UnoGenerator
37
39
let iconGenerator : UnoGenerator
38
40
39
- const styleMap = new Map <
40
- string ,
41
- {
42
- data : SetObject
43
- updatedAt : number
44
- writtenAt ?: number
45
- }
46
- > ( [
41
+ const styleMap = new Map < string , any > ( [
47
42
[
48
- '__GLOBAL ' ,
43
+ '__GLOBAL__ ' ,
49
44
{
50
45
data : {
51
- inlineClasses : new Set ( ) ,
52
- inlineDirectives : new Set ( ) ,
53
- inlineExpressions : new Set ( ) ,
54
- inlineIcons : new Set ( ) ,
55
- inlineAttributify : new Map ( ) ,
46
+ inline : {
47
+ utilities : new Set ( ) ,
48
+ attributifies : new Map ( ) ,
49
+ icons : new Set ( ) ,
50
+ } ,
51
+ styles : {
52
+ utilities : new Set ( ) ,
53
+ icons : new Set ( ) ,
54
+ } ,
56
55
} ,
57
56
updatedAt : Date . now ( ) ,
58
57
} ,
59
58
] ,
60
59
] )
61
60
62
61
function extractStyles ( ) : PreprocessorGroup {
63
- // add logic for global scan
62
+ // MARK: experiment global on entry
63
+ if ( generatorConfiguration . experimental ?. scan ) {
64
+ const filePaths = fg . sync ( [ 'src/**/*.svelte' ] , { } )
65
+ for ( const filepath of filePaths ) {
66
+ const content = readFileSync ( filepath ) . toString ( )
67
+ const ast = parse ( content , { filename : filepath } )
68
+ const hasGlobalInline = ast . css . attributes . some (
69
+ el => el . name == 'windi-inline-global'
70
+ )
71
+
72
+ const result = new FileHandler ( content ) . prepare ( ) . scan ( ) . getStyles ( )
73
+ const globalStyles = styleMap . get ( '__GLOBAL__' )
74
+ if ( globalStyles && hasGlobalInline ) {
75
+ styleMap . set ( '__GLOBAL__' , {
76
+ data : {
77
+ inline : {
78
+ utilities : new Set ( [
79
+ ...globalStyles . data . inline . utilities ,
80
+ ...result . data . inline . utilities ,
81
+ ] ) ,
82
+ attributifies : new Map ( [
83
+ ...globalStyles . data . inline . attributifies ,
84
+ ...result . data . inline . attributifies ,
85
+ ] ) ,
86
+ icons : new Map ( [
87
+ ...globalStyles . data . inline . icons ,
88
+ ...result . data . inline . icons ,
89
+ ] ) ,
90
+ } ,
91
+ } ,
92
+ updatedAt : Date . now ( ) ,
93
+ } )
94
+ styleMap . set ( filepath , {
95
+ data : {
96
+ inline : null ,
97
+ } ,
98
+ updatedAt : Date . now ( ) ,
99
+ } )
100
+ } else {
101
+ styleMap . set ( filepath , {
102
+ data : result ,
103
+ updatedAt : Date . now ( ) ,
104
+ } )
105
+ }
106
+ }
107
+ }
108
+
64
109
return {
65
110
async markup ( { content, filename } ) : Promise < Processed > {
66
111
if ( ! filename ) return { code : content }
@@ -70,8 +115,9 @@ function extractStyles(): PreprocessorGroup {
70
115
. prepare ( )
71
116
. scan ( )
72
117
. getStyles ( )
118
+
73
119
styleMap . set ( filename , {
74
- data : fileStyles ,
120
+ data : fileStyles . data ,
75
121
updatedAt : Date . now ( ) ,
76
122
} )
77
123
return {
@@ -87,11 +133,7 @@ function generateCSS(): PreprocessorGroup {
87
133
if ( ! filename ) return { code : content }
88
134
console . log ( 'generate: Style' , filename )
89
135
90
- const styleSet = new Set ( [
91
- ...( styleMap . get ( filename ) ?. data . inlineClasses || new Set ( ) ) ,
92
- ...( styleMap . get ( filename ) ?. data . inlineDirectives || new Set ( ) ) ,
93
- ...( styleMap . get ( filename ) ?. data . inlineExpressions || new Set ( ) ) ,
94
- ] )
136
+ const styleSet = styleMap . get ( filename ) ?. data . inline . utilities
95
137
const windiStyles = await windiGenerator . generate ( styleSet )
96
138
const windiStylesCSS = new MagicString ( windiStyles . css )
97
139
const windiStyleSheet = CSSParser ( windiStylesCSS . toString ( ) , {
0 commit comments