1
1
/* eslint-disable import/no-commonjs */
2
2
3
3
const path = require ( 'path' ) ;
4
+ const { cosmiconfigSync } = require ( 'cosmiconfig' ) ;
5
+ const { lstatSync } = require ( 'fs' ) ;
6
+ const { name } = require ( './package.json' ) ;
4
7
5
8
/**
6
9
* Get Babel configuration for the example project.
@@ -14,22 +17,28 @@ const path = require('path');
14
17
* @returns {import('@babel/core').TransformOptions } Babel configuration
15
18
*/
16
19
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
+ } ) ;
18
24
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 ;
28
27
29
28
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
+ }
33
42
}
34
43
35
44
return {
0 commit comments