Skip to content

Commit 3a4e724

Browse files
authored
feat: use the bob preset for the library during dev (#599)
Previously the React Native Babel preset was being used t compile the library code as well. However, this preset differs from the preset used to publish the library which can introduce inconsistencies. For example, Metro compiles modules in loose mode which can hide strict mode errors during dev, but they'll surface when consumer uses the library. With this change, we make sure that the same preset is used for dev to avoid the inconsistency. It also adds a babel plugin to explicitly add `use strict` - as Metro doesn't use strict mode for ESM code which is not spec-compliant, and the behavior differs from the CommonJS build where we add `use strict` during publish.
1 parent 5b45554 commit 3a4e724

File tree

8 files changed

+86
-43
lines changed

8 files changed

+86
-43
lines changed

packages/create-react-native-library/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,7 @@ async function create(_argv: yargs.Arguments<any>) {
743743
dest: folder,
744744
arch,
745745
project: options.project,
746+
bobVersion,
746747
reactNativeVersion,
747748
});
748749
}

packages/create-react-native-library/src/utils/generateExampleApp.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ const PACKAGES_TO_REMOVE = [
3535
'typescript',
3636
];
3737

38-
const PACKAGES_TO_ADD_DEV = {
39-
'babel-plugin-module-resolver': '^5.0.0',
40-
};
41-
4238
const PACKAGES_TO_ADD_WEB = {
4339
'@expo/metro-runtime': '~3.2.1',
4440
'react-dom': '18.2.0',
@@ -50,6 +46,7 @@ export default async function generateExampleApp({
5046
dest,
5147
arch,
5248
project,
49+
bobVersion,
5350
reactNativeVersion = 'latest',
5451
}: {
5552
type: ExampleType;
@@ -60,6 +57,7 @@ export default async function generateExampleApp({
6057
name: string;
6158
package: string;
6259
};
60+
bobVersion: string;
6361
reactNativeVersion?: string;
6462
}) {
6563
const directory = path.join(dest, 'example');
@@ -188,6 +186,11 @@ export default async function generateExampleApp({
188186
delete devDependencies[name];
189187
});
190188

189+
const PACKAGES_TO_ADD_DEV = {
190+
'babel-plugin-module-resolver': '^5.0.0',
191+
'react-native-builder-bob': `^${bobVersion}`,
192+
};
193+
191194
Object.assign(devDependencies, PACKAGES_TO_ADD_DEV);
192195

193196
if (type === 'expo') {
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module.exports = {
2-
presets: ['module:@react-native/babel-preset'],
2+
presets: [
3+
['module:react-native-builder-bob/babel-preset', { modules: 'commonjs' }],
4+
],
35
};
Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,40 @@
11
const path = require('path');
22
const pak = require('../package.json');
33

4+
const root = path.resolve(__dirname, '..');
5+
46
module.exports = function (api) {
57
api.cache(true);
68

79
return {
8-
presets: ['babel-preset-expo'],
9-
plugins: [
10-
[
11-
'module-resolver',
12-
{
13-
extensions: ['.tsx', '.ts', '.js', '.json'],
14-
alias: {
15-
// For development, we want to alias the library to the source
16-
[pak.name]: path.join(__dirname, '..', pak.source),
17-
},
18-
},
19-
],
10+
overrides: [
11+
{
12+
exclude: path.join(root, 'src'),
13+
presets: ['babel-preset-expo'],
14+
},
15+
{
16+
include: path.join(root, 'src'),
17+
presets: [
18+
[
19+
'module:react-native-builder-bob/babel-preset',
20+
{ modules: 'commonjs' },
21+
],
22+
],
23+
},
24+
{
25+
exclude: /\/node_modules\//,
26+
plugins: [
27+
[
28+
'module-resolver',
29+
{
30+
extensions: ['.tsx', '.ts', '.jsx', '.js', '.json'],
31+
alias: {
32+
[pak.name]: path.join(root, pak.source),
33+
},
34+
},
35+
],
36+
],
37+
},
2038
],
2139
};
2240
};
Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,36 @@
11
const path = require('path');
22
const pak = require('../package.json');
33

4+
const root = path.resolve(__dirname, '..');
5+
46
module.exports = {
5-
presets: ['module:@react-native/babel-preset'],
6-
plugins: [
7-
[
8-
'module-resolver',
9-
{
10-
extensions: ['.tsx', '.ts', '.js', '.json'],
11-
alias: {
12-
[pak.name]: path.join(__dirname, '..', pak.source),
13-
},
14-
},
15-
],
7+
overrides: [
8+
{
9+
exclude: path.join(root, 'src'),
10+
presets: ['module:@react-native/babel-preset'],
11+
},
12+
{
13+
include: path.join(root, 'src'),
14+
presets: [
15+
[
16+
'module:react-native-builder-bob/babel-preset',
17+
{ modules: 'commonjs' },
18+
],
19+
],
20+
},
21+
{
22+
exclude: /\/node_modules\//,
23+
plugins: [
24+
[
25+
'module-resolver',
26+
{
27+
extensions: ['.tsx', '.ts', '.jsx', '.js', '.json'],
28+
alias: {
29+
[pak.name]: path.join(root, pak.source),
30+
},
31+
},
32+
],
33+
],
34+
},
1635
],
1736
};

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ module.exports = function (api, options, cwd) {
4444
require.resolve('@babel/preset-flow'),
4545
],
4646
plugins: [
47+
require.resolve('@babel/plugin-transform-strict-mode'),
4748
[
4849
require.resolve('./lib/babel'),
4950
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
},
4545
"dependencies": {
4646
"@babel/core": "^7.18.5",
47-
"@babel/plugin-proposal-class-properties": "^7.17.12",
47+
"@babel/plugin-transform-strict-mode": "^7.24.7",
4848
"@babel/preset-env": "^7.18.2",
4949
"@babel/preset-flow": "^7.17.12",
5050
"@babel/preset-react": "^7.17.12",

yarn.lock

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ __metadata:
197197
languageName: node
198198
linkType: hard
199199

200-
"@babel/helper-create-class-features-plugin@npm:^7.18.6, @babel/helper-create-class-features-plugin@npm:^7.22.11, @babel/helper-create-class-features-plugin@npm:^7.22.15, @babel/helper-create-class-features-plugin@npm:^7.22.5":
200+
"@babel/helper-create-class-features-plugin@npm:^7.22.11, @babel/helper-create-class-features-plugin@npm:^7.22.15, @babel/helper-create-class-features-plugin@npm:^7.22.5":
201201
version: 7.22.15
202202
resolution: "@babel/helper-create-class-features-plugin@npm:7.22.15"
203203
dependencies:
@@ -590,18 +590,6 @@ __metadata:
590590
languageName: node
591591
linkType: hard
592592

593-
"@babel/plugin-proposal-class-properties@npm:^7.17.12":
594-
version: 7.18.6
595-
resolution: "@babel/plugin-proposal-class-properties@npm:7.18.6"
596-
dependencies:
597-
"@babel/helper-create-class-features-plugin": ^7.18.6
598-
"@babel/helper-plugin-utils": ^7.18.6
599-
peerDependencies:
600-
"@babel/core": ^7.0.0-0
601-
checksum: 49a78a2773ec0db56e915d9797e44fd079ab8a9b2e1716e0df07c92532f2c65d76aeda9543883916b8e0ff13606afeffa67c5b93d05b607bc87653ad18a91422
602-
languageName: node
603-
linkType: hard
604-
605593
"@babel/plugin-proposal-private-property-in-object@npm:7.21.0-placeholder-for-preset-env.2":
606594
version: 7.21.0-placeholder-for-preset-env.2
607595
resolution: "@babel/plugin-proposal-private-property-in-object@npm:7.21.0-placeholder-for-preset-env.2"
@@ -1448,6 +1436,17 @@ __metadata:
14481436
languageName: node
14491437
linkType: hard
14501438

1439+
"@babel/plugin-transform-strict-mode@npm:^7.24.7":
1440+
version: 7.24.7
1441+
resolution: "@babel/plugin-transform-strict-mode@npm:7.24.7"
1442+
dependencies:
1443+
"@babel/helper-plugin-utils": ^7.24.7
1444+
peerDependencies:
1445+
"@babel/core": ^7.0.0-0
1446+
checksum: 514e847f6ba4bac825eb0e9ae697c6ae632041d887f6cc1167f9cb989924b2f8735ec040607a02083c3a03e62f1c8973ba59a2b106ca3b55c2e2c416b51e2372
1447+
languageName: node
1448+
linkType: hard
1449+
14511450
"@babel/plugin-transform-template-literals@npm:^7.22.5":
14521451
version: 7.22.5
14531452
resolution: "@babel/plugin-transform-template-literals@npm:7.22.5"
@@ -12827,7 +12826,7 @@ __metadata:
1282712826
dependencies:
1282812827
"@babel/cli": ^7.17.10
1282912828
"@babel/core": ^7.18.5
12830-
"@babel/plugin-proposal-class-properties": ^7.17.12
12829+
"@babel/plugin-transform-strict-mode": ^7.24.7
1283112830
"@babel/preset-env": ^7.18.2
1283212831
"@babel/preset-flow": ^7.17.12
1283312832
"@babel/preset-react": ^7.17.12

0 commit comments

Comments
 (0)