Skip to content

Commit fac2fe2

Browse files
committed
fix: use project's codegen plugin for babel
fixes #719
1 parent 520f7bf commit fac2fe2

File tree

6 files changed

+38
-666
lines changed

6 files changed

+38
-666
lines changed

packages/react-native-builder-bob/babel-config.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,17 @@ const getConfig = (defaultConfig, { root, pkg }) => {
6161
},
6262
{
6363
include: path.join(root, src),
64-
presets: [require.resolve('./babel-preset')],
64+
presets: [
65+
[
66+
require.resolve('./babel-preset'),
67+
{
68+
// Let the app's preset handle the commonjs transform
69+
// Otherwise this causes issues with `@react-native/babel-plugin-codegen`
70+
// Codegen generates `export` statements in wrong places causing syntax error
71+
supportsStaticESM: true,
72+
},
73+
],
74+
],
6575
},
6676
],
6777
};

packages/react-native-builder-bob/babel-preset.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,21 @@ const browserslist = require('browserslist');
44

55
/**
66
* Babel preset for React Native Builder Bob
7+
*
8+
* @param {Boolean} options.supportsStaticESM - Whether to preserve ESM imports/exports, defaults to `false`
9+
* @param {Boolean} options.rewriteImportExtensions - Whether to rewrite import extensions to '.js', defaults to `false`
10+
* @param {Boolean} options.staticViewConfigsCodegen - Whether to enable the React Native codegen plugin, defaults to `false`
11+
* @param {'automatic' | 'classic'} options.jsxRuntime - Which JSX runtime to use, defaults to 'automatic'
712
*/
813
module.exports = function (api, options, cwd) {
914
const opt = (name) =>
10-
api.caller((caller) => (caller != null ? caller[name] : undefined));
15+
options[name] !== undefined
16+
? options[name]
17+
: api.caller((caller) => (caller != null ? caller[name] : undefined));
1118

1219
const supportsStaticESM = opt('supportsStaticESM');
1320
const rewriteImportExtensions = opt('rewriteImportExtensions');
21+
const staticViewConfigsCodegen = opt('staticViewConfigsCodegen');
1422
const jsxRuntime = opt('jsxRuntime');
1523

1624
return {
@@ -47,7 +55,13 @@ module.exports = function (api, options, cwd) {
4755
require.resolve('@babel/preset-flow'),
4856
],
4957
plugins: [
50-
require.resolve('@react-native/babel-plugin-codegen'),
58+
...(staticViewConfigsCodegen
59+
? [
60+
require.resolve('@react-native/babel-plugin-codegen', {
61+
paths: [cwd],
62+
}),
63+
]
64+
: []),
5165
require.resolve('@babel/plugin-transform-strict-mode'),
5266
[
5367
require.resolve('./lib/babel'),

packages/react-native-builder-bob/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
"@babel/preset-flow": "^7.24.7",
5252
"@babel/preset-react": "^7.24.7",
5353
"@babel/preset-typescript": "^7.24.7",
54-
"@react-native/babel-plugin-codegen": "^0.76.3",
5554
"babel-plugin-module-resolver": "^5.0.2",
5655
"browserslist": "^4.20.4",
5756
"cosmiconfig": "^9.0.0",

packages/react-native-builder-bob/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export type Options = {
2525
declare module '@babel/core' {
2626
export interface TransformCaller {
2727
rewriteImportExtensions: boolean;
28+
staticViewConfigsCodegen: boolean;
2829
jsxRuntime: 'automatic' | 'classic';
2930
}
3031
}

packages/react-native-builder-bob/src/utils/compile.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export default async function compile({
105105
? true
106106
: false,
107107
rewriteImportExtensions: esm,
108+
staticViewConfigsCodegen: /\bcodegenNativeComponent</.test(filepath),
108109
jsxRuntime,
109110
},
110111
cwd: root,

0 commit comments

Comments
 (0)