-
Notifications
You must be signed in to change notification settings - Fork 63
/
unplugin.ts
73 lines (63 loc) · 1.8 KB
/
unplugin.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
import { promises as fs } from 'node:fs'
import { createUnplugin } from 'unplugin'
import type { FilterPattern } from '@rollup/pluginutils'
import { createFilter } from '@rollup/pluginutils'
import MagicString from 'magic-string'
import type { UnimportOptions } from './types'
import { createUnimport } from './context'
export interface UnimportPluginOptions extends UnimportOptions {
include: FilterPattern
exclude: FilterPattern
dts: boolean | string
/**
* Enable implicit auto import.
* Generate global TypeScript definitions.
*
* @default true
*/
autoImport?: boolean
}
export const defaultIncludes = [/\.[jt]sx?$/, /\.vue$/, /\.vue\?vue/, /\.svelte$/]
export const defaultExcludes = [/[\\/]node_modules[\\/]/, /[\\/]\.git[\\/]/]
function toArray<T>(x: T | T[] | undefined | null): T[] {
return x == null ? [] : Array.isArray(x) ? x : [x]
}
export default createUnplugin<Partial<UnimportPluginOptions>>((options = {}) => {
const ctx = createUnimport(options)
const filter = createFilter(
toArray(options.include as string[] || []).length
? options.include
: defaultIncludes,
options.exclude || defaultExcludes,
)
const dts = options.dts === true
? 'unimport.d.ts'
: options.dts
const {
autoImport = true,
} = options
return {
name: 'unimport',
enforce: 'post',
transformInclude(id) {
return filter(id)
},
async transform(code, id) {
const s = new MagicString(code)
await ctx.injectImports(s, id, {
autoImport,
})
if (!s.hasChanged())
return
return {
code: s.toString(),
map: s.generateMap(),
}
},
async buildStart() {
await ctx.init()
if (dts)
return fs.writeFile(dts, await ctx.generateTypeDeclarations(), 'utf-8')
},
}
})