Repository navigation
refactor: clean up some dependencies #836
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
6ea1a97
4f2cf4b
b019576
7e0cdea
3ef1dcb
2ba4325
d8e0727
1787347
79f6f5a
e0c379b
d315685
b5922ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,22 @@ | ||
| import { existsSync, readFileSync } from 'node:fs' | ||
| import { createRequire, registerHooks } from 'node:module' | ||
| import { fileURLToPath, pathToFileURL } from 'node:url' | ||
| import { dirname, relative, resolve } from 'pathe' | ||
| import { resolvePathSync } from 'mlly' | ||
| import { dirname, join, relative, resolve } from 'pathe' | ||
| import { createPathsMatcher, getTsconfig } from 'get-tsconfig' | ||
| import type ts from 'typescript' | ||
|
|
||
| import type { Transform } from '../transpile/transpile-dir.ts' | ||
| import { isInside, NODE_MODULES_RE, probe, relativeSpecifier, SCRIPT_RE, TYPESCRIPT_RE } from '../utils.ts' | ||
| import { packageName } from '../transpile/tsconfig.ts' | ||
| import { findPackageDir, findUp, isFile, isInside, isPlainObject, NODE_MODULES_RE, probe, relativeSpecifier, resolveFrom, SCRIPT_RE, TYPESCRIPT_RE } from '../utils.ts' | ||
| import type { PackageJson } from '../utils.ts' | ||
| import { guide } from './guide.ts' | ||
| import { importsKeyFor } from './package.ts' | ||
|
|
||
| const VIRTUAL_ALIAS_RE = /^#(?:app|imports|build)(?:\/|$)/ | ||
| const PATH_ALIAS_RE = /^(?:~|@\/)/ | ||
| const JSON_RE = /\.json$/ | ||
| const UNLOADABLE_RE = /\.(?:vue|[jt]sx)$/ | ||
| const IMPORT_CONDITIONS = new Set(['import', 'node', 'default']) | ||
|
|
||
| export interface ImportModuleOptions { | ||
| /** Module root, inside which `version` applies. */ | ||
|
|
@@ -214,20 +217,6 @@ function walkSources(cwd: string, entries: string[], visit: (file: string, analy | |
| } | ||
| } | ||
|
|
||
| function resolvesAsPackage(specifier: string, parentDir: string) { | ||
| try { | ||
| return resolvePathSync(specifier, { url: pathToFileURL(resolve(parentDir, 'index.js')), conditions: ['import', 'node', 'default'] }) | ||
| } | ||
| catch { | ||
| try { | ||
| return createRequire(resolve(parentDir, 'index.js')).resolve(specifier) | ||
| } | ||
| catch { | ||
| return undefined | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * The module source `specifier` reaches: a relative file, an entry in the `imports` of the | ||
| * module's `package.json`, or a tsconfig `paths` alias. Packages and the aliases only a | ||
|
|
@@ -241,10 +230,16 @@ function resolveSource(specifier: string, parentDir: string, cwd: string, matchP | |
| const file = probe(resolve(parentDir, specifier)) | ||
| return file ? { file, via: 'relative' } : undefined | ||
| } | ||
| const resolved = PATH_ALIAS_RE.test(specifier) ? undefined : resolvesAsPackage(specifier, parentDir) | ||
| const resolved = PATH_ALIAS_RE.test(specifier) | ||
| ? undefined | ||
| : (specifier.startsWith('#') && resolvePackageImport(specifier, parentDir)) || resolveFrom(resolve(parentDir, 'index.js'), specifier) | ||
| if (resolved) { | ||
| return specifier.startsWith('#') && isInside(cwd, resolved) && !NODE_MODULES_RE.test(resolved) ? { file: resolved, via: 'imports' } : undefined | ||
| } | ||
| const name = packageName(specifier) | ||
| if (name && findPackageDir(name, parentDir)) { | ||
| return | ||
|
Comment on lines
+240
to
+241
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check the full specifier before skipping alias detection. If Resolve the full specifier under import conditions before skipping the paths matcher. Keep importable packages ahead of aliases. (raw.githubusercontent.com)
|
||
| } | ||
| for (const candidate of matchPath?.(specifier) || []) { | ||
| const file = probe(candidate) | ||
| if (file) { | ||
|
|
@@ -253,6 +248,37 @@ function resolveSource(specifier: string, parentDir: string, cwd: string, matchP | |
| } | ||
| } | ||
|
|
||
| /** The file that `specifier` maps to through the `imports` of the closest `package.json`, with the conditions of `import`. */ | ||
| function resolvePackageImport(specifier: string, parentDir: string) { | ||
| const scope = findUp(parentDir, dir => existsSync(join(dir, 'package.json')) ? dir : undefined) | ||
| const { imports } = scope ? JSON.parse(readFileSync(join(scope, 'package.json'), 'utf8').replace(/^\uFEFF/, '')) as PackageJson : {} | ||
| const key = isPlainObject(imports) ? importsKeyFor(Object.keys(imports), specifier) : undefined | ||
| if (!scope || !key) { | ||
| return | ||
| } | ||
| const star = key.indexOf('*') | ||
| const target = importTarget((imports as Record<string, unknown>)[key], star === -1 ? undefined : specifier.slice(star, specifier.length - key.length + star + 1)) | ||
| const file = target?.startsWith('./') ? resolve(scope, target) : undefined | ||
| return file && isFile(file) ? file : undefined | ||
| } | ||
|
|
||
| function importTarget(target: unknown, match: string | undefined): string | undefined { | ||
| if (typeof target === 'string') { | ||
| return match === undefined ? target : target.replaceAll('*', match) | ||
| } | ||
| if (Array.isArray(target)) { | ||
| return target.map(item => importTarget(item, match)).find(item => item !== undefined) | ||
| } | ||
| if (isPlainObject(target)) { | ||
| for (const [condition, value] of Object.entries(target)) { | ||
| const resolved = IMPORT_CONDITIONS.has(condition) ? importTarget(value, match) : undefined | ||
| if (resolved !== undefined) { | ||
| return resolved | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let typescript: typeof ts | undefined | ||
|
|
||
| function scriptKind(ts: typeof import('typescript'), filename: string) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolve import-only packages before checking tsconfig aliases.
If an installed package exports only an
importtarget,resolveFromcannot resolve it under require conditions. An existing tsconfigpathsentry for that package then reaches the alias branch, although Node can import the installed package. (nodejs.org)findModuleCodeIssuesreports a false alias error, andassertLoadableModuleCodeblocks the build. The removed helper tried import conditions first. Restore that check before alias detection, and retain the require fallback for packages that need it. (raw.githubusercontent.com)