@@ -25,20 +25,31 @@ if (babelRootImport.default) {
25
25
}
26
26
27
27
// returns the root import config as an object
28
- function getConfigFromBabel ( start ) {
28
+ function getConfigFromBabel ( start , babelrc = '.babelrc' ) {
29
29
if ( start === '/' ) return [ ] ;
30
30
31
- const babelrc = path . join ( start , '.babelrc' ) ;
32
- if ( fs . existsSync ( babelrc ) ) {
33
- const babelrcJson = JSON5 . parse ( fs . readFileSync ( babelrc , 'utf8' ) ) ;
31
+ const packageJSONPath = path . join ( start , 'package.json' ) ;
32
+ const packageJSON = require ( packageJSONPath ) ;
33
+ const babelConfig = packageJSON . babel ;
34
+ if ( babelConfig ) {
35
+ const pluginConfig = babelConfig . plugins . find ( p => (
36
+ p [ 0 ] === 'babel-root-import'
37
+ ) ) ;
38
+ process . chdir ( path . dirname ( packageJSONPath ) ) ;
39
+ return pluginConfig [ 1 ] ;
40
+ }
41
+
42
+ const babelrcPath = path . join ( start , babelrc ) ;
43
+ if ( fs . existsSync ( babelrcPath ) ) {
44
+ const babelrcJson = JSON5 . parse ( fs . readFileSync ( babelrcPath , 'utf8' ) ) ;
34
45
if ( babelrcJson && Array . isArray ( babelrcJson . plugins ) ) {
35
46
const pluginConfig = babelrcJson . plugins . find ( p => (
36
47
p [ 0 ] === 'babel-plugin-root-import'
37
48
) ) ;
38
49
// The src path inside babelrc are from the root so we have
39
50
// to change the working directory for the same directory
40
51
// to make the mapping to work properly
41
- process . chdir ( path . dirname ( babelrc ) ) ;
52
+ process . chdir ( path . dirname ( babelrcPath ) ) ;
42
53
return pluginConfig [ 1 ] ;
43
54
}
44
55
}
@@ -54,28 +65,60 @@ exports.interfaceVersion = 2;
54
65
* @param {string } source - the module to resolve; i.e './some-module'
55
66
* @param {string } file - the importing file's full path; i.e. '/usr/local/bin/file.js'
56
67
* @param {object } config - the resolver options
68
+ * @param {string } babelrc - the name of the babelrc file
57
69
* @return {object }
58
70
*/
59
- exports . resolve = ( source , file , config ) => {
60
- const opts = getConfigFromBabel ( process . cwd ( ) ) ;
71
+ exports . resolve = ( source , file , config , babelrc ) => {
72
+ const opts = getConfigFromBabel ( process . cwd ( ) , babelrc ) ;
61
73
62
- let rootPathSuffix = '' ;
63
- let rootPathPrefix = '' ;
74
+ // [{rootPathPrefix: rootPathSuffix}]
75
+ const rootPathConfig = [ ] ;
64
76
65
- if ( opts . rootPathSuffix && typeof opts . rootPathSuffix === 'string' ) {
66
- rootPathSuffix = `/${ opts . rootPathSuffix . replace ( / ^ ( \/ ) | ( \/ ) $ / g, '' ) } ` ;
67
- }
77
+ if ( Array . isArray ( opts ) ) {
78
+ opts . forEach ( ( option ) => {
79
+ let prefix = '' ;
80
+ if ( option . rootPathPrefix && typeof option . rootPathPrefix === 'string' ) {
81
+ prefix = option . rootPathPrefix ;
82
+ }
68
83
69
- if ( opts . rootPathPrefix && typeof opts . rootPathPrefix === 'string' ) {
70
- rootPathPrefix = opts . rootPathPrefix ;
84
+ let suffix = '' ;
85
+ if ( option . rootPathSuffix && typeof option . rootPathSuffix === 'string' ) {
86
+ suffix = `/${ option . rootPathSuffix . replace ( / ^ ( \/ ) | ( \/ ) $ / g, '' ) } ` ;
87
+ }
88
+
89
+ rootPathConfig . push ( {
90
+ rootPathPrefix : prefix ,
91
+ rootPathSuffix : suffix
92
+ } ) ;
93
+ } ) ;
71
94
} else {
72
- rootPathPrefix = '~' ;
95
+ let rootPathPrefix = '~' ;
96
+ if ( opts . rootPathPrefix && typeof opts . rootPathPrefix === 'string' ) {
97
+ rootPathPrefix = opts . rootPathPrefix ;
98
+ }
99
+
100
+ let rootPathSuffix = '' ;
101
+ if ( opts . rootPathSuffix && typeof opts . rootPathSuffix === 'string' ) {
102
+ rootPathSuffix = `/${ opts . rootPathSuffix . replace ( / ^ ( \/ ) | ( \/ ) $ / g, '' ) } ` ;
103
+ }
104
+
105
+ rootPathConfig . push ( {
106
+ rootPathPrefix,
107
+ rootPathSuffix
108
+ } ) ;
73
109
}
74
110
75
111
let transformedSource = source ;
76
- if ( hasRootPathPrefixInString ( source , rootPathPrefix ) ) {
77
- transformedSource = transformRelativeToRootPath ( source , rootPathSuffix , rootPathPrefix ) ;
112
+ for ( let i = 0 ; i < rootPathConfig . length ; i += 1 ) {
113
+ const option = rootPathConfig [ i ] ;
114
+ const prefix = option . rootPathPrefix ;
115
+ const suffix = option . rootPathSuffix ;
116
+ if ( hasRootPathPrefixInString ( source , option . rootPathPrefix ) ) {
117
+ transformedSource = transformRelativeToRootPath ( source , suffix , prefix ) ;
118
+ break ;
119
+ }
78
120
}
79
121
80
122
return nodeResolve ( transformedSource , file , config ) ;
81
123
} ;
124
+
0 commit comments