Skip to content

Commit

Permalink
Revert "fix: skip compiling fabric view as react native freaks out (#720
Browse files Browse the repository at this point in the history
)"

This reverts commit 2ec645b.
  • Loading branch information
satya164 committed Dec 6, 2024
1 parent c999fb2 commit c798f70
Show file tree
Hide file tree
Showing 7 changed files with 672 additions and 55 deletions.
12 changes: 1 addition & 11 deletions packages/react-native-builder-bob/babel-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,7 @@ const getConfig = (defaultConfig, { root, pkg }) => {
},
{
include: path.join(root, src),
presets: [
[
require.resolve('./babel-preset'),
{
// Let the app's preset handle the commonjs transform
// Otherwise this causes issues with `@react-native/babel-plugin-codegen`
// Codegen generates `export` statements in wrong places causing syntax error
supportsStaticESM: true,
},
],
],
presets: [require.resolve('./babel-preset')],
},
],
};
Expand Down
9 changes: 2 additions & 7 deletions packages/react-native-builder-bob/babel-preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@ const browserslist = require('browserslist');

/**
* Babel preset for React Native Builder Bob
*
* @param {Boolean} options.supportsStaticESM - Whether to preserve ESM imports/exports, defaults to `false`
* @param {Boolean} options.rewriteImportExtensions - Whether to rewrite import extensions to '.js', defaults to `false`
* @param {'automatic' | 'classic'} options.jsxRuntime - Which JSX runtime to use, defaults to 'automatic'
*/
module.exports = function (api, options, cwd) {
const opt = (name) =>
options[name] !== undefined
? options[name]
: api.caller((caller) => (caller != null ? caller[name] : undefined));
api.caller((caller) => (caller != null ? caller[name] : undefined));

const supportsStaticESM = opt('supportsStaticESM');
const rewriteImportExtensions = opt('rewriteImportExtensions');
Expand Down Expand Up @@ -53,6 +47,7 @@ module.exports = function (api, options, cwd) {
require.resolve('@babel/preset-flow'),
],
plugins: [
require.resolve('@react-native/babel-plugin-codegen'),
require.resolve('@babel/plugin-transform-strict-mode'),
[
require.resolve('./lib/babel'),
Expand Down
1 change: 1 addition & 0 deletions packages/react-native-builder-bob/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@babel/preset-flow": "^7.24.7",
"@babel/preset-react": "^7.24.7",
"@babel/preset-typescript": "^7.24.7",
"@react-native/babel-plugin-codegen": "^0.76.3",
"babel-plugin-module-resolver": "^5.0.2",
"browserslist": "^4.20.4",
"cosmiconfig": "^9.0.0",
Expand Down
21 changes: 6 additions & 15 deletions packages/react-native-builder-bob/src/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type {
ExportAllDeclaration,
ExportNamedDeclaration,
} from '@babel/types';
import isFabricComponentFile from './utils/isFabricComponentFile';

type Options = {
/**
Expand Down Expand Up @@ -39,18 +38,17 @@ const isDirectory = (filename: string): boolean => {
return exists;
};

const checkExts = (
const isModule = (
filename: string,
extension: string,
platforms: string[],
callback: (ext: string) => boolean
platforms: string[]
): boolean => {
const exts = ['js', 'ts', 'jsx', 'tsx', extension];

return exts.some(
(ext) =>
callback(`${filename}.${ext}`) &&
platforms.every((platform) => !callback(`${filename}.${platform}.${ext}`))
isFile(`${filename}.${ext}`) &&
platforms.every((platform) => !isFile(`${filename}.${platform}.${ext}`))
);
};

Expand Down Expand Up @@ -114,29 +112,22 @@ export default function (
node.source.value
);

// Skip if file is a fabric view
if (
checkExts(filename, extension, platforms, (f) => isFabricComponentFile(f))
) {
return;
}

// Replace .ts extension with .js if file with extension is explicitly imported
if (isFile(filename)) {
node.source.value = node.source.value.replace(/\.tsx?$/, `.${extension}`);
return;
}

// Add extension if .ts file or file with extension exists
if (checkExts(filename, extension, platforms, isFile)) {
if (isModule(filename, extension, platforms)) {
node.source.value += `.${extension}`;
return;
}

// Expand folder imports to index and add extension
if (
isDirectory(filename) &&
checkExts(path.join(filename, 'index'), extension, platforms, isFile)
isModule(path.join(filename, 'index'), extension, platforms)
) {
node.source.value = node.source.value.replace(
/\/?$/,
Expand Down
8 changes: 0 additions & 8 deletions packages/react-native-builder-bob/src/utils/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import kleur from 'kleur';
import * as babel from '@babel/core';
import glob from 'glob';
import type { Input } from '../types';
import isFabricComponentFile from './isFabricComponentFile';

type Options = Input & {
esm?: boolean;
Expand Down Expand Up @@ -96,13 +95,6 @@ export default async function compile({
return;
}

if (isFabricComponentFile(filepath)) {
// React Native wants to handle compiling the file for fabric components
// So we just copy it over as is
fs.copy(filepath, path.join(output, path.relative(source, filepath)));
return;
}

const content = await fs.readFile(filepath, 'utf-8');
const result = await babel.transformAsync(content, {
caller: {
Expand Down

This file was deleted.

Loading

0 comments on commit c798f70

Please sign in to comment.