Skip to content

Commit 4c56684

Browse files
Telokisfatfisz
authored andcommittedAug 23, 2018
fix: Escape key of alias to support using $ prefix (#313)
Closes #312 (See #312 for detailed explanation)
1 parent 4076423 commit 4c56684

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed
 

‎src/normalizeOptions.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import findBabelConfig from 'find-babel-config';
66
import glob from 'glob';
77
import pkgUp from 'pkg-up';
88

9+
import { escapeRegExp } from './utils';
910
import defaultResolvePath from './resolvePath';
1011

1112

@@ -79,7 +80,7 @@ function normalizeRoot(optsRoot, cwd) {
7980
}
8081

8182
function getAliasTarget(key, isKeyRegExp) {
82-
const regExpPattern = isKeyRegExp ? key : `^${key}(/.*|)$`;
83+
const regExpPattern = isKeyRegExp ? key : `^${escapeRegExp(key)}(/.*|)$`;
8384
return new RegExp(regExpPattern);
8485
}
8586

‎src/utils.js

+4
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,7 @@ export function mapPathString(nodePath, state) {
8080
export function isImportCall(types, calleePath) {
8181
return types.isImport(calleePath.node.callee);
8282
}
83+
84+
export function escapeRegExp(string) {
85+
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
86+
}

‎test/index.test.js

+10
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ describe('module-resolver', () => {
342342
'^regexp-priority': './hit',
343343
'regexp-priority$': './miss',
344344
'regexp-priority': './miss',
345+
'$src': './test/testproject/src/'
345346
},
346347
}],
347348
],
@@ -466,6 +467,15 @@ describe('module-resolver', () => {
466467
);
467468
});
468469

470+
it('should escape regexp', () => {
471+
// See https://github.com/tleunen/babel-plugin-module-resolver/issues/312
472+
testWithImport(
473+
'$src/app',
474+
'./test/testproject/src/app',
475+
aliasTransformerOpts,
476+
);
477+
})
478+
469479
describe('with a regular expression', () => {
470480
it('should support replacing parts of a path', () => {
471481
testWithImport(

0 commit comments

Comments
 (0)
Please sign in to comment.