Skip to content

Commit a857fd3

Browse files
committed
feat(baseUrl): add support baseUrl from config and options #10
1 parent d8b07ff commit a857fd3

File tree

2 files changed

+43
-27
lines changed

2 files changed

+43
-27
lines changed

packages/react-app-alias-ex/src/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ const path = require('path')
22
const paths = require('react-scripts/config/paths')
33
const {
44
aliasJest: aliasJestSafe,
5-
configFilePath,
5+
configFilePathSafe,
6+
readConfig,
67
configPathsRaw,
78
configPaths,
89
defaultOptions,
@@ -78,8 +79,9 @@ function packagePathsRaw() {
7879

7980
function expandPluginsTsChecker(plugins, configPath) {
8081
const pluginName = 'ForkTsCheckerWebpackPlugin'
81-
const confPath = configFilePath(configPath)
82-
const tsjsPaths = configPathsRaw(confPath)
82+
const confPath = configFilePathSafe(configPath)
83+
const conf = readConfig(confPath)
84+
const tsjsPaths = configPathsRaw(conf)
8385
const packagePaths = packagePathsRaw(confPath)
8486
const pluginPos = plugins
8587
.map(x => x.constructor.name)

packages/react-app-alias/src/index.js

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin')
44
const paths = require('react-scripts/config/paths')
55

66
const guessConfigFiles = [
7-
'tsconfig.paths.json',
87
'tsconfig.json',
9-
'jsconfig.paths.json',
8+
'tsconfig.paths.json',
109
'jsconfig.json',
10+
'jsconfig.paths.json',
1111
]
1212

1313
function expandResolveAlias(resolve, alias) {
@@ -71,10 +71,10 @@ function checkOutside(aliasMap) {
7171
}
7272

7373
function aliasWebpack(options) {
74-
const aliasMap = defaultOptions(options).aliasMap
74+
const {aliasMap, baseUrl} = defaultOptions(options)
7575
checkOutside(aliasMap)
7676
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])
7878
return a
7979
}, {})
8080
return function(config) {
@@ -86,8 +86,8 @@ function aliasWebpack(options) {
8686
}
8787

8888
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)
9191
return function(config) {
9292
return {
9393
...config,
@@ -123,7 +123,7 @@ function autoscan(tasks) {
123123
return aliasMap
124124
}
125125

126-
function configFilePath(configPath = '') {
126+
function configFilePathSafe(configPath = '') {
127127
if(
128128
configPath.length > 0 && fs.existsSync(path.resolve(paths.appPath, configPath))
129129
) return path.resolve(paths.appPath, configPath)
@@ -134,36 +134,43 @@ function configFilePath(configPath = '') {
134134
return existsPaths.length ? existsPaths[0] : ''
135135
}
136136

137-
function configPathsRaw(confPath) {
137+
function readConfig(confPath) {
138138
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')
140140

141141
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)}
145143

146-
const extUrl = conf.compilerOptions.extends
144+
const extUrl = conf.extends
147145
const extPath = extUrl ? path.resolve(confdir, extUrl) : ''
148-
const ext = extUrl ? require(extPath) : {}
146+
conf.extends = extUrl ? require(extPath) : {}
149147

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
150156
const extPaths = ext.compilerOptions && ext.compilerOptions.paths ?
151157
ext.compilerOptions.paths : {}
152158

153159
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`)
155161
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`)
157163

158164
return {
159165
...confPaths,
160166
...extPaths,
161167
}
162168
}
163169

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)
167174
const aliasMap = Object.keys(paths).reduce( (a, path) => {
168175
const value = paths[path]
169176
const target = Array.isArray(value) ? value[0] : value
@@ -174,13 +181,19 @@ function configPaths(configPath = '') {
174181
}
175182

176183
function defaultOptions(options) {
177-
const configPath = configFilePath(
184+
const configPath = configFilePathSafe(
178185
options.tsconfig || options.jsconfig
179186
)
180-
const aliasMap = options.alias || configPaths(configPath)
187+
const conf = readConfig(configPath)
188+
189+
const aliasMap = options.alias || configPaths(configPath, conf)
181190
const aliasAutoMap = autoscan(options.autoscan)
182191

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+
183195
const opts = {
196+
baseUrl: conf.compilerOptions.baseUrl || '.',
184197
...options,
185198
aliasMap: {
186199
...aliasAutoMap,
@@ -190,12 +203,12 @@ function defaultOptions(options) {
190203
return opts
191204
}
192205

193-
function aliasMapForJest(aliasMap) {
206+
function aliasMapForJest(baseUrl, aliasMap) {
194207
return Object.keys(aliasMap).reduce( (a, i) => {
195208
const outside = isOutsideOfRoot(aliasMap[i])
196209
const restr = i.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
197210
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`
199212
return { ...a, [alias]: targ }
200213
}, {})
201214
}
@@ -213,7 +226,8 @@ module.exports = {
213226
aliasWebpack,
214227
aliasJest,
215228
autoscan,
216-
configFilePath,
229+
configFilePathSafe,
230+
readConfig,
217231
configPathsRaw,
218232
configPaths,
219233
defaultOptions,

0 commit comments

Comments
 (0)