/
context.ts
279 lines (242 loc) · 8.6 KB
/
context.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import { detectSyntax, findStaticImports, parseStaticImport } from 'mlly'
import MagicString from 'magic-string'
import type { Addon, Import, ImportInjectionResult, InjectImportsOptions, Thenable, TypeDeclarationOptions, UnimportContext, UnimportMeta, UnimportOptions } from './types'
import { excludeRE, stripCommentsAndStrings, separatorRE, importAsRE, toTypeDeclarationFile, addImportToCode, dedupeImports, toExports, normalizeImports, matchRE, getMagicString } from './utils'
import { resolveBuiltinPresets } from './preset'
import { vueTemplateAddon } from './addons'
export type Unimport = ReturnType<typeof createUnimport>
export function createUnimport (opts: Partial<UnimportOptions>) {
// Cache for combine imports
let _combinedImports: Import[] | undefined
const _map = new Map()
const addons: Addon[] = []
if (Array.isArray(opts.addons)) {
addons.push(...opts.addons)
} else if (opts.addons?.vueTemplate) {
addons.push(vueTemplateAddon())
}
opts.addons = addons
opts.commentsDisable = opts.commentsDisable ?? ['@unimport-disable', '@imports-disable']
opts.commentsDebug = opts.commentsDebug ?? ['@unimport-debug', '@imports-debug']
let metadata: UnimportMeta | undefined
if (opts.collectMeta) {
metadata = {
injectionUsage: {}
}
}
const ctx: UnimportContext = {
staticImports: [...(opts.imports || [])].filter(Boolean),
dynamicImports: [],
async getImports () {
await resolvePromise
return updateImports()
},
async getImportMap () {
await ctx.getImports()
return _map
},
invalidate () {
_combinedImports = undefined
},
getMetadata () {
return metadata
},
resolveId: (id, parentId) => opts.resolveId?.(id, parentId),
addons,
options: opts
}
// Resolve presets
const resolvePromise = resolveBuiltinPresets(opts.presets || [])
.then((r) => {
ctx.staticImports.unshift(...r)
_combinedImports = undefined
updateImports()
})
function updateImports () {
if (!_combinedImports) {
// Combine static and dynamic imports
// eslint-disable-next-line no-console
const imports = normalizeImports(dedupeImports([...ctx.staticImports, ...ctx.dynamicImports], opts.warn || console.warn))
.filter(i => !i.disabled)
// Create map
_map.clear()
for (const _import of imports) {
_map.set(_import.as ?? _import.name, _import)
}
_combinedImports = imports
}
return _combinedImports
}
async function modifyDynamicImports (fn: (imports: Import[]) => Thenable<void | Import[]>) {
const result = await fn(ctx.dynamicImports)
if (Array.isArray(result)) {
ctx.dynamicImports = result
}
ctx.invalidate()
}
function clearDynamicImports () {
ctx.dynamicImports.length = 0
ctx.invalidate()
}
async function generateTypeDeclarations (options?: TypeDeclarationOptions) {
const opts: TypeDeclarationOptions = {
resolvePath: i => i.from.replace(/\.ts$/, ''),
...options
}
let dts = toTypeDeclarationFile(await ctx.getImports(), opts)
for (const addon of ctx.addons) {
dts = await addon.declaration?.call(ctx, dts, opts) ?? dts
}
return dts
}
return {
clearDynamicImports,
modifyDynamicImports,
getImports: () => ctx.getImports(),
detectImports: (code: string | MagicString) => detectImports(code, ctx),
injectImports: async (code: string | MagicString, id?: string, options?: InjectImportsOptions) => {
const result = await injectImports(code, id, ctx, options)
// Collect metadata
if (metadata) {
result.imports.forEach((i) => {
metadata!.injectionUsage[i.name] = metadata!.injectionUsage[i.name] || { import: i, count: 0, moduleIds: [] }
metadata!.injectionUsage[i.name].count++
if (id && !metadata!.injectionUsage[i.name].moduleIds.includes(id)) {
metadata!.injectionUsage[i.name].moduleIds.push(id)
}
})
}
return result
},
toExports: async (filepath?: string) => toExports(await ctx.getImports(), filepath),
parseVirtualImports: (code: string) => parseVirtualImports(code, ctx),
generateTypeDeclarations,
getMetadata: () => ctx.getMetadata()
}
}
function parseVirtualImports (code: string, ctx: UnimportContext) {
if (ctx.options.virtualImports?.length) {
return findStaticImports(code)
.filter(i => ctx.options.virtualImports!.includes(i.specifier))
.map(i => parseStaticImport(i))
}
return []
}
async function detectImports (code: string | MagicString, ctx: UnimportContext, options?: InjectImportsOptions) {
const s = getMagicString(code)
// Strip comments so we don't match on them
const original = s.original
const strippedCode = stripCommentsAndStrings(original)
const syntax = detectSyntax(strippedCode)
const isCJSContext = syntax.hasCJS && !syntax.hasESM
let matchedImports: Import[] = []
const map = await ctx.getImportMap()
// Auto import, search for unreferenced usages
if (options?.autoImport !== false) {
// Find all possible injection
const identifiers = new Set(
Array.from(strippedCode.matchAll(matchRE))
.map((i) => {
// Remove dot access, but keep destructuring
if (i[1] === '.') {
return ''
}
// Remove property, but keep `case x:` and `? x :`
const end = strippedCode[i.index! + i[0].length]
if (end === ':' && !['?', 'case'].includes(i[1].trim())) {
return ''
}
return i[2]
})
.filter(Boolean)
)
// Remove those already defined
for (const regex of excludeRE) {
for (const match of strippedCode.matchAll(regex)) {
const segments = [...match[1]?.split(separatorRE) || [], ...match[2]?.split(separatorRE) || []]
for (const segment of segments) {
const identifier = segment.replace(importAsRE, '').trim()
identifiers.delete(identifier)
}
}
}
matchedImports = Array.from(identifiers)
.map(name => map.get(name))
.filter(i => i && !i.disabled) as Import[]
for (const addon of ctx.addons) {
matchedImports = await addon.matchImports?.call(ctx, identifiers, matchedImports) || matchedImports
}
}
// Transform virtual imports like `import { foo } from '#imports'`
if (options?.transformVirtualImports !== false && options?.transformVirtualImoports !== false && ctx.options.virtualImports?.length) {
const virtualImports = parseVirtualImports(original, ctx)
virtualImports.forEach((i) => {
s.remove(i.start, i.end)
Object.entries(i.namedImports || {})
.forEach(([name, as]) => {
const original = map.get(name)
if (!original) {
throw new Error(`[unimport] failed to find "${name}" imported from "${i.specifier}"`)
}
matchedImports.push({
from: original.from,
name: original.name,
as
})
})
})
}
return {
s,
strippedCode,
isCJSContext,
matchedImports
}
}
async function injectImports (
code: string | MagicString,
id: string | undefined,
ctx: UnimportContext,
options?: InjectImportsOptions
): Promise<ImportInjectionResult> {
const s = getMagicString(code)
if (ctx.options.commentsDisable?.some(c => s.original.includes(c))) {
return {
s,
get code () { return s.toString() },
imports: []
}
}
for (const addon of ctx.addons) {
await addon.transform?.call(ctx, s, id)
}
const { isCJSContext, matchedImports } = await detectImports(s, ctx, options)
const imports = await resolveImports(ctx, matchedImports, id)
if (ctx.options.commentsDebug?.some(c => s.original.includes(c))) {
// eslint-disable-next-line no-console
const log = ctx.options.debugLog || console.log
log(`[unimport] ${imports.length} imports detected in "${id}"${imports.length ? ': ' + imports.map(i => i.name).join(', ') : ''}`)
}
return {
...addImportToCode(s, imports, isCJSContext, options?.mergeExisting),
imports
}
}
async function resolveImports (ctx: UnimportContext, imports: Import[], id: string | undefined) {
const resolveCache = new Map<string, string>()
const _imports = await Promise.all(imports.map(async (i) => {
if (!resolveCache.has(i.from)) {
resolveCache.set(i.from, await ctx.resolveId(i.from, id) || i.from)
}
const from = resolveCache.get(i.from)!
// reference to self
if (i.from === id || !from || from === '.' || from === id) {
return
}
return <Import>{
...i,
from
}
}))
return _imports.filter(Boolean) as Import[]
}