Skip to content

Commit 0ecba21

Browse files
refactor: ♻️ update utility scan logic
1 parent 5164257 commit 0ecba21

File tree

2 files changed

+183
-184
lines changed

2 files changed

+183
-184
lines changed

src/index.ts

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import windicssPreset from '@unocss/preset-wind'
66
import { parse as CSSParser, walk as CSSWalker } from 'css-tree'
77
import MagicString from 'magic-string'
88
import { bgRed, white } from 'nanocolors'
9-
import { preprocess } from 'svelte/compiler'
9+
import { parse, preprocess } from 'svelte/compiler'
1010
import type {
1111
PreprocessorGroup,
1212
Processed,
1313
} from 'svelte/types/compiler/preprocess'
1414
import { loadConfig } from 'unconfig'
1515
import { FileHandler, SetObject } from './utils'
16+
import fg from 'fast-glob'
17+
import { readFileSync } from 'fs'
1618

1719
export interface BaseConfig {
1820
silent?: boolean
@@ -36,31 +38,74 @@ let windiConfiguration: any
3638
let windiGenerator: UnoGenerator
3739
let iconGenerator: UnoGenerator
3840

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>([
4742
[
48-
'__GLOBAL',
43+
'__GLOBAL__',
4944
{
5045
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+
},
5655
},
5756
updatedAt: Date.now(),
5857
},
5958
],
6059
])
6160

6261
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+
64109
return {
65110
async markup({ content, filename }): Promise<Processed> {
66111
if (!filename) return { code: content }
@@ -70,8 +115,9 @@ function extractStyles(): PreprocessorGroup {
70115
.prepare()
71116
.scan()
72117
.getStyles()
118+
73119
styleMap.set(filename, {
74-
data: fileStyles,
120+
data: fileStyles.data,
75121
updatedAt: Date.now(),
76122
})
77123
return {
@@ -87,11 +133,7 @@ function generateCSS(): PreprocessorGroup {
87133
if (!filename) return { code: content }
88134
console.log('generate: Style', filename)
89135

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
95137
const windiStyles = await windiGenerator.generate(styleSet)
96138
const windiStylesCSS = new MagicString(windiStyles.css)
97139
const windiStyleSheet = CSSParser(windiStylesCSS.toString(), {

0 commit comments

Comments
 (0)