If you're looking to streamline your module imports, consider utilizing a combination of baseUrl
and paths
in your tsconfig.json
. For detailed information, refer to the comprehensivedocumentation provided here.
An example presented in the documentation closely resembles your situation:
With "paths," you can create more complex mappings that include multiple fallback locations. Imagine a project setup where specific modules are located in one place while others are in separate directories. After a build process consolidates these modules, the project structure may appear as follows:
projectRoot
├── folder1
│ ├── file1.ts (imports 'folder1/file2' and 'folder2/file3')
│ └── file2.ts
├── generated
│ ├── folder1
│ └── folder2
│ └── file3.ts
└── tsconfig.json
The corresponding configuration within tsconfig.json would be:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"*": [
"*",
"generated/*"
]
}
}
}
This setup instructs the compiler to search in two locations - baseUrl
and generated/
for any module import matching the "*" pattern.
import ‘folder2/file3’
- the wildcard captures the entire module name under the '*' pattern
- first substitution attempt: ‘*’ -> folder2/file3
- non-relative name result requires combining with baseUrl -> projectRoot/folder2/file3.ts.
- File not found, move to second substitution
- second substitution ‘generated/*’ -> generated/folder2/file3
- non-relative name result requires combining with baseUrl -> projectRoot/generated/folder2/file3.ts.
- File found. Process complete
In your scenario, consider using my_modules
instead of generated/*
:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"*": [
"my_modules/*",
"*",
]
}
}
}