Skip to content
This repository was archived by the owner on Mar 22, 2019. It is now read-only.

resolving.cn

e-cloud edited this page Jun 26, 2016 · 1 revision

The resolving process is pretty simple. We distinguish three types of requests: 解析过程相对简单。我们区分三种类型的请求:

  • absolute path: require("/home/me/file") require("C:\Home\me\file")

  • relative path: require("../src/file") require("./file")

  • module path: require("module") require("module/lib/file")

  • 绝对路径:require("/home/me/file") require("C:\Home\me\file")

  • 相对路径:require("../src/file") require("./file")

  • 模块路径:require("module") require("module/lib/file")

Resolving an absolute path

解析绝对路径

We first check if the path points to a directory. For a directory we need to find the main file in this directory. Therefore the main field in the package.json is joined to the path. If there is no package.json or no main field, index is used as filename. 我们首先检查路径是否指向一个目录。对于一个目录,我们需要找到此目录中的主文件。因此在package.jsonmain字段会合并到路径中。如果没有package.json或没有main字段,index被用作文件名。

We have an absolute path to a file now. We try to append all extensions (configuration option resolve.extensions). The first existing file is used as result. 我们现在有一个文件的绝对路径。我们尝试添加所有扩展名(配置选项resolve.extensions)。第一个存在的文件被采纳为结果。

Resolving a relative path

解析相对路径

The context directory is the directory of the resource file that contains the require statement. If there is no resource file the configuration option context is used as context directory. (This can occur for entry points or with loader-generated files). 上下文目录是一个包含了require语句的资源文件的目录。如果没有资源文件,配置选项context会被用作上下文目录。(这可能发生在入口点或加载生成的文件)

The relative path is joined to the context directory and the resulting absolute file is resolved according to "Resolving an absolute path". 相对路径被合并到上下文目录,并根据“解析绝对路径”解析对应文件。

Resolving a module path

解析模块路径

For resolving a module we first gather all search directories for modules from the context directory. This process is similar to the node.js resolving process, but the search directories are configurable with the configuration option resolve.modulesDirectories. In addition to this the directories in the configuration option resolve.root are prepended and directories in the configuration option resolve.fallback are appended. 为了解析一个模块,我们首先为上下文目录中的模块收集所有的搜索目录。这个过程类似于node.js的解析流程

The module is looked up in each module directory and resolved according to "Resolving an absolute path". If the first match has no success, the second is tried and so on. 会在每个模块目录中搜索模块,并根据“解析绝对路径”进行解析。如果第一个没有匹配成功,会不断尝试候选目录。

Aliasing

别名

There is a configuration option resolve.alias which renames modules. 有一个配置选项resolve.alias用来重命名模块。

When trying to "resolve a module path" the module name is matched to the resolve.alias option and when there is a match it gets replaced with the alias. 当试图“解析模块路径”,模块名称与resolve.alias选项进行匹配,当存在一个匹配,模块名会被别名替换。

Caching

缓存

Every filesystem access is cached so that multiple parallel or serial requests to the same thing are merged. In watching mode only changed files are removed from cache (the watcher knows which files got changed). In non-watching mode the cache is purged before every compilation. 每次文件系统的访问都会被缓存,以便合并对同一个文件的多个并行或串行请求。在监视模式下只有更改的文件会从缓存中被删除(监视器知道是哪个文件已经改变)。在非监视模式下。缓存在每次编译之前清除。

Unsafe caching

不安全的缓存

There is a configuration option resolve.unsafeCache which boosts performance by aggressive caching. 有一个配置选项resolve.unsafeCache能通过激进的缓存策略提升性能

Every resolve process is cached and isn't ever purged. This is correct in most cases, but incorrect in edge cases (what edge cases?). 每个解析过程都会被缓存,而且不会被清除。这在大多数情况下是正确的,但在边缘的场景下不正确的(什么边缘场景?

Context

上下文

When trying to resolve a context "Resolving an absolute path" ends when a directory is found. 当试图解析一个context时,“解析绝对路径”在找到一个目录后结束。

Loaders

For loaders the configuration options in resolveLoader are used. 对于 loader,会使用resolveLoader中的配置。

In addition to that when trying to "resolve a module path" all module name variations in the configuration option resolveLoader.moduleTemplates are tried. 除了试图“解析模块路径”时,配置选项resolveLoader.moduleTemplates中所有模块名的变体都会被尝试。

Asynchronous

异步

The above description suggests a serial process, but in the implementation the process is completely asynchronous and parallel. This may cause more filesystem access than required. 上面的描述建议一个串行的处理流程,但在实现中,该流程是完全异步和并行。这可能会导致比预期更多的文件系统访问。

Clone this wiki locally