microsoft / microsoft/TypeScript
Compiler option to define output-to-input directory map for module resolution of package.json `"imports"`/`"exports"`
还没有人认领这个 Issue。
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.3k
- 平均合并
- 2 天 4 小时
- 30 天内合并 PR
- 132
描述
🔍 Search Terms
"rootDir", "outDir", "paths", "map", "mapping", "imports", "exports", "moduleResolution", "module resolution"
✅ Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion
A TS project with moduleResolution: bundler (and others) uses the rootDir and outDir options to map the project's package.json "imports" and "exports" entries from outDir to rootDir directory. However, this only works for files inside rootDir.
A new option could be introduced for programs to define/inform that another local project compiles rootDir into outDir. This would be similar (indeed, identical in intent) to the paths option, but with more convenient support for package.json's "imports"/"exports" resolution, since only output-to-input directory mappings are required; with paths, one mapping would be required for every "imports" and "exports" entry.
📃 Motivating Example
Consider a project with the following structure:
├──📁 src
├──📁 test
├──📄 package.json
├──📄 tsconfig.json // references the other config files
├──📄 tsconfig.src.json
└──📄 tsconfig.test.json
// tsconfig.src.json
"compilerOptions": {
"module": "esnext",
"moduleResolution": "bundler",
"rootDir": "src",
"outDir": "dts",
},
"include": ["src"]
// package.json
"imports": {
"#foo": {
"types": "./dts/foo.d.ts"
}
}
With the previous configuration, tsc is able to resolve #foo imports inside src/, even if dts/ has not yet been generated, because of rootDir and outDir.
The desired behavior is for tsconfig.test.json (for the test/ directory) to also be able to resolve #foo imports before dts/ is generated. That is, inform tsconfig.test.json that other program compiles src into dts. This could be achieved with a new compiler option:
// tsconfig.test.json
"<newOptionName>": {
"dts": "src" // Inform tsc that other program compiles src into dts
}
💻 Use Cases
To resolve package.json's "imports" and "exports" output file paths by mapping them to corresponding input files. It may be seen in the previous example that this would be useful in the same way import specifier maps with paths is useful, but more convenient for "imports"/"exports" resolution.
Alternatives
1. paths
The desired behavior can be achieved with the paths option, but it is not as convenient since a map entry is required for every "imports" and "exports" entry, paths mappings are in a different file, and updating "imports"/"exports" requires a corresponding paths update.
// package.json
"imports": {
"#foo": {
"types": "./dts/foo.d.ts"
},
"#bar": {
"types": "./dts/bar.d.ts"
}
}
// tsconfig.json
"paths": {
"#foo": ["./src/foo.ts"],
"#bar": ["./src/bar.ts"]
}
Besides, it seems more appropriate for tsc to resolve "#" and self-reference import specifiers from paths defined by package.json "imports"/"exports" since that is, after all, their purpose.
2. customConditions
A second alternative is using customConditions, but drawbacks are somewhat similar. Although with a custom condition, all map entries can be defined in package.json, more boilerplate is required:
// package.json
"imports": {
"#foo": {
"types": {
"dev": "./src/foo.ts",
"default": "./dts/foo.d.ts"
}
},
"#bar": {
"types": {
"dev": "./src/bar.ts",
"default": "./dts/bar.d.ts"
}
}
}
// tsconfig.json
"customConditions": ["dev"]
This, however, better mirrors the desired behavior; the resolution process with this configuration is very similar to the outDir to rootDir mapping tsc performs (see package.json "imports" and self-name imports).
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
该 issue 没有列出仓库源文件或测试。首先使用所示的 tsconfig.src.json 和 tsconfig.test.json 配置,复现 rootDir/outDir 以及 package.json 的 "imports" 和 "exports" 行为,然后跟踪现有的 moduleResolution 处理方式。完成的标准是:一个有文档说明的编译器选项能够将另一个项目的输出目录重新映射回其输入文件,而无需要求匹配的 paths 条目。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- typescript
- 领域
- compilers
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 28/100