microsoft / microsoft/TypeScript
Using `"allowJs": true` and `"module": "commonjs"` to transform `.mjs` files should emit `.cjs` files
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.4k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
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:
- emit it as
dist/javascript.cjsor alternatively - take into account the
"type": "commonjs"in the package.json and emit the file as.jsas it does with thesrc/typescript.tsfile.
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Reproduce el problema con package.json, tsconfig.json, src/javascript.mjs y src/typescript.ts del informe, y luego ejecuta los scripts de build y test. Compara las extensiones y los formatos de módulo generados en dist; se considera terminado cuando la entrada .mjs transformada a CommonJS se genera con una extensión válida para ese formato, con cobertura para el comportamiento elegido.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- javascript, typescript
- Área
- compilers
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 42/100