microsoft / microsoft/TypeScript

Using `"allowJs": true` and `"module": "commonjs"` to transform `.mjs` files should emit `.cjs` files

オープン
#54,573 コメント 12 件 リアクション 4 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Domain: Node ESM Possible Improvement
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

Bug Report

🔎 Search Terms

CommonJS, allowJS, ESM, CJS, file extensions

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about output "file extensions for CommonJS modules".
⏯ Playground Link

Sandbox link with relevant code (I couldn't use the playground, since this involves using an .mjs file).

💻 Code
// package.json
{
  "name": "tsc-mjs-extension-bug",
  "version": "0.1.0",
  "type": "commonjs",
  "main": "dist/javascript.mjs",
  "scripts": {
    "build": "tsc",
    "test": "node ."
  },
  "dependencies": {
    "typescript": "5.1.3"
  }
}

// tsconfig.json
{
  "compilerOptions": {
    "module": "commonjs",
    "moduleResolution": "nodenext",
    "allowJs": true,
    "outDir": "dist"
  }
  "include": ["src"]
}
// src/javascript.mjs
export const where = "javascript";
console.log(`Hello ${where}`);
// src/typescript.ts
export const where = "typescript";
console.log(`Hello ${where}`);
🙁 Actual behavior

The javascript.mjs is transformed to CommonJS (as expected), but the file extension of the emitted file is still .mjs.
This breaks the package, since .mjs is supposed to be use exclusively for JavaScript using ESM and the emitted file now uses CommonJS.

🙂 Expected behavior

I would expect tsc to transform the src/javascript.mjs to CommonJS and either:

  1. emit it as dist/javascript.cjs or alternatively
  2. take into account the "type": "commonjs" in the package.json and emit the file as .js as it does with the src/typescript.ts file.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

レポートにある package.json、tsconfig.json、src/javascript.mjs、src/typescript.ts を使って問題を再現し、その後 build スクリプトと test スクリプトを実行します。dist に出力された拡張子とモジュール形式を比較します。完了とは、CommonJS に変換された .mjs 入力が、その形式で有効な拡張子で出力され、選択した動作がテストでカバーされていることを意味します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript, typescript
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
42/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。