Skip to content

Commit f2173ee

Browse files
anchengjiantleunen
authored andcommitted
feat: Add support for alias with array of paths (#376)
1 parent 77b1bc1 commit f2173ee

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

src/normalizeOptions.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,13 @@ function getAliasSubstitute(value, isKeyRegExp) {
9090
}
9191

9292
if (!isKeyRegExp) {
93-
return ([, match]) => `${value}${match}`;
93+
return ([, match]) => {
94+
// Alias with array of paths
95+
if (Array.isArray(value)) {
96+
return value.map(v => `${v}${match}`);
97+
}
98+
return `${value}${match}`;
99+
};
94100
}
95101

96102
const parts = value.split('\\\\');

src/resolvePath.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ function resolvePathFromAliasConfig(sourcePath, currentFile, opts) {
6464
return null;
6565
}
6666

67+
// Alias with array of paths
68+
if (Array.isArray(aliasedSourceFile)) {
69+
return aliasedSourceFile
70+
.map(asf => {
71+
if (isRelativePath(asf)) {
72+
return toLocalPath(toPosixPath(mapToRelative(opts.cwd, currentFile, asf)));
73+
}
74+
return asf;
75+
})
76+
.find(src => nodeResolvePath(src, path.dirname(currentFile), opts.extensions));
77+
}
78+
6779
if (isRelativePath(aliasedSourceFile)) {
6880
return toLocalPath(toPosixPath(
6981
mapToRelative(opts.cwd, currentFile, aliasedSourceFile)),

test/index.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,27 @@ describe('module-resolver', () => {
10461046
);
10471047
});
10481048

1049+
// fix: https://github.com/tleunen/babel-plugin-module-resolver/issues/261
1050+
it('Alias with array of paths', () => {
1051+
testWithImport(
1052+
'testArr/tools',
1053+
'../test/tools',
1054+
{
1055+
babelrc: false,
1056+
plugins: [
1057+
[plugin, {
1058+
root: './src',
1059+
alias: {
1060+
testArr: ['./src', '/test', './test'],
1061+
},
1062+
cwd: 'packagejson',
1063+
}],
1064+
],
1065+
filename: './test/testproject/src/app.js',
1066+
},
1067+
);
1068+
});
1069+
10491070
describe('unknown filename', () => {
10501071
const unknownFileTransformerOpts = {
10511072
babelrc: false,

0 commit comments

Comments
 (0)