-
-
Notifications
You must be signed in to change notification settings - Fork 778
/
index.ts
205 lines (181 loc) · 6.11 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { readFile, stat } from 'node:fs/promises'
import type { UnoGenerator } from '@unocss/core'
import fg from 'fast-glob'
import type { Result, Root } from 'postcss'
import postcss from 'postcss'
import { createGenerator, warnOnce } from '@unocss/core'
import { loadConfig } from '@unocss/config'
import { defaultIncludeGlobs } from '../../shared-integration/src/defaults'
import { parseApply } from './apply'
import { parseTheme, themeFnRE } from './theme'
import { parseScreen } from './screen'
import type { UnoPostcssPluginOptions } from './types'
export * from './types'
function unocss(options: UnoPostcssPluginOptions = {}) {
warnOnce(
'`@unocss/postcss` package is in an experimental state right now. '
+ 'It doesn\'t follow semver, and may introduce breaking changes in patch versions.',
)
const {
cwd = process.cwd(),
content,
configOrPath,
} = options
const directiveMap = Object.assign({
apply: 'apply',
theme: 'theme',
screen: 'screen',
unocss: 'unocss',
}, options.directiveMap || {})
const fileMap = new Map()
const fileClassMap = new Map()
const classes = new Set<string>()
const targetCache = new Set<string>()
const config = loadConfig(cwd, configOrPath)
let uno: UnoGenerator
let promises: Promise<void>[] = []
let last_config_mtime = 0
const targetRE = new RegExp(Object.values(directiveMap).join('|'))
return {
postcssPlugin: directiveMap.unocss,
plugins: [
async function (root: Root, result: Result) {
if (!result.opts.from?.split('?')[0].endsWith('.css'))
return
let isTarget = false
if (targetRE.test(root.toString())) {
if (!targetCache.has(result.opts.from)) {
root.walkAtRules((rule) => {
if (
rule.name === directiveMap.unocss
|| rule.name === directiveMap.apply
|| rule.name === directiveMap.theme
|| rule.name === directiveMap.screen
)
isTarget = true
})
if (!isTarget) {
const themeFn = themeFnRE(directiveMap.theme)
root.walkDecls((decl) => {
if (themeFn.test(decl.value))
isTarget = true
})
}
}
else {
isTarget = true
}
}
else if (targetCache.has(result.opts.from)) {
targetCache.delete(result.opts.from)
}
if (!isTarget)
return
else
targetCache.add(result.opts.from)
try {
const cfg = await config
if (!uno) {
uno = createGenerator(cfg.config)
}
else if (cfg.sources.length) {
const config_mtime = (await stat(cfg.sources[0])).mtimeMs
if (config_mtime > last_config_mtime) {
uno = createGenerator((await loadConfig(cwd, configOrPath)).config)
last_config_mtime = config_mtime
}
}
}
catch (error: any) {
throw new Error (`UnoCSS config not found: ${error.message}`)
}
const globs = content?.filter(v => typeof v === 'string') as string[] ?? defaultIncludeGlobs
const rawContent = content?.filter(v => typeof v === 'object') as {
raw: string
extension: string
}[] ?? []
const entries = await fg(globs, {
cwd,
dot: true,
absolute: true,
ignore: ['**/{.git,node_modules}/**'],
stats: true,
}) as unknown as { path: string; mtimeMs: number }[]
result.messages.push({
type: 'dependency',
plugin: directiveMap.unocss,
file: result.opts.from,
parent: result.opts.from,
})
await parseApply(root, uno, directiveMap.apply)
await parseTheme(root, uno, directiveMap.theme)
await parseScreen(root, uno, directiveMap.screen)
promises.push(
...rawContent.map(async (v) => {
const { matched } = await uno.generate(v.raw, {
id: `unocss.${v.extension}`,
})
for (const candidate of matched)
classes.add(candidate)
}),
...entries.map(async ({ path: file, mtimeMs }) => {
result.messages.push({
type: 'dependency',
plugin: directiveMap.unocss,
file,
parent: result.opts.from,
})
if (fileMap.has(file) && mtimeMs <= fileMap.get(file))
return
else
fileMap.set(file, mtimeMs)
const content = await readFile(file, 'utf8')
const { matched } = await uno.generate(content, {
id: file,
})
fileClassMap.set(file, matched)
}),
)
await Promise.all(promises)
promises = []
for (const set of fileClassMap.values()) {
for (const candidate of set)
classes.add(candidate)
}
const c = await uno.generate(classes)
classes.clear()
const excludes: string[] = []
root.walkAtRules(directiveMap.unocss, (rule) => {
if (rule.params) {
const source = rule.source
const layers = rule.params.split(',').map(v => v.trim())
const css = postcss.parse(
layers
.map(i => (i === 'all' ? c.getLayers() : c.getLayer(i)) || '')
.filter(Boolean)
.join('\n'),
)
css.walkDecls((declaration) => {
declaration.source = source
})
rule.replaceWith(css)
excludes.push(rule.params)
}
})
root.walkAtRules(directiveMap.unocss, (rule) => {
if (!rule.params) {
const source = rule.source
const css = postcss.parse(c.getLayers(undefined, excludes) || '')
css.walkDecls((declaration) => {
declaration.source = source
})
rule.replaceWith(css)
}
})
},
],
}
}
unocss.postcss = true
unocss.default = unocss
export default unocss