react-app-alias
Advanced tools
Comparing version 2.1.0 to 2.2.0
{ | ||
"name": "react-app-alias", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Alias for craco or rewire react app", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -7,6 +7,6 @@ const fs = require('fs') | ||
const guessConfigFiles = [ | ||
'tsconfig.json', | ||
'tsconfig.paths.json', | ||
'tsconfig.json', | ||
'jsconfig.json', | ||
'jsconfig.paths.json', | ||
'jsconfig.json', | ||
] | ||
@@ -75,6 +75,6 @@ | ||
function aliasWebpack(options) { | ||
const aliasMap = defaultOptions(options).aliasMap | ||
const {aliasMap, baseUrl} = defaultOptions(options) | ||
checkOutside(aliasMap) | ||
const aliasLocal = Object.keys(aliasMap).reduce( (a,i) => { | ||
a[i] = path.resolve(paths.appPath, aliasMap[i]) | ||
a[i] = path.resolve(paths.appPath, baseUrl, aliasMap[i]) | ||
return a | ||
@@ -91,4 +91,4 @@ }, {}) | ||
function aliasJest(options) { | ||
const aliasMap = defaultOptions(options).aliasMap | ||
const jestAliasMap = aliasMapForJest(aliasMap) | ||
const {baseUrl, aliasMap} = defaultOptions(options) | ||
const jestAliasMap = aliasMapForJest(baseUrl, aliasMap) | ||
return function(config) { | ||
@@ -129,3 +129,3 @@ return { | ||
function configFilePath(configPath = '') { | ||
function configFilePathSafe(configPath = '') { | ||
if( | ||
@@ -141,15 +141,21 @@ configPath.length > 0 && fs.existsSync(path.resolve(paths.appPath, configPath)) | ||
function configPathsRaw(confPath) { | ||
function readConfig(confPath) { | ||
if(!confPath) | ||
throw Error('react-app-alias:configPaths: there is no [ts|js]config file found') | ||
throw Error('react-app-alias:readConfig: there is no [ts|js]config file found') | ||
const confdir = path.dirname(confPath) | ||
const conf = require(confPath) | ||
const conf = {...require(confPath)} | ||
const extUrl = conf.extends | ||
const extPath = extUrl ? path.resolve(confdir, extUrl) : '' | ||
conf.extends = extUrl ? require(extPath) : {} | ||
return conf | ||
} | ||
function configPathsRaw(conf) { | ||
const confPaths = conf.compilerOptions && conf.compilerOptions.paths ? | ||
conf.compilerOptions.paths : {} | ||
const extUrl = conf.compilerOptions.extends | ||
const extPath = extUrl ? path.resolve(confdir, extUrl) : '' | ||
const ext = extUrl ? require(extPath) : {} | ||
const ext = conf.extends || {} | ||
const extPaths = ext.compilerOptions && ext.compilerOptions.paths ? | ||
@@ -159,6 +165,6 @@ ext.compilerOptions.paths : {} | ||
if(typeof confPaths !== 'object') | ||
throw Error(`react-app-alias:configPaths: '${confPath}' array expected for paths`) | ||
throw Error(`react-app-alias:configPathsRaw: compilerOptions.paths must be object`) | ||
if(typeof extPaths !== 'object') | ||
throw Error(`react-app-alias:configPaths: '${extPath}' array expected for paths`) | ||
throw Error(`react-app-alias:configPathsRaw: compilerOptions.extends->compilerOptions.paths must be object`) | ||
return { | ||
@@ -170,5 +176,6 @@ ...confPaths, | ||
function configPaths(configPath = '') { | ||
const confPath = configFilePath(configPath) | ||
const paths = configPathsRaw(confPath) | ||
function configPaths(configPath = '', confUndoc) { | ||
const confPath = configFilePathSafe(configPath) | ||
const conf = confUndoc || readConfig(confPath) | ||
const paths = configPathsRaw(conf) | ||
const aliasMap = Object.keys(paths).reduce( (a, path) => { | ||
@@ -184,9 +191,15 @@ const value = paths[path] | ||
function defaultOptions(options) { | ||
const configPath = configFilePath( | ||
const configPath = configFilePathSafe( | ||
options.tsconfig || options.jsconfig | ||
) | ||
const aliasMap = options.alias || configPaths(configPath) | ||
const conf = readConfig(configPath) | ||
const aliasMap = options.alias || configPaths(configPath, conf) | ||
const aliasAutoMap = autoscan(options.autoscan) | ||
if(options.autoscan) | ||
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') | ||
const opts = { | ||
baseUrl: conf.compilerOptions.baseUrl || '.', | ||
...options, | ||
@@ -201,3 +214,3 @@ aliasMap: { | ||
function aliasMapForJest(aliasMap) { | ||
function aliasMapForJest(baseUrl, aliasMap) { | ||
return Object.keys(aliasMap).reduce( (a, i) => { | ||
@@ -207,3 +220,3 @@ const outside = isOutsideOfRoot(aliasMap[i]) | ||
const alias = `^${restr}/(.*)$` | ||
const targ = outside ? path.resolve(aliasMap[i])+'/$1' : `<rootDir>/${aliasMap[i]}/$1` | ||
const targ = outside ? path.resolve(baseUrl, aliasMap[i])+'/$1' : `<rootDir>/${aliasMap[i]}/$1` | ||
return { ...a, [alias]: targ } | ||
@@ -226,3 +239,4 @@ }, {}) | ||
autoscan, | ||
configFilePath, | ||
configFilePathSafe, | ||
readConfig, | ||
configPathsRaw, | ||
@@ -229,0 +243,0 @@ configPaths, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7732
212