Skip to content

Commit 77df6cd

Browse files
committed
fix: read source directory from bob config
1 parent be426bb commit 77df6cd

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

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

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
/* eslint-disable import/no-commonjs */
22

33
const path = require('path');
4+
const { cosmiconfigSync } = require('cosmiconfig');
5+
const { lstatSync } = require('fs');
6+
const { name } = require('./package.json');
47

58
/**
69
* Get Babel configuration for the example project.
@@ -14,22 +17,28 @@ const path = require('path');
1417
* @returns {import('@babel/core').TransformOptions} Babel configuration
1518
*/
1619
const getConfig = (defaultConfig, { root, pkg }) => {
17-
let src;
20+
const explorer = cosmiconfigSync(name, {
21+
stopDir: root,
22+
searchPlaces: ['package.json', 'bob.config.cjs', 'bob.config.js'],
23+
});
1824

19-
if (pkg.source.includes('/')) {
20-
const segments = pkg.source.split('/');
21-
22-
if (segments[0] === '.') {
23-
segments.shift();
24-
}
25-
26-
src = segments[0];
27-
}
25+
const result = explorer.search();
26+
const src = result ? result.config.source : null;
2827

2928
if (src == null) {
30-
throw new Error(
31-
"Couldn't determine the source directory. Does the 'source' field in your 'package.json' point to a file within a directory?"
32-
);
29+
if (
30+
lstatSync(path.join(root, 'bob.config.mjs'), {
31+
throwIfNoEntry: false,
32+
}).isFile()
33+
) {
34+
throw new Error(
35+
"Found a 'bob.config.mjs' file. However, ESM syntax is currently not supported for the Babel configuration."
36+
);
37+
} else {
38+
throw new Error(
39+
"Couldn't determine the source directory. Does your config specify a 'source' field?"
40+
);
41+
}
3342
}
3443

3544
return {

0 commit comments

Comments
 (0)