react-app-alias icon indicating copy to clipboard operation
react-app-alias copied to clipboard

Array of sources for an alias

Open danielmitchell opened this issue 2 years ago • 2 comments

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`,
      ),
    }
  }, {})
}

danielmitchell avatar May 04 '23 10:05 danielmitchell

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

oklas avatar May 06 '23 02:05 oklas

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

oklas avatar May 06 '23 02:05 oklas