模块解析·续1
rootDirs的灵活性不仅仅局限于其指定了要在逻辑上合并的物理目录列表。它提供的数组可以包含任意数量的任何名字的目录,不论它们是否存在。这允许编译器以类型安全的方式处理复杂捆绑(bundles)和运行时的特性,比如条件引入和工程特定的加载器插件。
设想这样一个国际化的场景,构建工具自动插入特定的路径记号来生成针对不同区域的捆绑,比如将#{locale}做为相对模块路径./#{locale}/messages的一部分。在这个假定的设置下,工具会枚举支持的区域,将抽像的路径映射成./zh/messages,./de/messages等。
假设每个模块都会导出一个字符串的数组。比如./zh/messages可能包含:
export default [ "您好吗", "很高兴认识你"];
利用rootDirs我们可以让编译器了解这个映射关系,从而也允许编译器能够安全地解析./#{locale}/messages,就算这个目录永远都不存在。比如,使用下面的tsconfig.json:
{ "compilerOptions": { "rootDirs": [ "src/zh", "src/de", "src/#{locale}" ] }}
编译器现在可以将import messages from './#{locale}/messages'解析为import messages from './zh/messages'用做工具支持的目的,并允许在开发时不必了解区域信息。
跟踪模块解析
如之前讨论,编译器在解析模块时可能访问当前文件夹外的文件。 这会导致很难诊断模块为什么没有被解析,或解析到了错误的位置。 通过--traceResolution启用编译器的模块解析跟踪,它会告诉我们在模块解析过程中发生了什么。
假设我们有一个使用了typescript模块的简单应用。 app.ts里有一个这样的导入import * as ts from "typescript"。
│ tsconfig.json├───node_modules│ └───typescript│ └───lib│ typescript.d.ts└───src app.ts
使用--traceResolution调用编译器。
tsc --traceResolution
输出结果如下:
======== Resolving module 'typescript' from 'src/app.ts'. ========Module resolution kind is not specified, using 'NodeJs'.Loading module 'typescript' from 'node_modules' folder.File 'src/node_modules/typescript.ts' does not exist.File 'src/node_modules/typescript.tsx' does not exist.File 'src/node_modules/typescript.d.ts' does not exist.File 'src/node_modules/typescript/package.json' does not exist.File 'node_modules/typescript.ts' does not exist.File 'node_modules/typescript.tsx' does not exist.File 'node_modules/typescript.d.ts' does not exist.Found 'package.json' at 'node_modules/typescript/package.json'.'package.json' has 'types' field './lib/typescript.d.ts' that references 'node_modules/typescript/lib/typescript.d.ts'.File 'node_modules/typescript/lib/typescript.d.ts' exist - use it as a module resolution result.======== Module name 'typescript' was successfully resolved to 'node_modules/typescript/lib/typescript.d.ts'. ========
需要留意的地方
- 导入的名字及位置
======== Resolving module ‘typescript’ from ‘src/app.ts’. ========
- 编译器使用的策略
Module resolution kind is not specified, using ‘NodeJs’.
- 从npm加载types
‘package.json’ has ‘types’ field ‘./lib/typescript.d.ts’ that references ‘node_modules/typescript/lib/typescript.d.ts’.
- 最终结果
======== Module name ‘typescript’ was successfully resolved to ‘node_modules/typescript/lib/typescript.d.ts’. ========
使用--noResolve
正常来讲编译器会在开始编译之前解析模块导入。 每当它成功地解析了对一个文件import,这个文件被会加到一个文件列表里,以供编译器稍后处理。
--noResolve编译选项告诉编译器不要添加任何不是在命令行上传入的文件到编译列表。 编译器仍然会尝试解析模块,但是只要没有指定这个文件,那么它就不会被包含在内。
比如
app.ts
import * as A from "moduleA" // OK, moduleA passed on the command-lineimport * as B from "moduleB" // Error TS2307: Cannot find module 'moduleB'.
tsc app.ts moduleA.ts --noResolve
使用--noResolve编译app.ts:
-
可能正确找到moduleA,因为它在命令行上指定了。
-
找不到moduleB,因为没有在命令行上传递。
常见问题
为什么在exclude列表里的模块还会被编译器使用
tsconfig.json将文件夹转变一个“工程” 如果不指定任何“exclude”或“files”,文件夹里的所有文件包括tsconfig.json和所有的子目录都会在编译列表里。 如果你想利用“exclude”排除某些文件,甚至你想指定所有要编译的文件列表,请使用“files”。
有些是被tsconfig.json自动加入的。 它不会涉及到上面讨论的模块解析。 如果编译器识别出一个文件是模块导入目标,它就会加到编译列表里,不管它是否被排除了。
因此,要从编译列表中排除一个文件,你需要在排除它的同时,还要排除所有对它进行import或使用了/// <reference path="..." />指令的文件。
