@@ -4,10 +4,10 @@ const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin')
4
4
const paths = require ( 'react-scripts/config/paths' )
5
5
6
6
const guessConfigFiles = [
7
- 'tsconfig.paths.json' ,
8
7
'tsconfig.json' ,
9
- 'jsconfig .paths.json' ,
8
+ 'tsconfig .paths.json' ,
10
9
'jsconfig.json' ,
10
+ 'jsconfig.paths.json' ,
11
11
]
12
12
13
13
function expandResolveAlias ( resolve , alias ) {
@@ -71,10 +71,10 @@ function checkOutside(aliasMap) {
71
71
}
72
72
73
73
function aliasWebpack ( options ) {
74
- const aliasMap = defaultOptions ( options ) . aliasMap
74
+ const { aliasMap, baseUrl } = defaultOptions ( options )
75
75
checkOutside ( aliasMap )
76
76
const aliasLocal = Object . keys ( aliasMap ) . reduce ( ( a , i ) => {
77
- a [ i ] = path . resolve ( paths . appPath , aliasMap [ i ] )
77
+ a [ i ] = path . resolve ( paths . appPath , baseUrl , aliasMap [ i ] )
78
78
return a
79
79
} , { } )
80
80
return function ( config ) {
@@ -86,8 +86,8 @@ function aliasWebpack(options) {
86
86
}
87
87
88
88
function aliasJest ( options ) {
89
- const aliasMap = defaultOptions ( options ) . aliasMap
90
- const jestAliasMap = aliasMapForJest ( aliasMap )
89
+ const { baseUrl , aliasMap} = defaultOptions ( options )
90
+ const jestAliasMap = aliasMapForJest ( baseUrl , aliasMap )
91
91
return function ( config ) {
92
92
return {
93
93
...config ,
@@ -123,7 +123,7 @@ function autoscan(tasks) {
123
123
return aliasMap
124
124
}
125
125
126
- function configFilePath ( configPath = '' ) {
126
+ function configFilePathSafe ( configPath = '' ) {
127
127
if (
128
128
configPath . length > 0 && fs . existsSync ( path . resolve ( paths . appPath , configPath ) )
129
129
) return path . resolve ( paths . appPath , configPath )
@@ -134,36 +134,43 @@ function configFilePath(configPath = '') {
134
134
return existsPaths . length ? existsPaths [ 0 ] : ''
135
135
}
136
136
137
- function configPathsRaw ( confPath ) {
137
+ function readConfig ( confPath ) {
138
138
if ( ! confPath )
139
- throw Error ( 'react-app-alias:configPaths : there is no [ts|js]config file found' )
139
+ throw Error ( 'react-app-alias:readConfig : there is no [ts|js]config file found' )
140
140
141
141
const confdir = path . dirname ( confPath )
142
- const conf = require ( confPath )
143
- const confPaths = conf . compilerOptions && conf . compilerOptions . paths ?
144
- conf . compilerOptions . paths : { }
142
+ const conf = { ...require ( confPath ) }
145
143
146
- const extUrl = conf . compilerOptions . extends
144
+ const extUrl = conf . extends
147
145
const extPath = extUrl ? path . resolve ( confdir , extUrl ) : ''
148
- const ext = extUrl ? require ( extPath ) : { }
146
+ conf . extends = extUrl ? require ( extPath ) : { }
149
147
148
+ return conf
149
+ }
150
+
151
+ function configPathsRaw ( conf ) {
152
+ const confPaths = conf . compilerOptions && conf . compilerOptions . paths ?
153
+ conf . compilerOptions . paths : { }
154
+
155
+ const ext = conf . extends
150
156
const extPaths = ext . compilerOptions && ext . compilerOptions . paths ?
151
157
ext . compilerOptions . paths : { }
152
158
153
159
if ( typeof confPaths !== 'object' )
154
- throw Error ( `react-app-alias:configPaths: ' ${ confPath } ' array expected for paths ` )
160
+ throw Error ( `react-app-alias:configPathsRaw: compilerOptions.paths must be object ` )
155
161
if ( typeof extPaths !== 'object' )
156
- throw Error ( `react-app-alias:configPaths: ' ${ extPath } ' array expected for paths ` )
162
+ throw Error ( `react-app-alias:configPathsRaw: compilerOptions.extends->compilerOptions.paths must be object ` )
157
163
158
164
return {
159
165
...confPaths ,
160
166
...extPaths ,
161
167
}
162
168
}
163
169
164
- function configPaths ( configPath = '' ) {
165
- const confPath = configFilePath ( configPath )
166
- const paths = configPathsRaw ( confPath )
170
+ function configPaths ( configPath = '' , confUndoc ) {
171
+ const confPath = configFilePathSafe ( configPath )
172
+ const conf = confUndoc || readConfig ( confPath )
173
+ const paths = configPathsRaw ( conf )
167
174
const aliasMap = Object . keys ( paths ) . reduce ( ( a , path ) => {
168
175
const value = paths [ path ]
169
176
const target = Array . isArray ( value ) ? value [ 0 ] : value
@@ -174,13 +181,19 @@ function configPaths(configPath = '') {
174
181
}
175
182
176
183
function defaultOptions ( options ) {
177
- const configPath = configFilePath (
184
+ const configPath = configFilePathSafe (
178
185
options . tsconfig || options . jsconfig
179
186
)
180
- const aliasMap = options . alias || configPaths ( configPath )
187
+ const conf = readConfig ( configPath )
188
+
189
+ const aliasMap = options . alias || configPaths ( configPath , conf )
181
190
const aliasAutoMap = autoscan ( options . autoscan )
182
191
192
+ if ( options . autoscan )
193
+ console . warn ( 'react-app-alias: You are using experimental `autoscan` feature (https://github.com/oklas/react-app-alias/issues/70) it is not documented and may be it will be removed' )
194
+
183
195
const opts = {
196
+ baseUrl : conf . compilerOptions . baseUrl || '.' ,
184
197
...options ,
185
198
aliasMap : {
186
199
...aliasAutoMap ,
@@ -190,12 +203,12 @@ function defaultOptions(options) {
190
203
return opts
191
204
}
192
205
193
- function aliasMapForJest ( aliasMap ) {
206
+ function aliasMapForJest ( baseUrl , aliasMap ) {
194
207
return Object . keys ( aliasMap ) . reduce ( ( a , i ) => {
195
208
const outside = isOutsideOfRoot ( aliasMap [ i ] )
196
209
const restr = i . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' )
197
210
const alias = `^${ restr } /(.*)$`
198
- const targ = outside ? path . resolve ( aliasMap [ i ] ) + '/$1' : `<rootDir>/${ aliasMap [ i ] } /$1`
211
+ const targ = outside ? path . resolve ( baseUrl , aliasMap [ i ] ) + '/$1' : `<rootDir>/${ aliasMap [ i ] } /$1`
199
212
return { ...a , [ alias ] : targ }
200
213
} , { } )
201
214
}
@@ -213,7 +226,8 @@ module.exports = {
213
226
aliasWebpack,
214
227
aliasJest,
215
228
autoscan,
216
- configFilePath,
229
+ configFilePathSafe,
230
+ readConfig,
217
231
configPathsRaw,
218
232
configPaths,
219
233
defaultOptions,
0 commit comments