“This is the 22nd day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

In the previous two articles, we looked at the TS module resolution process and the differences between relative and non-relative module imports in TS. Both articles mentioned the TS module resolution policy: Does the compiler parse the policy according to Classic or Node, and go to the corresponding place to find the module

Related articles can be accessed at the following link:

  • TS module parsing process
  • Do you know the difference between relative and non-relative module imports in TS?

Today we’re going to talk about one of those parsing strategies: Classic

You can use the –moduleResolution tag to specify which moduleResolution strategy to use

If use this tag to specify, to distinguish the Classic strategy is simple, if you use – the module AMD | System | ES2015, the default for the Classic, other conditions for the Node

Two things to remember about the Classic strategy:

First, a relative imported module is parsed relative to the file into which it was imported

So import {b} from “./moduleB” in /root/src/folder/ a.ts will use the following search flow:

  1. /root/src/folder/moduleB.ts
  2. /root/src/folder/moduleB.d.ts

Second, for non-relative module imports, the compiler will start at the directory containing the import file and work its way up the directory, trying to locate a matching declaration file

For example, there is a non-relative import of moduleB import {b} from “moduleB”, which is in the /root/src/ Folder/a.ts file, and will locate “moduleB” as follows:

  1. /root/src/folder/moduleB.ts
  2. /root/src/folder/moduleB.d.ts
  3. /root/src/moduleB.ts
  4. /root/src/moduleB.d.ts
  5. /root/moduleB.ts
  6. /root/moduleB.d.ts
  7. /moduleB.ts
  8. /moduleB.d.ts

The raison d ‘etre of Classic is primarily for backward compatibility

END

In addition to the Classic parsing policy, there is also a Node parsing policy, which will be covered in a later article

That’s all for this article. If you have any questions, please point out