-
-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy pathctx.ts
301 lines (261 loc) · 9.35 KB
/
ctx.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import { dirname, isAbsolute, join, relative, resolve } from 'node:path'
import { existsSync, promises as fs } from 'node:fs'
import process from 'node:process'
import { slash, throttle, toArray } from '@antfu/utils'
import { createFilter } from '@rollup/pluginutils'
import { isPackageExists } from 'local-pkg'
import type { Import, InlinePreset } from 'unimport'
import { createUnimport, resolvePreset, scanExports } from 'unimport'
import fg from 'fast-glob'
// @ts-expect-error types
import { vueTemplateAddon } from 'unimport/addons'
import MagicString from 'magic-string'
import { presets } from '../presets'
import type { ESLintGlobalsPropValue, ESLintrc, ImportExtended, Options } from '../types'
import { generateESLintConfigs } from './eslintrc'
import { resolversAddon } from './resolvers'
function resolveGlobsExclude(root: string, glob: string) {
const excludeReg = /^!/
return `${excludeReg.test(glob) ? '!' : ''}${resolve(root, glob.replace(excludeReg, ''))}`
}
async function scanDirExports(dirs: string[], root: string) {
const result = await fg(dirs, {
absolute: true,
cwd: root,
onlyFiles: true,
followSymbolicLinks: true,
})
const files = Array.from(new Set(result.flat())).map(slash)
return (await Promise.all(files.map(i => scanExports(i, false)))).flat()
}
export function createContext(options: Options = {}, root = process.cwd()) {
root = slash(root)
const {
dts: preferDTS = isPackageExists('typescript'),
} = options
const dirs = options.dirs?.concat(options.dirs.map(dir => join(dir, '*.{tsx,jsx,ts,js,mjs,cjs,mts,cts}')))
.map(dir => slash(resolveGlobsExclude(root, dir)))
const eslintrc: ESLintrc = options.eslintrc || {}
eslintrc.enabled = eslintrc.enabled === undefined ? false : eslintrc.enabled
eslintrc.filepath = eslintrc.filepath || './.eslintrc-auto-import.json'
eslintrc.globalsPropValue = eslintrc.globalsPropValue === undefined ? true : eslintrc.globalsPropValue
const resolvers = options.resolvers ? [options.resolvers].flat(2) : []
// When "options.injectAtEnd" is undefined or true, it's true.
const injectAtEnd = options.injectAtEnd !== false
const unimport = createUnimport({
imports: [],
presets: options.packagePresets?.map(p => typeof p === 'string' ? { package: p } : p) ?? [],
injectAtEnd,
parser: options.parser,
addons: [
...(options.vueTemplate ? [vueTemplateAddon()] : []),
resolversAddon(resolvers),
{
declaration(dts) {
return `${`
/* eslint-disable */
/* prettier-ignore */
// @ts-nocheck
// noinspection JSUnusedGlobalSymbols
// Generated by unplugin-auto-import
${dts}`.trim()}\n`
},
},
],
})
const importsPromise = flattenImports(options.imports)
.then((imports) => {
if (!imports.length && !resolvers.length && !dirs?.length)
console.warn('[auto-import] plugin installed but no imports has defined, see https://github.com/antfu/unplugin-auto-import#configurations for configurations')
const compare = (left: string | undefined, right: NonNullable<(Options['ignore'] | Options['ignoreDts'])>[number]) => {
return right instanceof RegExp
? right.test(left!)
: right === left
}
options.ignore?.forEach((name) => {
const i = imports.find(i => compare(i.as, name))
if (i)
i.disabled = true
})
options.ignoreDts?.forEach((name) => {
const i = imports.find(i => compare(i.as, name))
if (i)
i.dtsDisabled = true
})
return unimport.getInternalContext().replaceImports(imports)
})
const filter = createFilter(
options.include || [/\.[jt]sx?$/, /\.vue$/, /\.vue\?vue/, /\.svelte$/],
options.exclude || [/[\\/]node_modules[\\/]/, /[\\/]\.git[\\/]/],
)
const dts = preferDTS === false
? false
: preferDTS === true
? resolve(root, 'auto-imports.d.ts')
: resolve(root, preferDTS)
const multilineCommentsRE = /\/\*.*?\*\//gms
const singlelineCommentsRE = /\/\/.*$/gm
const dtsReg = /declare\s+global\s*{(.*?)[\n\r]}/s
const componentCustomPropertiesReg = /interface\s+ComponentCustomProperties\s*{(.*?)[\n\r]}/gs
function parseDTS(dts: string) {
dts = dts
.replace(multilineCommentsRE, '')
.replace(singlelineCommentsRE, '')
const code = dts.match(dtsReg)?.[0]
if (!code)
return
return Object.fromEntries(Array.from(code.matchAll(/['"]?(const\s*[^\s'"]+)['"]?\s*:\s*(.+?)[,;\r\n]/g)).map(i => [i[1], i[2]]))
}
async function generateDTS(file: string) {
await importsPromise
const dir = dirname(file)
const originalContent = existsSync(file) ? await fs.readFile(file, 'utf-8') : ''
const originalDTS = parseDTS(originalContent)
let currentContent = await unimport.generateTypeDeclarations({
resolvePath: (i) => {
if (i.from.startsWith('.') || isAbsolute(i.from)) {
const related = slash(relative(dir, i.from).replace(/\.ts(x)?$/, ''))
return !related.startsWith('.')
? `./${related}`
: related
}
return i.from
},
})
const currentDTS = parseDTS(currentContent)!
if (options.vueTemplate) {
currentContent = currentContent.replace(
componentCustomPropertiesReg,
$1 => `interface GlobalComponents {}\n ${$1}`,
)
}
if (originalDTS) {
Object.keys(currentDTS).forEach((key) => {
originalDTS[key] = currentDTS[key]
})
const dtsList = Object.keys(originalDTS).sort().map(k => ` ${k}: ${originalDTS[k]}`)
return currentContent.replace(dtsReg, () => `declare global {\n${dtsList.join('\n')}\n}`)
}
return currentContent
}
async function parseESLint() {
const configStr = existsSync(eslintrc.filepath!) ? await fs.readFile(eslintrc.filepath!, 'utf-8') : ''
const config = JSON.parse(configStr || '{ "globals": {} }')
return config.globals as Record<string, ESLintGlobalsPropValue>
}
async function generateESLint() {
return generateESLintConfigs(await unimport.getImports(), eslintrc, await parseESLint())
}
const writeConfigFilesThrottled = throttle(500, writeConfigFiles, { noLeading: false })
async function writeFile(filePath: string, content = '') {
await fs.mkdir(dirname(filePath), { recursive: true })
return await fs.writeFile(filePath, content, 'utf-8')
}
let lastDTS: string | undefined
let lastESLint: string | undefined
async function writeConfigFiles() {
const promises: any[] = []
if (dts) {
promises.push(
generateDTS(dts).then((content) => {
if (content !== lastDTS) {
lastDTS = content
return writeFile(dts, content)
}
}),
)
}
if (eslintrc.enabled && eslintrc.filepath) {
promises.push(
generateESLint().then((content) => {
content = `${content}\n`
if (content.trim() !== lastESLint?.trim()) {
lastESLint = content
return writeFile(eslintrc.filepath!, content)
}
}),
)
}
return Promise.all(promises)
}
async function scanDirs() {
if (dirs?.length) {
await unimport.modifyDynamicImports(async (imports) => {
const exports_ = await scanDirExports(dirs, root) as ImportExtended[]
exports_.forEach(i => i.__source = 'dir')
return modifyDefaultExportsAlias([
...imports.filter((i: ImportExtended) => i.__source !== 'dir'),
...exports_,
], options)
})
}
writeConfigFilesThrottled()
}
async function transform(code: string, id: string) {
await importsPromise
const s = new MagicString(code)
await unimport.injectImports(s, id)
if (!s.hasChanged())
return
writeConfigFilesThrottled()
return {
code: s.toString(),
map: s.generateMap({ source: id, includeContent: true, hires: true }),
}
}
return {
root,
dirs,
filter,
scanDirs,
writeConfigFiles,
writeConfigFilesThrottled,
transform,
generateDTS,
generateESLint,
}
}
export async function flattenImports(map: Options['imports']): Promise<Import[]> {
const promises = await Promise.all(toArray(map)
.map(async (definition) => {
if (typeof definition === 'string') {
if (!presets[definition])
throw new Error(`[auto-import] preset ${definition} not found`)
const preset = presets[definition]
definition = typeof preset === 'function' ? preset() : preset
}
if ('from' in definition && 'imports' in definition) {
return await resolvePreset(definition as InlinePreset)
}
else {
const resolved: Import[] = []
for (const mod of Object.keys(definition)) {
for (const id of definition[mod]) {
const meta = {
from: mod,
} as Import
if (Array.isArray(id)) {
meta.name = id[0]
meta.as = id[1]
}
else {
meta.name = id
meta.as = id
}
resolved.push(meta)
}
}
return resolved
}
}))
return promises.flat()
}
function modifyDefaultExportsAlias(imports: ImportExtended[], options: Options): Import[] {
if (options.defaultExportByFilename) {
imports.forEach((i) => {
if (i.name === 'default')
i.as = i.from.split('/').pop()?.split('.')?.shift() ?? i.as
})
}
return imports as Import[]
}