Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a9f2ede

Browse files
authoredDec 4, 2024··
feat: pass options to babel-preset using callers (#704)
This removes options parameter from the babel preset and passed them using callers. With this change, Bob can automatically pass relevant parameters to the preset when building without the user to do anything additional if the chose to override the config. **BREAKING CHANGE** After this change, consumers can't provide options to the preset directly.
1 parent e52ba57 commit a9f2ede

File tree

4 files changed

+28
-23
lines changed

4 files changed

+28
-23
lines changed
 
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
module.exports = {
2-
presets: [
3-
['module:react-native-builder-bob/babel-preset', { modules: 'commonjs' }],
4-
],
2+
presets: ['module:react-native-builder-bob/babel-preset'],
53
};

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ const browserslist = require('browserslist');
44

55
/**
66
* Babel preset for React Native Builder Bob
7-
* @param {'commonjs' | 'preserve'} options.modules - Whether to compile modules to CommonJS or preserve them
8-
* @param {Boolean} options.esm - Whether to output ES module compatible code, e.g. by adding extension to import/export statements
9-
* @param {'automatic' | 'classic'} options.jsxRuntime - Which JSX runtime to use, defaults to 'automatic'
107
*/
118
module.exports = function (api, options, cwd) {
12-
const cjs = options.modules === 'commonjs';
9+
const opt = (name) =>
10+
api.caller((caller) => (caller != null ? caller[name] : undefined));
11+
12+
const supportsStaticESM = opt('supportsStaticESM');
13+
const rewriteImportExtensions = opt('rewriteImportExtensions');
14+
const jsxRuntime = opt('jsxRuntime');
1315

1416
return {
1517
presets: [
@@ -32,14 +34,13 @@ module.exports = function (api, options, cwd) {
3234
node: '18',
3335
},
3436
useBuiltIns: false,
35-
modules: cjs ? 'commonjs' : false,
37+
modules: supportsStaticESM ? false : 'commonjs',
3638
},
3739
],
3840
[
3941
require.resolve('@babel/preset-react'),
4042
{
41-
runtime:
42-
options.jsxRuntime !== undefined ? options.jsxRuntime : 'automatic',
43+
runtime: jsxRuntime !== undefined ? jsxRuntime : 'automatic',
4344
},
4445
],
4546
require.resolve('@babel/preset-typescript'),
@@ -50,7 +51,7 @@ module.exports = function (api, options, cwd) {
5051
[
5152
require.resolve('./lib/babel'),
5253
{
53-
extension: options.esm ? 'js' : undefined,
54+
extension: rewriteImportExtensions ? 'js' : undefined,
5455
},
5556
],
5657
],

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,10 @@ export type Options = {
2121
targets?: (Target | [target: Target, options: object])[];
2222
exclude?: string;
2323
};
24+
25+
declare module '@babel/core' {
26+
export interface TransformCaller {
27+
rewriteImportExtensions: boolean;
28+
jsxRuntime: 'automatic' | 'classic';
29+
}
30+
}

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,16 @@ export default async function compile({
9797

9898
const content = await fs.readFile(filepath, 'utf-8');
9999
const result = await babel.transformAsync(content, {
100+
caller: {
101+
name: 'react-native-builder-bob',
102+
supportsStaticESM:
103+
/\.m[jt]s$/.test(filepath) || // If a file is explicitly marked as ESM, then preserve the syntax
104+
modules === 'preserve'
105+
? true
106+
: false,
107+
rewriteImportExtensions: esm,
108+
jsxRuntime,
109+
},
100110
cwd: root,
101111
babelrc: babelrc,
102112
configFile: configFile,
@@ -107,18 +117,7 @@ export default async function compile({
107117
...(babelrc || configFile
108118
? null
109119
: {
110-
presets: [
111-
[
112-
require.resolve('../../babel-preset'),
113-
{
114-
modules:
115-
// If a file is explicitly marked as ESM, then preserve the syntax
116-
/\.m[jt]s$/.test(filepath) ? 'preserve' : modules,
117-
esm,
118-
jsxRuntime,
119-
},
120-
],
121-
],
120+
presets: [require.resolve('../../babel-preset')],
122121
}),
123122
});
124123

0 commit comments

Comments
 (0)
Please sign in to comment.