-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathindex.ts
470 lines (418 loc) · 14.3 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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
import _debug from 'debug'
import * as fs from 'fs'
import globRex from 'globrex'
import { resolve } from 'path'
import type {
TSConfckParseNativeResult,
TSConfckParseOptions,
TSConfckParseResult,
} from 'tsconfck'
import type { CompilerOptions } from 'typescript'
import { inspect } from 'util'
import { normalizePath, Plugin, searchForWorkspaceRoot } from 'vite'
import { resolvePathMappings } from './mappings'
import { basename, dirname, isAbsolute, join, relative } from './path'
import { PluginOptions } from './types'
import { debug, debugResolve } from './debug'
const notApplicable = [undefined, false] as const
const notFound = [undefined, true] as const
type ViteResolve = (id: string, importer: string) => Promise<string | undefined>
type Resolver = (
viteResolve: ViteResolve,
id: string,
importer: string
) => Promise<readonly [resolved: string | undefined, matched: boolean]>
export type { PluginOptions }
export default (opts: PluginOptions = {}): Plugin => {
let resolversByDir: Record<string, Resolver[]>
return {
name: 'vite-tsconfig-paths',
enforce: 'pre',
async configResolved(config) {
let projectRoot = config.root
let workspaceRoot!: string
let { root } = opts
if (root) {
root = resolve(projectRoot, root)
} else {
workspaceRoot = searchForWorkspaceRoot(projectRoot)
}
debug('options.root ==', root)
debug('project root ==', projectRoot)
debug('workspace root ==', workspaceRoot)
// The "root" option overrides both of these.
if (root) {
projectRoot = root
workspaceRoot = root
}
const tsconfck = await import('tsconfck')
const projects = opts.projects
? opts.projects.map((file) => {
if (!file.endsWith('.json')) {
file = join(file, 'tsconfig.json')
}
return resolve(projectRoot, file)
})
: await tsconfck.findAll(workspaceRoot, {
configNames: opts.configNames || ['tsconfig.json', 'jsconfig.json'],
skip(dir) {
if (dir === '.git' || dir === 'node_modules') {
return true
}
if (typeof opts.skip === 'function') {
return opts.skip(dir)
}
return false
},
})
debug('projects:', projects)
let hasTypeScriptDep = false
if (opts.parseNative) {
try {
const pkgJson = fs.readFileSync(
join(workspaceRoot, 'package.json'),
'utf8'
)
const pkg = JSON.parse(pkgJson)
const deps = { ...pkg.dependencies, ...pkg.devDependencies }
hasTypeScriptDep = 'typescript' in deps
} catch (e: any) {
if (e.code != 'ENOENT') {
throw e
}
}
}
let firstError: any
const parseOptions: TSConfckParseOptions = {
cache: new tsconfck.TSConfckCache(),
}
const parsedProjects = new Set(
await Promise.all(
projects.map((tsconfigFile) => {
if (tsconfigFile === null) {
debug('tsconfig file not found:', tsconfigFile)
return null
}
return (
hasTypeScriptDep
? tsconfck.parseNative(tsconfigFile, parseOptions)
: tsconfck.parse(tsconfigFile, parseOptions)
).catch((error) => {
if (opts.ignoreConfigErrors) {
debug('tsconfig file caused a parsing error:', tsconfigFile)
} else {
config.logger.error(
'[tsconfig-paths] An error occurred while parsing "' +
tsconfigFile +
'". See below for details.' +
(firstError
? ''
: ' To disable this message, set the `ignoreConfigErrors` option to true.'),
{ error }
)
if (config.logger.hasErrorLogged(error)) {
console.error(error)
}
firstError = error
}
return null
})
})
)
)
resolversByDir = {}
parsedProjects.forEach((project) => {
if (!project) {
return
}
// Don't create a resolver for projects with a references array.
// Instead, create a resolver for each project in that array.
if (project.referenced) {
project.referenced.forEach((projectRef) => {
parsedProjects.add(projectRef)
})
// Reinsert the parent project so it's tried last. This is
// important because project references can be used to
// override the parent project.
parsedProjects.delete(project)
parsedProjects.add(project)
project.referenced = undefined
} else {
const resolver = createResolver(project)
if (resolver) {
const projectDir = normalizePath(dirname(project.tsconfigFile))
const resolvers = (resolversByDir[projectDir] ||= [])
resolvers.push(resolver)
}
}
})
},
async resolveId(id, importer, options) {
if (debugResolve.enabled) {
debugResolve('resolving:', { id, importer })
}
if (!importer) {
debugResolve('importer is empty or undefined. skipping...')
return
}
if (relativeImportRE.test(id)) {
debugResolve('id is a relative import. skipping...')
return
}
if (isAbsolute(id)) {
debugResolve('id is an absolute path. skipping...')
return
}
if (id.includes('\0')) {
debugResolve('id is a virtual module. skipping...')
return
}
// For Vite 4 and under, skipSelf needs to be set.
const resolveOptions = { ...options, skipSelf: true }
const viteResolve: ViteResolve = async (id, importer) =>
(await this.resolve(id, importer, resolveOptions))?.id
let prevProjectDir: string | undefined
let projectDir = normalizePath(dirname(importer))
// Find the nearest directory with a matching tsconfig file.
loop: while (projectDir && projectDir != prevProjectDir) {
const resolvers = resolversByDir[projectDir]
if (resolvers) {
for (const resolve of resolvers) {
const [resolved, matched] = await resolve(viteResolve, id, importer)
if (resolved) {
return resolved
}
if (matched) {
// Once a matching resolver is found, stop looking.
break loop
}
}
}
prevProjectDir = projectDir
projectDir = dirname(prevProjectDir)
}
},
}
type TsConfig = {
files?: string[]
include?: string[]
exclude?: string[]
compilerOptions?: CompilerOptions
}
function resolvePathsRootDir(
project: TSConfckParseResult | TSConfckParseNativeResult
): string {
if ('result' in project) {
return (
project.result.options?.pathsBasePath ?? dirname(project.tsconfigFile)
)
}
const baseUrl = (project.tsconfig as TsConfig).compilerOptions?.baseUrl
if (baseUrl) {
return baseUrl
}
const projectWithPaths = project.extended?.find(
(p) => (p.tsconfig as TsConfig).compilerOptions?.paths
)
return dirname((projectWithPaths ?? project).tsconfigFile)
}
function createResolver(
project: TSConfckParseResult | TSConfckParseNativeResult
): Resolver | null {
const configPath = normalizePath(project.tsconfigFile)
const config = project.tsconfig as TsConfig
debug('config loaded:', inspect({ configPath, config }, false, 10, true))
// Sometimes a tsconfig is not meant to be used for path resolution,
// but rather for pointing to other tsconfig files and possibly
// being extended by them. This is represented by an explicitly
// empty "files" array and a missing/empty "include" array.
if (config.files?.length == 0 && !config.include?.length) {
debug(
`[!] skipping "${configPath}" as no files can be matched since "files" is empty and "include" is missing or empty`
)
return null
}
const options = config.compilerOptions || {}
const { baseUrl, paths } = options
if (!baseUrl && !paths) {
debug(`[!] missing baseUrl and paths: "${configPath}"`)
return null
}
type InternalResolver = (
viteResolve: ViteResolve,
id: string,
importer: string
) => Promise<string | undefined>
const resolveWithBaseUrl: InternalResolver | undefined = baseUrl
? (viteResolve, id, importer) => {
const absoluteId = join(baseUrl, id)
debugResolve('trying with baseUrl:', absoluteId)
return viteResolve(absoluteId, importer)
}
: undefined
let resolveId: InternalResolver
if (paths) {
const pathsRootDir = resolvePathsRootDir(project)
const pathMappings = resolvePathMappings(paths, pathsRootDir)
const resolveWithPaths: InternalResolver = async (
viteResolve,
id,
importer
) => {
for (const mapping of pathMappings) {
const match = id.match(mapping.pattern)
if (!match) {
continue
}
for (let pathTemplate of mapping.paths) {
let starCount = 0
const mappedId = pathTemplate.replace(/\*/g, () => {
// There may exist more globs in the path template than in
// the match pattern. In that case, we reuse the final
// glob match.
const matchIndex = Math.min(++starCount, match.length - 1)
return match[matchIndex]
})
debugResolve('found match, trying to resolve:', mappedId)
const resolved = await viteResolve(mappedId, importer)
if (resolved) {
return resolved
}
}
}
}
if (resolveWithBaseUrl) {
resolveId = (viteResolve, id, importer) =>
resolveWithPaths(viteResolve, id, importer).then((resolved) => {
return resolved ?? resolveWithBaseUrl(viteResolve, id, importer)
})
} else {
resolveId = resolveWithPaths
}
} else {
resolveId = resolveWithBaseUrl!
}
const configDir = dirname(configPath)
// When `tsconfck.parseNative` is used, the outDir is absolute,
// which is not what `getIncluder` expects.
let { outDir } = options
if (outDir && isAbsolute(outDir)) {
outDir = relative(configDir, outDir)
}
const isIncludedRelative = getIncluder(
config.include?.map((p) => ensureRelative(configDir, p)),
config.exclude?.map((p) => ensureRelative(configDir, p)),
outDir
)
const importerExtRE = opts.loose
? /./
: options.allowJs || basename(configPath).startsWith('jsconfig.')
? jsLikeRE
: /\.[mc]?tsx?$/
const resolutionCache = new Map<string, string>()
return async (viteResolve, id, importer) => {
// Ideally, Vite would normalize the importer path for us.
importer = normalizePath(importer)
// Remove query and hash parameters from the importer path.
const importerFile = importer.replace(/[#?].+$/, '')
// Ignore importers with unsupported extensions.
if (!importerExtRE.test(importerFile)) {
debugResolve('importer has unsupported extension. skipping...')
return notApplicable
}
// Respect the include/exclude properties.
const relativeImporterFile = relative(configDir, importerFile)
if (!isIncludedRelative(relativeImporterFile)) {
debugResolve('importer is not included. skipping...')
return notApplicable
}
// Find and remove Vite's suffix (e.g. "?url") if present.
// If the path is resolved, the suffix will be added back.
const suffix = /\?.+$/.exec(id)?.[0]
if (suffix) {
id = id.slice(0, -suffix.length)
}
let resolvedId = resolutionCache.get(id)
if (!resolvedId) {
resolvedId = await resolveId(viteResolve, id, importer)
if (!resolvedId) {
return notFound
}
resolutionCache.set(id, resolvedId)
if (debugResolve.enabled) {
debugResolve('resolved without error:', {
id,
importer,
resolvedId,
configPath,
})
}
}
// Restore the suffix if one was removed earlier.
if (suffix) {
resolvedId += suffix
}
return [resolvedId, true]
}
}
}
const jsLikeRE = /\.(vue|svelte|mdx|[mc]?[jt]sx?)$/
const relativeImportRE = /^\.\.?(\/|$)/
const defaultInclude = ['**/*']
const defaultExclude = [
'**/node_modules',
'**/bower_components',
'**/jspm_packages',
]
/**
* The returned function does not support absolute paths.
* Be sure to call `path.relative` on your path first.
*/
function getIncluder(
includePaths = defaultInclude,
excludePaths = defaultExclude,
outDir?: string
) {
if (outDir) {
excludePaths = excludePaths.concat(outDir)
}
if (includePaths.length || excludePaths.length) {
const includers: RegExp[] = []
const excluders: RegExp[] = []
includePaths.forEach(addCompiledGlob, includers)
excludePaths.forEach(addCompiledGlob, excluders)
debug(`compiled globs:`, { includers, excluders })
return (path: string) => {
path = path.replace(/\?.+$/, '')
if (!relativeImportRE.test(path)) {
path = './' + path
}
const test = (glob: RegExp) => glob.test(path)
return includers.some(test) && !excluders.some(test)
}
}
return () => true
}
function addCompiledGlob(this: RegExp[], glob: string) {
const endsWithGlob = glob.split('/').pop()!.includes('*')
const relativeGlob = relativeImportRE.test(glob) ? glob : './' + glob
if (endsWithGlob) {
this.push(compileGlob(relativeGlob))
} else {
// Append a globstar to possible directories.
this.push(compileGlob(relativeGlob + '/**'))
// Try to match specific files (must have file extension).
if (/\.\w+$/.test(glob)) {
this.push(compileGlob(relativeGlob))
}
}
}
function compileGlob(glob: string) {
return globRex(glob, {
extended: true,
globstar: true,
}).regex
}
function ensureRelative(dir: string, path: string) {
return isAbsolute(path) ? relative(dir, path) : path
}