Path mappings based module resolution
Problem
Proposed module resolution strategy UPDATE: proposal below is updated based on the results of the design meeting. Initial version can be found here. Primary differences: - instead of having `baseUrl` as a separate module resolution strategy we are introducing a set of properties that will allow to customize resoluton process in existing resolution strategies but base strategy still is used as a fallback. - `rootDirs` are decoupled from the `baseUrl` and can be used without it. Currently TypeScript supports two ways of resolving module names: `classic` (module name always resolves to a file, module are searched using a folder walk) and `node` (uses rules similar to node module loader, was introduced in TypeScript 1.6). These approaches worked reasonably well but they were not able to model _baseUrl_ based mechanics used by RequireJS or SystemJS. We could introduce third type of module resolution that will fill this gap but this will mean that once user has started to use this new type then support to discover typings embedded in node modules (and distributed via `npm`) is lost. Effectively user that wanted both to use `baseUrl` to refer to modules defined inside the project and rely on `npm` to obtain modules with typings will have to choose what part of the system will be broken. Instead of doing this we'll allow to declare a set of properties that will augment existing module resolution strategies. These properties are: `baseUrl`, `paths` and `rootDirs` (`paths`
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Enhance TypeScript Module Resolution with Custom Properties
The current TypeScript module resolution strategies (classic and node) do not adequately support the baseUrl mechanics used by RequireJS or SystemJS. This limitation forces users to choose between using baseUrl for local modules and relying on npm for external modules with typings, leading to potential conflicts and loss of functionality.
Awaiting Verification
Be the first to verify this fix
- 1
Define Custom Module Resolution Properties
Introduce a set of properties in your TypeScript configuration file (tsconfig.json) to customize the module resolution process. This includes baseUrl, paths, and rootDirs, which will enhance the existing strategies without breaking compatibility with npm modules.
bash[object Object] - 2
Update TypeScript Configuration
Modify your tsconfig.json file to include the new properties. Ensure that baseUrl is set to the directory where your modules are located, and define paths for any aliases you want to use.
bash[object Object] - 3
Decouple rootDirs from baseUrl
Ensure that rootDirs can be used independently of baseUrl. This allows for a more flexible directory structure and enables TypeScript to resolve modules from multiple root directories effectively.
bash[object Object] - 4
Test Module Resolution
Run your TypeScript compiler to ensure that the new module resolution properties are functioning as expected. Check for any errors related to module imports and confirm that all modules are resolved correctly.
bash[object Object] - 5
Validate Compatibility with npm Modules
Verify that the changes do not interfere with the discovery of typings in npm modules. Import a few npm packages and ensure that TypeScript recognizes their types without issues.
bash[object Object]
Validation
To confirm the fix worked, ensure that TypeScript compiles without any module resolution errors and that all imports resolve correctly. Additionally, check that npm modules are still recognized and their typings are available.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep