Skip to content

Commit 930ee59

Browse files
committed
feat: process config file form extends section of tsconfig #59
1 parent c6c20f5 commit 930ee59

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"compilerOptions": {
3+
"baseUrl": ".",
4+
"extends": "./59-tsconfig.paths.json",
5+
"paths": {
6+
"alias/*": [ "target/*" ]
7+
}
8+
}
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"compilerOptions": {
3+
"baseUrl": ".",
4+
"paths": {
5+
"alias-paths/*": [ "target-paths/*" ]
6+
}
7+
}
8+
}
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
'use strict';
2-
3-
const reactAppAlias = require('../src');
2+
const path = require('path')
3+
const {configPathsRaw} = require('../src');
44

55
describe('react-app-alias', () => {
66
test.todo('tested by tests in projects in example folder');
77
});
8+
9+
describe('extends section of tsconfig on detect config file stage', () => {
10+
test('read both file and extends', () => {
11+
const paths = configPathsRaw(path.resolve(__dirname, './59-tsconfig.json'))
12+
expect(paths['alias/*'][0]).toBe('target/*');
13+
expect(paths['alias-paths/*'][0]).toBe('target-paths/*');
14+
});
15+
});
16+

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,27 @@ function configPathsRaw(confPath) {
114114
if(!confPath)
115115
throw Error('react-app-rewire-alias:configPaths: there is no config file found')
116116

117+
const confdir = path.dirname(confPath)
117118
const conf = require(confPath)
118-
const extmsg = !conf.extends ? '' : `, specify ${conf.extends} instead of ${confPath} for configPaths()`
119+
const confPaths = conf.compilerOptions && conf.compilerOptions.paths ?
120+
conf.compilerOptions.paths : {}
119121

120-
if(!conf.compilerOptions || !conf.compilerOptions.paths)
121-
return {}
122-
123-
if(typeof conf.compilerOptions.paths !== 'object')
124-
throw Error(`
125-
react-app-rewire-alias:configPaths: array expected for paths${extmsg}`)
122+
const extUrl = conf.compilerOptions.extends
123+
const extPath = extUrl ? path.resolve(confdir, extUrl) : ''
124+
const ext = extUrl ? require(extPath) : {}
125+
126+
const extPaths = ext.compilerOptions && ext.compilerOptions.paths ?
127+
ext.compilerOptions.paths : {}
126128

127-
return conf.compilerOptions.paths
129+
if(typeof confPaths !== 'object')
130+
throw Error(`react-app-alias:configPaths: '${confPath}' array expected for paths`)
131+
if(typeof extPaths !== 'object')
132+
throw Error(`react-app-alias:configPaths: '${extPath}' array expected for paths`)
133+
134+
return {
135+
...confPaths,
136+
...extPaths,
137+
}
128138
}
129139

130140
function configPaths(configPath = '') {

0 commit comments

Comments
 (0)