Skip to content

Commit

Permalink
feat: process config file form extends section of tsconfig #59
Browse files Browse the repository at this point in the history
  • Loading branch information
oklas committed Feb 25, 2022
1 parent c6c20f5 commit 930ee59
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
9 changes: 9 additions & 0 deletions packages/react-app-alias/__tests__/59-tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"baseUrl": ".",
"extends": "./59-tsconfig.paths.json",
"paths": {
"alias/*": [ "target/*" ]
}
}
}
8 changes: 8 additions & 0 deletions packages/react-app-alias/__tests__/59-tsconfig.paths.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"alias-paths/*": [ "target-paths/*" ]
}
}
}
13 changes: 11 additions & 2 deletions packages/react-app-alias/__tests__/react-app-alias.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
'use strict';

const reactAppAlias = require('../src');
const path = require('path')
const {configPathsRaw} = require('../src');

describe('react-app-alias', () => {
test.todo('tested by tests in projects in example folder');
});

describe('extends section of tsconfig on detect config file stage', () => {
test('read both file and extends', () => {
const paths = configPathsRaw(path.resolve(__dirname, './59-tsconfig.json'))
expect(paths['alias/*'][0]).toBe('target/*');
expect(paths['alias-paths/*'][0]).toBe('target-paths/*');
});
});

26 changes: 18 additions & 8 deletions packages/react-app-alias/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,27 @@ function configPathsRaw(confPath) {
if(!confPath)
throw Error('react-app-rewire-alias:configPaths: there is no config file found')

const confdir = path.dirname(confPath)
const conf = require(confPath)
const extmsg = !conf.extends ? '' : `, specify ${conf.extends} instead of ${confPath} for configPaths()`
const confPaths = conf.compilerOptions && conf.compilerOptions.paths ?
conf.compilerOptions.paths : {}

if(!conf.compilerOptions || !conf.compilerOptions.paths)
return {}

if(typeof conf.compilerOptions.paths !== 'object')
throw Error(`
react-app-rewire-alias:configPaths: array expected for paths${extmsg}`)
const extUrl = conf.compilerOptions.extends
const extPath = extUrl ? path.resolve(confdir, extUrl) : ''
const ext = extUrl ? require(extPath) : {}

const extPaths = ext.compilerOptions && ext.compilerOptions.paths ?
ext.compilerOptions.paths : {}

return conf.compilerOptions.paths
if(typeof confPaths !== 'object')
throw Error(`react-app-alias:configPaths: '${confPath}' array expected for paths`)
if(typeof extPaths !== 'object')
throw Error(`react-app-alias:configPaths: '${extPath}' array expected for paths`)

return {
...confPaths,
...extPaths,
}
}

function configPaths(configPath = '') {
Expand Down

0 comments on commit 930ee59

Please sign in to comment.