-
-
Notifications
You must be signed in to change notification settings - Fork 780
/
index.ts
40 lines (36 loc) · 1.26 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
import type { Plugin, ResolvedConfig } from 'vite'
import { createFilter } from '@rollup/pluginutils'
import type { UnocssPluginContext } from '@unocss/core'
import { defaultExclude } from '../../integration'
import { transformSvelteSFC } from './transform'
export * from './transform'
export function SvelteScopedPlugin({ ready, uno }: UnocssPluginContext): Plugin {
let viteConfig: ResolvedConfig
let filter = createFilter([/\.svelte$/, /\.svelte\.md$/, /\.svx$/], defaultExclude)
return {
name: 'unocss:svelte-scoped',
enforce: 'pre',
async configResolved(_viteConfig) {
viteConfig = _viteConfig
const { config } = await ready
filter = createFilter(
config.include || [/\.svelte$/, /\.svelte\.md$/, /\.svx$/],
config.exclude || defaultExclude,
)
},
transform(code, id) {
if (!filter(id))
return
return transformSvelteSFC(code, id, uno, { combine: viteConfig.command === 'build' })
},
handleHotUpdate(ctx) {
const read = ctx.read
if (filter(ctx.file)) {
ctx.read = async () => {
const code = await read()
return (await transformSvelteSFC(code, ctx.file, uno, { combine: viteConfig.command === 'build' }))?.code || code
}
}
},
}
}