“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:

  1. TS adds an extension to Node logic; For example:.ts,.tsx and.d.ts

  2. 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

  1. /root/src/moduleB.ts
  2. /root/src/moduleB.tsx
  3. /root/src/moduleB.d.ts
  4. /root/src/moduleB/package.json(If specified"types"Attributes)
  5. /root/src/moduleB/index.ts
  6. /root/src/moduleB/index.tsx
  7. /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:

  1. /root/src/node_modules/moduleB.ts
  2. /root/src/node_modules/moduleB.tsx
  3. /root/src/node_modules/moduleB.d.ts
  4. /root/src/node_modules/moduleB/package.json(If specified"types"Attributes)
  5. /root/src/node_modules/moduleB/index.ts
  6. /root/src/node_modules/moduleB/index.tsx
  7. /root/src/node_modules/moduleB/index.d.ts
  8. /root/node_modules/moduleB.ts
  9. /root/node_modules/moduleB.tsx
  10. /root/node_modules/moduleB.d.ts
  11. /root/node_modules/moduleB/package.json(If specified"types"Attributes)
  12. /root/node_modules/moduleB/index.ts
  13. /root/node_modules/moduleB/index.tsx
  14. /root/node_modules/moduleB/index.d.ts
  15. /node_modules/moduleB.ts
  16. /node_modules/moduleB.tsx
  17. /node_modules/moduleB.d.ts
  18. /node_modules/moduleB/package.json(If specified"types"Attributes)
  19. /node_modules/moduleB/index.ts
  20. /node_modules/moduleB/index.tsx
  21. /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