-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
feat(compiler-sfc): support Node subpath imports for type resolution #13813
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
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -39,9 +39,10 @@ import { parse as babelParse } from '@babel/parser' | |||||
import { parse } from '../parse' | ||||||
import { createCache } from '../cache' | ||||||
import type TS from 'typescript' | ||||||
import { dirname, extname, join } from 'path' | ||||||
import { dirname, extname, isAbsolute, join } from 'path' | ||||||
import { minimatch as isMatch } from 'minimatch' | ||||||
import * as process from 'process' | ||||||
import { imports as resolveImports } from 'resolve.exports' | ||||||
edison1105 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
export type SimpleTypeResolveOptions = Partial< | ||||||
Pick< | ||||||
|
@@ -958,7 +959,9 @@ function importSourceToScope( | |||||
) | ||||||
} | ||||||
} | ||||||
resolved = resolveWithTS(scope.filename, source, ts, fs) | ||||||
resolved = | ||||||
resolveWithTS(scope.filename, source, ts, fs) || | ||||||
resolveWithNodeSubpathImports(scope.filename, source, fs) | ||||||
} | ||||||
if (resolved) { | ||||||
resolved = scope.resolvedImportSources[source] = normalizePath(resolved) | ||||||
|
@@ -1123,6 +1126,58 @@ function loadTSConfig( | |||||
return res | ||||||
} | ||||||
|
||||||
function resolveWithNodeSubpathImports( | ||||||
containingFile: string, | ||||||
source: string, | ||||||
fs: FS, | ||||||
): string | undefined { | ||||||
if (!__CJS__) return | ||||||
|
||||||
try { | ||||||
const pkgPath = findPackageJsonFile(containingFile, fs) | ||||||
if (!pkgPath) { | ||||||
return | ||||||
} | ||||||
|
||||||
const pkgStr = fs.readFile(pkgPath) | ||||||
if (!pkgStr) { | ||||||
return | ||||||
} | ||||||
|
||||||
const pkg = JSON.parse(pkgStr) | ||||||
const resolvedImports = resolveImports(pkg, source) | ||||||
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. Update to use correct import name from resolve.exports. After fixing the import statement, update this line to use the correct function name. - const resolvedImports = resolveImports(pkg, source)
+ const resolvedImports = imports(pkg, source) 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||
if (!resolvedImports || !resolvedImports.length) { | ||||||
return | ||||||
} | ||||||
|
||||||
const resolved = isAbsolute(resolvedImports[0]) | ||||||
? resolvedImports[0] | ||||||
: joinPaths(dirname(pkgPath), resolvedImports[0]) | ||||||
|
||||||
return fs.realpath ? fs.realpath(resolved) : resolved | ||||||
} catch (e) {} | ||||||
} | ||||||
|
||||||
function findPackageJsonFile( | ||||||
searchStartPath: string, | ||||||
fs: FS, | ||||||
): string | undefined { | ||||||
let currDir = searchStartPath | ||||||
while (true) { | ||||||
const filePath = joinPaths(currDir, 'package.json') | ||||||
if (fs.fileExists(filePath)) { | ||||||
return filePath | ||||||
} | ||||||
|
||||||
const parentDir = dirname(currDir) | ||||||
if (parentDir === currDir) { | ||||||
return | ||||||
} | ||||||
|
||||||
currDir = parentDir | ||||||
} | ||||||
} | ||||||
|
||||||
const fileToScopeCache = createCache<TypeScope>() | ||||||
|
||||||
/** | ||||||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
💡 Verification agent
🧩 Analysis chain
Test failure indicates the feature isn't working as expected.
Based on the pipeline failure message "Failed to resolve import source '#t1'", the Node subpath imports resolution is not working correctly. This could be due to:
resolve.exports
(as noted in the other file)__CJS__
flag during test executionLet me verify the test environment setup:
🏁 Script executed:
Length of output: 993
🏁 Script executed:
Length of output: 2326
🏁 Script executed:
Length of output: 2807
Ensure Node subpath imports are resolved at runtime and in tests
The failure “Failed to resolve import source ‘#t1’” is expected:
resolve.exports
is only listed under devDependencies, so it isn’t available when this code path executes.__CJS__
guard is falsey by default (no global definition), soresolveWithNodeSubpathImports
immediately returns without attempting subpath resolution.To fix this:
resolve.exports
intodependencies
so it’s always installed at runtime.__CJS__ = true
in the test environment (e.g. via a Jest setup file orglobals
in your test config) so the Node‐only resolution path is exercised.import { imports as resolveImports } from 'resolve.exports'
is correct).Locations to update:
+},
+},
// …
}
and ensure it’s loaded before tests (via your test runner’s
setupFiles
/setup
option).These changes will install the resolver at runtime and unblock the
resolveWithNodeSubpathImports
logic in tests.🤖 Prompt for AI Agents