-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathunplugin.ts
More file actions
47 lines (44 loc) · 1.4 KB
/
Copy pathunplugin.ts
File metadata and controls
47 lines (44 loc) · 1.4 KB
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
import type { UnpluginFactory } from 'unplugin'
import type { UserOptions } from '../types'
import remapping from '@jridgewell/remapping'
import { createUnplugin } from 'unplugin'
import { Context } from './context'
import { ifDirective, includeDirective, MessageDirective, theDefineDirective } from './directives'
export const unpluginFactory: UnpluginFactory<UserOptions | undefined> = (
options,
) => {
const ctx = new Context({ ...options, directives: [ifDirective, theDefineDirective, includeDirective, MessageDirective, ...options?.directives ?? []] })
return {
name: 'unplugin-preprocessor-directives',
enforce: 'pre',
transform: (code, id) => ctx.transform(code, id),
transformInclude(id) {
return ctx.filter(id)
},
vite: {
configResolved(config) {
ctx.env = {
...ctx.loadEnv(config.mode),
...config.env,
}
},
transform(code, id) {
if (ctx.filter(id)) {
const transformed = ctx.transformWithMap(code, id)
if (transformed) {
const map = remapping(
[this.getCombinedSourcemap() as any, transformed.map],
() => null,
) as any
return {
code: transformed.code,
map,
}
}
}
},
},
}
}
export const unplugin = /* #__PURE__ */ createUnplugin(unpluginFactory)
export default unplugin