-
-
Notifications
You must be signed in to change notification settings - Fork 180
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
Support subpath import with arbitrary extensions #723
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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
{ | ||
"name": "my-package", | ||
"imports": { | ||
"#internals/*": "./src/internals/*.ts" | ||
"#internals/*": "./src/internals/*.ts", | ||
"#internals-explicit-ext/*": "./src/internals/*", | ||
"#internals-alias/used.alt": "./src/internals/used.alt" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
import used from '#internals/used'; | ||
import '#internals-explicit-ext/used.ext'; | ||
import '#internals-alias/used.alt'; | ||
used; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
import { existsSync } from 'node:fs'; | ||
import { isBuiltin } from 'node:module'; | ||
import { createMatchPath } from 'tsconfig-paths'; | ||
import ts from 'typescript'; | ||
import { DEFAULT_EXTENSIONS } from '../constants.js'; | ||
import { timerify } from '../util/Performance.js'; | ||
import { sanitizeSpecifier } from '../util/modules.js'; | ||
import { dirname, extname, isAbsolute, isInNodeModules, join, toPosix } from '../util/path.js'; | ||
import { dirname, extname, isAbsolute, isInNodeModules, join } from '../util/path.js'; | ||
import { resolveSync } from '../util/resolve.js'; | ||
import type { ToSourceFilePath } from '../util/to-source-path.js'; | ||
import { isDeclarationFileExtension } from './ast-helpers.js'; | ||
|
@@ -35,6 +34,32 @@ export function createCustomModuleResolver( | |
const customCompilerExtensionsSet = new Set(customCompilerExtensions); | ||
const extensions = [...DEFAULT_EXTENSIONS, ...customCompilerExtensions]; | ||
|
||
const virtualDeclarationFiles = new Map<string, { path: string; ext: string }>(); | ||
|
||
const tsSys: ts.System = { | ||
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. Unless there's a good reason not to, I think we could move 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. My thinking behind this was that If you agree, I’ll keep it there, otherwise I can move it out. 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. You're right! |
||
...ts.sys, | ||
// We trick TypeScript into resolving paths with arbitrary extensions. When | ||
// a module "./module.ext" is imported TypeScript only tries to resolve it to | ||
// "./module.d.ext.ts". TypeScript never checks whether "./module.ext" itself exists. | ||
// So, if TypeScript checks whether "./module.d.ext.ts" exists and the file | ||
// does not exist we can assume the compiler wants to resolve `./module.ext`. | ||
// If the latter exists we return true and record this fact in | ||
// `virtualDeclarationFiles`. | ||
fileExists(path: string) { | ||
if (ts.sys.fileExists(path)) { | ||
return true; | ||
} | ||
|
||
const original = originalFromDeclarationPath(path); | ||
if (original && ts.sys.fileExists(original.path)) { | ||
virtualDeclarationFiles.set(path, original); | ||
return true; | ||
} | ||
|
||
return false; | ||
}, | ||
}; | ||
|
||
function resolveModuleNames(moduleNames: string[], containingFile: string): Array<ts.ResolvedModuleFull | undefined> { | ||
return moduleNames.map(moduleName => { | ||
if (!useCache) return resolveModuleName(moduleName, containingFile); | ||
|
@@ -54,13 +79,6 @@ export function createCustomModuleResolver( | |
}); | ||
} | ||
|
||
const tsMatchPath = createMatchPath( | ||
// If `baseUrl` is undefined we have already modified `paths` so that all | ||
// entries are absolute. See `mergePaths` in `src/PrincipalFactory.ts`. | ||
compilerOptions.baseUrl ?? '/', | ||
compilerOptions.paths || {} | ||
); | ||
|
||
/** | ||
* - Virtual files have built-in or custom compiler, return as JS | ||
* - Foreign files have path resolved verbatim (file manager will return empty source file) | ||
|
@@ -97,29 +115,11 @@ export function createCustomModuleResolver( | |
}; | ||
} | ||
|
||
const pathMappedFileName = tsMatchPath( | ||
sanitizedSpecifier, | ||
undefined, | ||
undefined, | ||
// Leave extensions empty and let `ts.resolveModuleName()` handle that. | ||
// `tsMatchPath` will strip extensions from the returned specifier which | ||
// means we don’t get the actual file path. | ||
[] | ||
); | ||
if (pathMappedFileName) { | ||
const ext = extname(pathMappedFileName); | ||
return { | ||
resolvedFileName: toPosix(pathMappedFileName), | ||
extension: customCompilerExtensionsSet.has(ext) ? ts.Extension.Js : ext, | ||
isExternalLibraryImport: false, | ||
}; | ||
} | ||
|
||
const tsResolvedModule = ts.resolveModuleName( | ||
sanitizedSpecifier, | ||
containingFile, | ||
compilerOptions, | ||
ts.sys | ||
tsSys | ||
).resolvedModule; | ||
|
||
if (tsResolvedModule) { | ||
|
@@ -135,6 +135,14 @@ export function createCustomModuleResolver( | |
}; | ||
} | ||
} | ||
const original = virtualDeclarationFiles.get(tsResolvedModule.resolvedFileName); | ||
if (original) { | ||
return { | ||
...tsResolvedModule, | ||
resolvedFileName: original.path, | ||
extension: customCompilerExtensionsSet.has(original.ext) ? ts.Extension.Js : original.ext, | ||
}; | ||
} | ||
|
||
return tsResolvedModule; | ||
} | ||
|
@@ -147,3 +155,19 @@ export function createCustomModuleResolver( | |
|
||
return timerify(resolveModuleNames); | ||
} | ||
|
||
const declarationPathRe = /^(.*)\.d(\.[^.]+)\.ts$/; | ||
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. This matches a lot, but I guess I'm having the same confusion as in the comment: I think you're saying "TypeScript tries to resolve |
||
|
||
/** | ||
* For paths that look like `.../module.d.yyy.ts` returns path `.../module.yyy` and | ||
* ext `yyy`. | ||
*/ | ||
function originalFromDeclarationPath(path: string): { path: string; ext: string } | undefined { | ||
const match = declarationPathRe.exec(path); | ||
if (match) { | ||
return { | ||
path: match[1] + match[2], | ||
ext: match[2], | ||
}; | ||
} | ||
} |
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.
🎉