microsoft / microsoft/TypeScript

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

Offen
#54,573 12 Kommentare 4 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Domain: Node ESM Possible Improvement
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Reproduziere das Problem mit package.json, tsconfig.json, src/javascript.mjs und src/typescript.ts aus dem Bericht und führe anschließend die Build- und Testskripte aus. Vergleiche die ausgegebenen Erweiterungen und Modulformate in dist; erledigt bedeutet, dass die in CommonJS umgewandelte .mjs-Eingabe mit einer für dieses Format gültigen Erweiterung ausgegeben wird und das gewählte Verhalten durch Tests abgedeckt ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
42/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.