“This is the 21st day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”

preface

In previous articles, we looked at TS module parsing

This article is about it, so let’s review the general flow of module resolution:

  1. First, the compiler looks at theClassicorNodeParse the policy, go to the appropriate place to look for modules
  2. If the above resolution fails and the module name is nonrelative, the compiler tries to locate an external module declaration
  3. If it still cannot be resolved, an error is logged

The second step mentioned that module names are nonrelative

This is also the subject of this article, what are relative and non-relative modules, and how are they different

Relative and non-relative module imports

In fact, there are two main differences between relative and non-relative module imports

1. Different forms

Relative imports start with /,./ or.. / at the beginning

Non-relative imports are the opposite, except with /,./ or.. All other forms of the/are said to be non-relative

Official examples are as follows:

// Relative - 'import Entry from "./components/Entry"; ` - `import { DefaultHeaders } from ".. /constants/http"; ` - `import "/mod"; '// non-relative -' import * as $from "jQuery"; ` - `import { Component } from "@angular/core"; `Copy the code

2. Different parsing methods

The TS compiler resolves module imports differently depending on whether module references are relative or non-relative

Relative import (with /,./ or.. /) is parsed relative to the file into which it was imported, and cannot be parsed as an external module declaration

Imports of non-relative modules can be resolved relative to baseUrl or through pathmaps described below. They can also be resolved into external module declarations

Mind maps

For convenience, the final summary is as follows:

END

You may not know much about the two parsing strategies (Classic and Node) mentioned in the article! Read on for more on these two strategies in our next article

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