Skip to content

Commit 2ec645b

Browse files
authored
fix: skip compiling fabric view as react native freaks out (#720)
fixes #719
1 parent 520f7bf commit 2ec645b

File tree

7 files changed

+55
-672
lines changed

7 files changed

+55
-672
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: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@ 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 {'automatic' | 'classic'} options.jsxRuntime - Which JSX runtime to use, defaults to 'automatic'
711
*/
812
module.exports = function (api, options, cwd) {
913
const opt = (name) =>
10-
api.caller((caller) => (caller != null ? caller[name] : undefined));
14+
options[name] !== undefined
15+
? options[name]
16+
: api.caller((caller) => (caller != null ? caller[name] : undefined));
1117

1218
const supportsStaticESM = opt('supportsStaticESM');
1319
const rewriteImportExtensions = opt('rewriteImportExtensions');
@@ -47,7 +53,6 @@ module.exports = function (api, options, cwd) {
4753
require.resolve('@babel/preset-flow'),
4854
],
4955
plugins: [
50-
require.resolve('@react-native/babel-plugin-codegen'),
5156
require.resolve('@babel/plugin-transform-strict-mode'),
5257
[
5358
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/babel.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
ExportAllDeclaration,
77
ExportNamedDeclaration,
88
} from '@babel/types';
9+
import isFabricComponentFile from './utils/isFabricComponentFile';
910

1011
type Options = {
1112
/**
@@ -38,17 +39,18 @@ const isDirectory = (filename: string): boolean => {
3839
return exists;
3940
};
4041

41-
const isModule = (
42+
const checkExts = (
4243
filename: string,
4344
extension: string,
44-
platforms: string[]
45+
platforms: string[],
46+
callback: (ext: string) => boolean
4547
): boolean => {
4648
const exts = ['js', 'ts', 'jsx', 'tsx', extension];
4749

4850
return exts.some(
4951
(ext) =>
50-
isFile(`${filename}.${ext}`) &&
51-
platforms.every((platform) => !isFile(`${filename}.${platform}.${ext}`))
52+
callback(`${filename}.${ext}`) &&
53+
platforms.every((platform) => !callback(`${filename}.${platform}.${ext}`))
5254
);
5355
};
5456

@@ -112,22 +114,29 @@ export default function (
112114
node.source.value
113115
);
114116

117+
// Skip if file is a fabric view
118+
if (
119+
checkExts(filename, extension, platforms, (f) => isFabricComponentFile(f))
120+
) {
121+
return;
122+
}
123+
115124
// Replace .ts extension with .js if file with extension is explicitly imported
116125
if (isFile(filename)) {
117126
node.source.value = node.source.value.replace(/\.tsx?$/, `.${extension}`);
118127
return;
119128
}
120129

121130
// Add extension if .ts file or file with extension exists
122-
if (isModule(filename, extension, platforms)) {
131+
if (checkExts(filename, extension, platforms, isFile)) {
123132
node.source.value += `.${extension}`;
124133
return;
125134
}
126135

127136
// Expand folder imports to index and add extension
128137
if (
129138
isDirectory(filename) &&
130-
isModule(path.join(filename, 'index'), extension, platforms)
139+
checkExts(path.join(filename, 'index'), extension, platforms, isFile)
131140
) {
132141
node.source.value = node.source.value.replace(
133142
/\/?$/,

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import kleur from 'kleur';
44
import * as babel from '@babel/core';
55
import glob from 'glob';
66
import type { Input } from '../types';
7+
import isFabricComponentFile from './isFabricComponentFile';
78

89
type Options = Input & {
910
esm?: boolean;
@@ -95,6 +96,13 @@ export default async function compile({
9596
return;
9697
}
9798

99+
if (isFabricComponentFile(filepath)) {
100+
// React Native wants to handle compiling the file for fabric components
101+
// So we just copy it over as is
102+
fs.copy(filepath, path.join(output, path.relative(source, filepath)));
103+
return;
104+
}
105+
98106
const content = await fs.readFile(filepath, 'utf-8');
99107
const result = await babel.transformAsync(content, {
100108
caller: {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default function isFabricComponentFile(filepath: string): boolean {
2+
return /(?:^|[\\/])(?:Native\w+|(\w+)NativeComponent)\.[jt]sx?$/.test(
3+
filepath
4+
);
5+
}

0 commit comments

Comments
 (0)