oklas / react-app-alias

:label: Alias and multiple src directory for craco or rewired create-react-app
MIT License
174 stars 18 forks source link

Array of sources for an alias #98

Open danielmitchell opened 1 year ago

danielmitchell commented 1 year ago

Some of my path aliases are an array of sources, which is supported by typescript and webpack but unfortunately not by this package. Only the first source is used and the rest are ignored which breaks my build.

It would be great if this package supported an array of sources. It seems like this should be an easy fix since I was able to get it working by making a couple of changes:

function configPaths(configPath = '', confUndoc) {
    ...
    const targets = Array.isArray(value) ? value : [value]
    a[path.replace(/\/\*$/,'')] = targets.map(t => t.replace(/\/\*$/,''))
    ...
}
function aliasWebpack(options) {
  const aliasMap = defaultOptions(options).aliasMap
  const aliasLocal = Object.keys(aliasMap).reduce( (a,i) => {
    a[i] = (Array.isArray(aliasMap[i]) ? aliasMap[i] : [aliasMap[i]]).map(p => path.resolve(paths.appPath, p))
    return a
  }, {})
  ...
}
function expandPluginsScope(plugins, dirs, files) {
    ...
    plugins[pluginPos] = new ModuleScopePlugin(dirs.flat(), files.flat())
  }
}
function aliasMapForJest(baseUrl, aliasMap) {
  return Object.keys(aliasMap).reduce((a, i) => {
    const restr = i.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
    const alias = `^${restr}/(.*)$`

    const targets = Array.isArray(aliasMap[i]) ? aliasMap[i] : [aliasMap[i]]

    return {
      ...a,
      [alias]: targets.map(t =>
        isOutsideOfRoot(t) ? path.resolve(baseUrl, t) + '/$1' : `<rootDir>/${t}/$1`,
      ),
    }
  }, {})
}
oklas commented 1 year ago

Looks good. You would have done a pr right away, we would have seen that the tests pass.

oklas commented 1 year ago

Check lines length no more 80 or 100, at your second snippet a[i] = ....