Metro babel transformer doesn't recognize .cjs config files (babel.config.cjs, .babelrc.cjs)
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- C++
- Star
- 127k
- Fork
- 25.3k
- Merge trung bình
- 1 ngày 23 giờ
- Pull request đã merge (30 ngày)
- 4
Mô tả
Description
The @react-native/metro-babel-transformer doesn't recognize .cjs babel config files, which causes silent failures when projects use "type": "module" in their package.json.
Problem
In @react-native/metro-babel-transformer/src/index.js (lines 74-88), the transformer only searches for:
- .babelrc
- .babelrc.js
- babel.config.js
When a project has "type": "module" in package.json:
- .js files are treated as ES modules by Node.js
- Babel config files that use module.exports = function(api) {...} (CommonJS) must use the .cjs extension
- Metro silently falls back to the default preset, ignoring custom plugins
This causes hard-to-debug issues. In my case, the react-native-worklets/plugin wasn't being applied, resulting in runtime errors like "Failed to create a worklet" with no indication that the babel config wasn't being loaded.
Expected behavior
Metro should also search for:
- .babelrc.cjs
- babel.config.cjs
This aligns with Babel's own configuration file resolution which supports .cjs extensions.
Workaround
Use extendsBabelConfigPath in Metro config:
// metro.config.cjs
const customConfig = {
transformer: {
extendsBabelConfigPath: path.resolve(__dirname, 'babel.config.cjs'),
},
};
Environment
React Native: 0.81.5
Node.js: 24.x
pnpm monorepo with "type": "module" in package.json
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu với packages/react-native-babel-transformer/src/index.js, đặc biệt là các dòng 74-88, và lần theo quy trình tra cứu cấu hình Babel hiện có. Kiểm tra hành vi đối với các dự án mà package.json đặt type thành module, sau đó xác minh rằng .babelrc.cjs và babel.config.cjs được phát hiện và các plugin tùy chỉnh được áp dụng thay vì âm thầm quay về default preset.
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, node.js, react-native
- Lĩnh vực
- mobile, tooling
- Loại issue
- Lỗi
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức độ hoạt động
- Sôi nổi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức phù hợp với người mới
- 78/100