This Presentation describes in details about module resolution concept in typescript. Based on this presentation you will know how compiler handling the modules concept.
2. Introduction
Module Resolution is the process uses by compiler to figure out imports.
Eg:-
import { a } from moduleA
The compiler check any usage of a in the entire application. If required
then the compiler will check the definition of moduleA.
4. Relative imports
A relative import is one of the importing mechanism. That starts with /, ./,
../.
Eg:-
import { DefaultHeaders } from ../constants/http;
Note:
Relative imports depends on reference path.
It is not supported for ambient modules.
5. Non-Relative imports
A non-relative import is one of the importing mechanism.
Eg:-
import { Component } from @angular/core;
Note:
Non-Relative imports depends on baseUrl.
It is supported for ambient modules too.
9. Resolution strategies - Node
This is resolution strategy attempts Node.js module resolution mechanism
at runtime.
Imports in node.js are performed by calling a function require.
The require behavior is differ from relative and no-relative imports in
different manner.
11. Resolution strategies - Node
Node Relative > resolution steps:
As a file
/ root / src / moduleB.js
As a folder
/ root / src / moduleB if it contains package.json and that specifies as main module.
Than node.js refers
/ root / src / moduleB / lib / mainModule.js.
As a folder
/ root / src / moduleB if it contains a file index.js. That file is implicitly considered that
folders main module.
12. Resolution strategies - Node
Node Non-Relative:
Node.js will look for your module in special folder node_modules.
A node_modules folder must be in the same level of hierarchy or higher up the director
chain.
Node will lookup directory chain, looking through each node_module until the module
tried to load.
15. How Typescript resolve modules
Typescript will use the node.js run-time resolution strategy for locate
definition files at compile time.
To accomplish this, Typescript overlays Typescript source file
extensions(.ts, .tsx and .d.ts).
Typescript will also use a field in package.json named typings.
Using typings the compiler will find main definition file.
23. Resolution Flags Base Url
Setting baseUrl informs the compiler where to find modules.
All non relative modules relative to baseUrl
Eg:-
{
compilerOptions : {
baseUrl : .
}
}
24. Resolution Flags Path Mapping
Some module are not located under baseUrl.
Eg:-
{
compilerOptions : {
baseUrl : .,
paths : {
jquery : [ node_modules / jquery / dist / jquery ]
}
}
}
25. Resolution Flags Virtual Directories
Some module are not located under baseUrl.
Eg:-
{
compilerOptions : {
rootDirs : [
src / views,
generated / templates / views
]
}
}