-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
index.ts
220 lines (195 loc) · 5.77 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { resolve } from 'path'
import type { Plugin, ResolvedConfig, ViteDevServer } from 'vite'
import _debug, { log } from 'debug'
import { UserOptions, WindiPluginUtils, createUtils, WindiPluginUtilsOptions } from '@windicss/plugin-utils'
import { createVirtualModuleLoader, MODULE_ID_VIRTUAL_PREFIX } from '../../shared/virtual-module'
import { createDevtoolsPlugin } from './devtools'
import { NAME } from './constants'
import { getCssModules, reloadChangedCssModules } from './modules'
const debug = {
hmr: _debug(`${NAME}:hmr`),
css: _debug(`${NAME}:transform:css`),
group: _debug(`${NAME}:transform:group`),
alias: _debug(`${NAME}:transform:alias`),
memory: _debug(`${NAME}:memory`),
}
function VitePluginWindicss(userOptions: UserOptions = {}, utilsOptions: WindiPluginUtilsOptions = {}): Plugin[] {
let utils: WindiPluginUtils
// let viteConfig: ResolvedConfig
let server: ViteDevServer | undefined
let viteConfig: ResolvedConfig
const plugins: Plugin[] = []
// transform alias
plugins.push({
name: `${NAME}:alias`,
enforce: 'pre',
configResolved(_config) {
viteConfig = _config
},
async transform(code, id) {
await utils.ensureInit()
if (!utils.isDetectTarget(id))
return
debug.alias(id)
return utils.transformAlias(code, !!viteConfig.build.sourcemap)
},
})
// Utilities grouping transform
if (userOptions.transformGroups !== false) {
plugins.push({
name: `${NAME}:groups`,
enforce: 'pre',
async transform(code, id) {
await utils.ensureInit()
if (!utils.isDetectTarget(id))
return
debug.group(id)
return utils.transformGroups(code, !!viteConfig.build.sourcemap)
},
})
}
// exposing api
plugins.push({
name: NAME,
get api() {
return utils
},
})
// CSS Entry via virtual module
plugins.push({
name: `${NAME}:entry`,
enforce: 'post',
configureServer(_server) {
server = _server
},
async configResolved(_config) {
// viteConfig = _config
utils = utilsOptions.utils ?? createUtils(userOptions, {
name: NAME,
root: _config.root,
onConfigurationError(e) {
if (_config.command === 'build') {
throw e
}
else {
console.error(`[${NAME}] Error on loading configurations`)
console.error(e)
}
},
...utilsOptions,
})
await utils.ensureInit()
},
...createVirtualModuleLoader({ get utils() { return utils } }),
})
let _cssReloadTask: any
function reloadCssModules(server: ViteDevServer) {
clearTimeout(_cssReloadTask)
_cssReloadTask = setTimeout(() => {
reloadChangedCssModules(server, utils)
}, 1)
}
// HMR
plugins.push({
name: `${NAME}:hmr`,
apply: 'serve',
enforce: 'pre',
async configureServer(_server) {
server = _server
await utils.ensureInit()
if (utils.configFilePath)
server.watcher.add(utils.configFilePath)
// NOTE: Track changes to the files so that they are re-scanned as needed.
// Added files are only detected if the user explicitly enables globbing.
const supportsGlobs = server.config.server.watch?.disableGlobbing === false
server.watcher.add(supportsGlobs ? utils.globs : await utils.getFiles())
},
async handleHotUpdate({ server, file, read }) {
// resolve normalized file path to system path
if (resolve(file) === utils.configFilePath) {
debug.hmr(`config file changed: ${file}`)
await utils.init()
setTimeout(() => {
log('configure file changed, reloading')
server.ws.send({ type: 'full-reload' })
}, 0)
return getCssModules(server)
}
if (!utils.isDetectTarget(file))
return
utils.extractFile(await read(), file, true)
.then((changed) => {
if (changed) {
debug.hmr(`refreshed by ${file}`)
reloadCssModules(server)
}
})
},
})
const { transformCSS: transformCSSOptions = true } = userOptions
const transformCSS = (code: string, id: string) => utils.transformCSS(code, id, {
onLayerUpdated() {
if (server)
reloadCssModules(server)
},
})
// CSS transform
if (transformCSSOptions === true) {
plugins.push({
name: `${NAME}:css`,
async transform(code, id) {
await utils.ensureInit()
if (!utils.isCssTransformTarget(id) || id.startsWith(MODULE_ID_VIRTUAL_PREFIX))
return
debug.css(id)
code = transformCSS(code, id)
if (viteConfig.build.sourcemap) {
return {
code: transformCSS(code, id),
map: { mappings: '' },
}
}
else {
return code
}
},
})
}
else if (typeof transformCSSOptions === 'string') {
plugins.push({
name: `${NAME}:css`,
enforce: transformCSSOptions,
transform(code, id) {
if (!utils.isCssTransformTarget(id) || id.startsWith(MODULE_ID_VIRTUAL_PREFIX))
return
debug.css(id, transformCSSOptions)
code = transformCSS(code, id)
if (viteConfig.build.sourcemap) {
return {
code: transformCSS(code, id),
map: { mappings: '' },
}
}
else {
return code
}
},
})
}
plugins.push({
name: `${NAME}:css:svelte`,
api: {
sveltePreprocess: {
style({ content, id }: { content: string; id: string}) {
return {
code: transformCSS(content, id),
}
},
},
},
})
plugins.push(...createDevtoolsPlugin({ get utils() { return utils } }))
return plugins
}
export * from '@windicss/plugin-utils'
export default VitePluginWindicss