react-app-alias
Advanced tools
Comparing version 1.0.0 to 1.1.0
{ | ||
"name": "react-app-alias", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Alias for craco or rewire react app", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -6,2 +6,9 @@ const fs = require('fs') | ||
const guessConfigFiles = [ | ||
'tsconfig.paths.json', | ||
'tsconfig.json', | ||
'jsconfig.paths.json', | ||
'jsconfig.json', | ||
] | ||
function expandResolveAlias(resolve, alias) { | ||
@@ -37,2 +44,5 @@ resolve.alias = Object.assign(resolve.alias || {}, alias) | ||
if(pluginPos !== -1) { | ||
const oldModuleScopePlugin = plugins[pluginPos] | ||
const allowedFiles = [...oldModuleScopePlugin.allowedFiles].filter(f => f !== paths.appPackageJson) | ||
files.push(...allowedFiles) | ||
plugins[pluginPos] = new ModuleScopePlugin(dirs, files) | ||
@@ -51,3 +61,3 @@ } | ||
console.error( | ||
`alias '${i}' is outside of root - supported only by aliasDangerous` | ||
`alias '${i}' is outside of root - supported only by react-app-alias-ex` | ||
) | ||
@@ -66,3 +76,4 @@ return true | ||
function alias(aliasMap) { | ||
function aliasWebpack(options) { | ||
const aliasMap = defaultOptions(options).aliasMap | ||
checkOutside(aliasMap) | ||
@@ -81,3 +92,4 @@ const aliasLocal = Object.keys(aliasMap).reduce( (a,i) => { | ||
function aliasJest(aliasMap) { | ||
function aliasJest(options) { | ||
const aliasMap = defaultOptions(options).aliasMap | ||
const jestAliasMap = aliasMapForJest(aliasMap) | ||
@@ -96,15 +108,10 @@ return function(config) { | ||
function configFilePath(configPath = '') { | ||
return ( | ||
configPath.length > 0 && fs.existsSync(path.resolve(paths.appPath, configPath)) ? | ||
path.resolve(paths.appPath, configPath) : | ||
fs.existsSync(path.resolve(paths.appPath, 'tsconfig.paths.json')) ? | ||
path.resolve(paths.appPath, 'tsconfig.paths.json') : | ||
fs.existsSync(path.resolve(paths.appPath, 'tsconfig.json')) ? | ||
path.resolve(paths.appPath, 'tsconfig.json') : | ||
fs.existsSync(path.resolve(paths.appPath, 'jsconfig.paths.json')) ? | ||
path.resolve(paths.appPath, 'jsconfig.paths.json') : | ||
fs.existsSync(path.resolve(paths.appPath, 'jsconfig.json')) ? | ||
path.resolve(paths.appPath, 'jsconfig.json') : | ||
'' | ||
if( | ||
configPath.length > 0 && fs.existsSync(path.resolve(paths.appPath, configPath)) | ||
) return path.resolve(paths.appPath, configPath) | ||
const existsPaths = (guessConfigFiles | ||
.map(filename => path.resolve(paths.appPath, filename)) | ||
.filter(pathname => fs.existsSync(pathname)) | ||
) | ||
return existsPaths.length ? existsPaths[0] : '' | ||
} | ||
@@ -116,13 +123,23 @@ | ||
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(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) : {} | ||
if(!conf.compilerOptions || !conf.compilerOptions.paths) | ||
return {} | ||
const extPaths = ext.compilerOptions && ext.compilerOptions.paths ? | ||
ext.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 conf.compilerOptions.paths | ||
return { | ||
...confPaths, | ||
...extPaths, | ||
} | ||
} | ||
@@ -141,2 +158,14 @@ | ||
function defaultOptions(options) { | ||
const configPath = configFilePath( | ||
options.tsconfig || options.jsconfig | ||
) | ||
const aliasMap = options.alias || configPaths(configPath) | ||
const opts = { | ||
...options, | ||
aliasMap, | ||
} | ||
return opts | ||
} | ||
function aliasMapForJest(aliasMap) { | ||
@@ -162,3 +191,3 @@ return Object.keys(aliasMap).reduce( (a, i) => { | ||
module.exports = { | ||
alias, | ||
aliasWebpack, | ||
aliasJest, | ||
@@ -168,2 +197,3 @@ configFilePath, | ||
configPaths, | ||
defaultOptions, | ||
expandResolveAlias, | ||
@@ -170,0 +200,0 @@ expandRulesInclude, |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
6461
173
3