“This is the 23rd day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”
preface
As for module resolution of TS, through the previous several articles, we have learned about the process of MODULE resolution of TS, and what are the differences between relative and non-relative module import in TS, and the Classic module resolution strategy
There is also a Node module resolution policy in TS, which attempts to emulate node.js module resolution at runtime. Therefore, in the previous article we looked at the node.js module resolution process, and briefly summarized it as follows:
This is a foreshadowing for this article
The following is the content of this article: TS module parsing policy Node
The body of the
As we know, the Node module resolution policy mimics the Node.js runtime resolution policy to locate the module definition file’s module resolution policy at compile time, but is slightly different from Node.js
The difference between TS Node resolution strategy and Node.js module resolution strategy
There are two main differences between TS Node resolution policy and Node.js module resolution policy:
-
TS adds an extension to Node logic; For example:.ts,.tsx and.d.ts
-
TS uses the field “types” in package.json to mean something like “main”
And you can see how that happens
Of course, depending on the path, there are two parsing processes: relative and non-relative path parsing
Relative path resolution process
Import {b} from “./moduleB” import {b} from “./moduleB”
It parses and locates modules as follows:./moduleB
/root/src/moduleB.ts
/root/src/moduleB.tsx
/root/src/moduleB.d.ts
/root/src/moduleB/package.json
(If specified"types"
Attributes)/root/src/moduleB/index.ts
/root/src/moduleB/index.tsx
/root/src/moduleB/index.d.ts
Non-relative path resolution process
Non-relative imports follow Node.js parsing logic, first looking for the file, then the appropriate folder
For example, there is an import statement import {b} from “moduleB” in /root/src/ modulea. ts
Since it is a non-relative path import, its parsing process looks like this:
/root/src/node_modules/moduleB.ts
/root/src/node_modules/moduleB.tsx
/root/src/node_modules/moduleB.d.ts
/root/src/node_modules/moduleB/package.json
(If specified"types"
Attributes)/root/src/node_modules/moduleB/index.ts
/root/src/node_modules/moduleB/index.tsx
/root/src/node_modules/moduleB/index.d.ts
/root/node_modules/moduleB.ts
/root/node_modules/moduleB.tsx
/root/node_modules/moduleB.d.ts
/root/node_modules/moduleB/package.json
(If specified"types"
Attributes)/root/node_modules/moduleB/index.ts
/root/node_modules/moduleB/index.tsx
/root/node_modules/moduleB/index.d.ts
/node_modules/moduleB.ts
/node_modules/moduleB.tsx
/node_modules/moduleB.d.ts
/node_modules/moduleB/package.json
(If specified"types"
Attributes)/node_modules/moduleB/index.ts
/node_modules/moduleB/index.tsx
/node_modules/moduleB/index.d.ts
Despite the above process, it is the same as the parsing process in Node.js
END
Well, that’s all for this article. If you have any questions, please point out