microsoft / microsoft/TypeScript
Dual ESM/CJS emit with tsc
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
#54546 explored one approach of enabling dual ESM/CJS emit for packages with tsc alone. The idea was that instead of determining the module format of .ts files by looking for package.json files in its ancestor directories, we would look for them starting at the directory where that file’s .js output would be emitted (a subdirectory of outDir). That way, two tsconfig files could point to two different output directories, each pre-seeded with a package.json files that set the module format for the output generated by that tsconfig.
This approach has two main downsides:
- It’s annoying to have to commit package.json files into your output directory while gitignoring everything else. Additionally, if used in combination with other tools (e.g. tsc for declaration emit but rollup for JS emit), other tools might wipe the output directory, deleting your package.json.
- Thinking about the implications for projects that aren’t doing dual emit, if we determine the module format based only on the output file structure, we potentially fail to analyze the behavior of ts-node, or a bundler that cares about package.json
"type". This can be a problem if a project compiles with tsc for publishing, but runs input .ts files directly during development (which is a scenario I think we should make a habit of considering). Ideally, we want a solution that ensures the module format of the output agrees with that of the input, unless the output format is being intentionally changed by config (presumably for purposes of dual emitting).
I think both of these can be solved by doing two things:
- Introduce a compiler option that allows a package.json with
{ "type": "module" }or{ "type": "commonjs" }(or blank, whatever) into theoutDir, and use this config setting (instead of what’s already present in theoutDir) to determine the module format of input files at a higher priority than any package.json files in a directory higher thanoutDir. This solves problem (1) above—no need to pre-seed or commit files in your build directory. - Whenever a package.json in a subfolder of the common source directory /
rootDirthat affects the computed module format of a file is seen, emit that package.json (or a stub of it with just"type"?) into the corresponding subfolder withinoutDir. This solves (2), and actually solves an issue that exists today, where tsc output can be invalid for Node without manually copying a package.json that occurs insiderootDir. (@rbuckton mentioned this in team chat one time, but it didn’t get much discussion.)
Emitting package.json files would be new territory for us, but I think it’s worth it for the problems it solves.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず issue #54546 と、ここで説明されている module-format および package.json の動作を読みます。この issue では実装ファイルやテストは指定されていません。outDir に対するコンパイラーオプションの動作と、package.json ファイルを出力する条件を定義し、その後、デュアル ESM/CJS 出力と入力および出力のモジュール形式の一致が意図どおりに機能することを示すカバレッジを追加します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100