Metro babel transformer doesn't recognize .cjs config files (babel.config.cjs, .babelrc.cjs)
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 78/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- javascript, node.js, react-native
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.
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
- Ngôn ngữ chính
- C++
- Star
- 127k
- Fork
- 25.3k
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
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.
Issue khác của react/react-native
-
Needs: Author Feedback Needs: Repro
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 92/100
react/react-native#58621 · 1 bình luận ·
-
Needs: Author Feedback Needs: Repro
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
react/react-native#58610 · 1 bình luận ·
-
Needs: Triage :mag:
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
react/react-native#58565 · 1 bình luận · 2 reaction ·
-
Needs: Author Feedback Needs: Repro
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
react/react-native#58555 · 5 bình luận · 2 reaction ·
-
Needs: Attention Needs: Repro
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
react/react-native#58526 · 2 bình luận ·
Tất cả issue của react/react-native
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
duckdb/duckdb-wasm#2258 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
objectionary/eo-graphs#75 ·
-
Coarray integration tests carry no LABELS, so run_tests.py silently skips them under every backend Đang mởcoarray
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
FISCO-BCOS/FISCO-BCOS#5642 ·