Search Terms
module file resolution custom
Suggestion
Add customModuleResolution / customModuleResolver to compilerOptions. This option provide path or name to custom module resolver module/file that should provide function that could resolve modules/files in custom way.
Simple implementation / working example:
https://github.com/webcarrot/TypeScript/commit/97781d8cba76fb3e3b4e07c295af918e94ceed80
Use Cases
Anything that is related with files resolution and cannot be handled by setting compilerOptions.paths etc.
Examples
Postfixs for different run-time environments:
Files:
./a.browser.ts // implementation for browsers
./a.node.ts // implementation for node
./b.ts
b.ts file:
import { a } from "./a"; // resolve to a.node.ts for example
Example custom resolver implementation:
import * as Typescript from "typescript";
import { existsSync } from "fs";
import { join, dirname } from "path";
type ResolveOutput =
| Typescript.ResolvedModuleWithFailedLookupLocations
| undefined;
const POSTFIXS = ["node", "browser", "modern"];
const EXTENSIONS = [
"d.ts",
"ts",
"tsx",
"js",
"jsx",
"abc" // custom extensions work - but not to well
];
const EXTENSIONS_MAP: { [key: string]: Typescript.Extension } = {
"d.ts": Typescript.Extension.Dts,
ts: Typescript.Extension.Ts,
tsx: Typescript.Extension.Tsx,
js: Typescript.Extension.Js,
jsx: Typescript.Extension.Jsx,
abc: Typescript.Extension.Ts // hack abc as ts
};
const check = (
path: string,
postfix: string,
extension: string
): ResolveOutput => {
const resolvedFileName = `${path}.${postfix}.${extension}`;
if (existsSync(resolvedFileName)) {
return {
resolvedModule: {
resolvedFileName,
isExternalLibraryImport: false,
extension: EXTENSIONS_MAP[extension]
}
};
}
return undefined;
};
export default (
moduleName: string,
containingFile: string,
compilerOptions: Typescript.CompilerOptions
): ResolveOutput => {
if (
moduleName.startsWith(".") &&
containingFile.startsWith(join(compilerOptions.baseUrl, "src"))
) {
const path = join(dirname(containingFile), moduleName);
return POSTFIXS.reduce<ResolveOutput>(
(out, postfix) =>
out
? out
: EXTENSIONS.reduce<ResolveOutput>(
(out, extension) => (out ? out : check(path, postfix, extension)),
undefined
),
undefined
);
}
return undefined;
};
Checklist
My suggestion meets these guidelines:
Contraindications
- This feature is insecure in language server context. Probably some configuration flag for language server should be provided to enable this feature.
- Nobody want feature like this? 😞
Search Terms
module file resolution custom
Suggestion
Add customModuleResolution / customModuleResolver to compilerOptions. This option provide path or name to custom module resolver module/file that should provide function that could resolve modules/files in custom way.
Simple implementation / working example:
https://github.com/webcarrot/TypeScript/commit/97781d8cba76fb3e3b4e07c295af918e94ceed80
Use Cases
Anything that is related with files resolution and cannot be handled by setting
compilerOptions.pathsetc.Examples
Postfixs for different run-time environments:
Files:
b.ts file:
Example custom resolver implementation:
Checklist
My suggestion meets these guidelines:
Contraindications