Suggestion
π Search Terms
nodenext, import mappings, package.json imports
β
Viability Checklist
My suggestion meets these guidelines:
β Suggestion
Right now, the detection for import mappings assume that the path passed in points both to the file to include in the compile and the built file. However, this cannot be used when you separate the dist from the src folder for example, as the file the import mapping would point to doesn't exist until after compiling. Ideally, just like how exports has a types property that can be specified so TS can tell where to find typings, so should imports.
π Motivating Example
Consider this example:
// package.json
{
"type": "module",
// ...
"imports": {
"#example": "./dist/example.mjs"
}
}
// tsconfig.json
{
"compilerOptions": {
"target": "ESNext",
"module": "NodeNext",
"moduleResolution": "NodeNext",
// ... extra configs
"rootDir": "./src",
"outDir": "./dist",
"tsBuildInfoFile": "./dist/.tsbuildinfo"
},
"include": ["src"]
}
// src/entrypoint.mts
import { add } from '#example';
console.log(add(9, 10));
// src/example.mts
export function add(num1: number, num2: number) {
return num1 + num2;
}
Given that structure, you'll receive a Cannot find module '#example'... error unless you ignore errors and still emit the built code. Ideally, we should be able to replace the package.json's content with something like:
// package.json
{
"type": "module",
// ...
"imports": {
"#example": {
"node": "./dist/example.mjs",
"types": "./src/example.mts"
}
}
}
which TS can then use to accurately point the files to their place.
π» Use Cases
A workaround for now is to specify the import paths in your tsconfig/jsconfig (but that defeats the purpose of just reading them from package.json). While this works, a better solution would be ideal.
Suggestion
π Search Terms
nodenext, import mappings, package.json imports
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
Right now, the detection for import mappings assume that the path passed in points both to the file to include in the compile and the built file. However, this cannot be used when you separate the dist from the src folder for example, as the file the import mapping would point to doesn't exist until after compiling. Ideally, just like how
exportshas atypesproperty that can be specified so TS can tell where to find typings, so shouldimports.π Motivating Example
Consider this example:
Given that structure, you'll receive a
Cannot find module '#example'...error unless you ignore errors and still emit the built code. Ideally, we should be able to replace the package.json's content with something like:which TS can then use to accurately point the files to their place.
π» Use Cases
A workaround for now is to specify the import paths in your tsconfig/jsconfig (but that defeats the purpose of just reading them from package.json). While this works, a better solution would be ideal.