microsoft / microsoft/TypeScript

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

Đang mở
#54,573 12 bình luận 4 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Domain: Node ESM Possible Improvement
Ngôn ngữ chính
Go
Star
111k
Fork
14.4k
Merge trung bình
1 ngày 19 giờ
Pull request đã merge (30 ngày)
117

Mô tả

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.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Tái hiện vấn đề bằng package.json, tsconfig.json, src/javascript.mjs và src/typescript.ts từ báo cáo, sau đó chạy các script build và test. So sánh các phần mở rộng và định dạng module được xuất ra trong dist; được xem là hoàn tất khi đầu vào .mjs được chuyển đổi sang CommonJS được xuất ra với phần mở rộng hợp lệ cho định dạng đó, kèm theo coverage cho hành vi đã chọn.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
javascript, typescript
Lĩnh vực
compilers
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
42/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.