Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to use updated babel preset package name in react native 73. #78

Merged
merged 2 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Assuming you've got an existing RN project, run the following from the project r

```sh
npx sb init --type react
yarn add react-dom react-native-web babel-plugin-react-native-web @storybook/addon-react-native-web metro-react-native-babel-preset --dev
yarn add react-dom react-native-web babel-plugin-react-native-web @storybook/addon-react-native-web @react-native/babel-preset --dev
```

Then edit your `.storybook/main.js`:
Expand Down
15 changes: 11 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"devDependencies": {
"@babel/cli": "^7.20.7",
"@babel/core": "^7.20.12",
"@babel/plugin-proposal-export-namespace-from": "^7.18.9",
"@babel/preset-env": "^7.20.2",
"@babel/preset-react": "^7.18.6",
"@babel/preset-typescript": "^7.18.6",
Expand All @@ -54,7 +55,6 @@
"@storybook/react": "^7.0.0",
"@storybook/react-webpack5": "^7.0.0",
"@types/jest": "^27.4.1",
"@types/react-native": "^0.71.1",
"@types/react-native-vector-icons": "^6.4.12",
"@types/react-test-renderer": "^18.0.0",
"auto": "^10.36.5",
Expand All @@ -72,13 +72,13 @@
"eslint-plugin-storybook": "^0.6.6",
"expo-linear-gradient": "^11.4.0",
"jest": "^28.0.0",
"metro-react-native-babel-preset": "^0.74.1",
"@react-native/babel-preset": "^0.73.18",
"native-base": "^3.4.19",
"prettier": "^2.7.1",
"prop-types": "^15.8.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-native": "^0.71.1",
"react-native": "^0.73.1",
"react-native-gesture-handler": "^2.8.0",
"react-native-paper": "^4.12.5",
"react-native-reanimated": "2.14.4",
Expand All @@ -98,8 +98,9 @@
},
"peerDependencies": {
"@babel/preset-react": "*",
"babel-plugin-react-native-web": "*",
"@react-native/babel-preset": "*",
"metro-react-native-babel-preset": "*",
"babel-plugin-react-native-web": "*",
"react": "^16.8.0 || ^17.0.0 || ^18.0.0",
"react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0"
},
Expand All @@ -109,6 +110,12 @@
},
"react-dom": {
"optional": true
},
"@react-native/babel-preset": {
"optional": true
},
"metro-react-native-babel-preset": {
"optional": true
}
},
"publishConfig": {
Expand Down
29 changes: 27 additions & 2 deletions src/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,31 @@ const DEFAULT_EXCLUDES = [
'(webpack)',
];

const isInstalled = (name: string) => {
try {
require(`${name}/package.json`);
return true;
} catch (er) {
return false;
}
};

const getRnPreset = () => {
if (isInstalled('@react-native/babel-preset')) {
console.log('Using @react-native/babel-preset');

return 'module:@react-native/babel-preset';
} else if (isInstalled('metro-react-native-babel-preset')) {
console.log('Using metro-react-native-babel-preset');

return 'module:metro-react-native-babel-preset';
} else {
throw new Error(
"Couldn't find babel-preset-react-native or metro-react-native-babel-preset.",
);
}
};

const webpackFinal = async (config: any, options: Options) => {
// Add __DEV__ global variable which is relied on by many libraries
config.plugins.push(
Expand Down Expand Up @@ -90,7 +115,7 @@ const webpackFinal = async (config: any, options: Options) => {
root,
presets: [
[
'module:metro-react-native-babel-preset',
getRnPreset(),
{
useTransformReactJSXExperimental: true,
},
Expand All @@ -102,7 +127,7 @@ const webpackFinal = async (config: any, options: Options) => {
},
],
],
plugins: [...babelPlugins, '@babel/plugin-proposal-class-properties'],
plugins: [...babelPlugins],
},
});

Expand Down
Loading