Skip to content

Commit

Permalink
feat: Add support for alias with array of paths (#376)
Browse files Browse the repository at this point in the history
  • Loading branch information
anchengjian authored and tleunen committed Dec 11, 2019
1 parent 77b1bc1 commit f2173ee
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/normalizeOptions.js
Expand Up @@ -90,7 +90,13 @@ function getAliasSubstitute(value, isKeyRegExp) {
}

if (!isKeyRegExp) {
return ([, match]) => `${value}${match}`;
return ([, match]) => {
// Alias with array of paths
if (Array.isArray(value)) {
return value.map(v => `${v}${match}`);
}
return `${value}${match}`;
};
}

const parts = value.split('\\\\');
Expand Down
12 changes: 12 additions & 0 deletions src/resolvePath.js
Expand Up @@ -64,6 +64,18 @@ function resolvePathFromAliasConfig(sourcePath, currentFile, opts) {
return null;
}

// Alias with array of paths
if (Array.isArray(aliasedSourceFile)) {
return aliasedSourceFile
.map(asf => {
if (isRelativePath(asf)) {
return toLocalPath(toPosixPath(mapToRelative(opts.cwd, currentFile, asf)));
}
return asf;
})
.find(src => nodeResolvePath(src, path.dirname(currentFile), opts.extensions));
}

if (isRelativePath(aliasedSourceFile)) {
return toLocalPath(toPosixPath(
mapToRelative(opts.cwd, currentFile, aliasedSourceFile)),
Expand Down
21 changes: 21 additions & 0 deletions test/index.test.js
Expand Up @@ -1046,6 +1046,27 @@ describe('module-resolver', () => {
);
});

// fix: https://github.com/tleunen/babel-plugin-module-resolver/issues/261
it('Alias with array of paths', () => {
testWithImport(
'testArr/tools',
'../test/tools',
{
babelrc: false,
plugins: [
[plugin, {
root: './src',
alias: {
testArr: ['./src', '/test', './test'],
},
cwd: 'packagejson',
}],
],
filename: './test/testproject/src/app.js',
},
);
});

describe('unknown filename', () => {
const unknownFileTransformerOpts = {
babelrc: false,
Expand Down

0 comments on commit f2173ee

Please sign in to comment.