Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/normalizeOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import findBabelConfig from 'find-babel-config';
import glob from 'glob';
import pkgUp from 'pkg-up';

import { escapeRegExp } from './utils';
import defaultResolvePath from './resolvePath';


Expand Down Expand Up @@ -79,7 +80,7 @@ function normalizeRoot(optsRoot, cwd) {
}

function getAliasTarget(key, isKeyRegExp) {
const regExpPattern = isKeyRegExp ? key : `^${key}(/.*|)$`;
const regExpPattern = isKeyRegExp ? key : `^${escapeRegExp(key)}(/.*|)$`;
return new RegExp(regExpPattern);
}

Expand Down
4 changes: 4 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,7 @@ export function mapPathString(nodePath, state) {
export function isImportCall(types, calleePath) {
return types.isImport(calleePath.node.callee);
}

export function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
10 changes: 10 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ describe('module-resolver', () => {
'^regexp-priority': './hit',
'regexp-priority$': './miss',
'regexp-priority': './miss',
'$src': './test/testproject/src/'
},
}],
],
Expand Down Expand Up @@ -466,6 +467,15 @@ describe('module-resolver', () => {
);
});

it('should escape regexp', () => {
// See https://github.com/tleunen/babel-plugin-module-resolver/issues/312
testWithImport(
'$src/app',
'./test/testproject/src/app',
aliasTransformerOpts,
);
})

describe('with a regular expression', () => {
it('should support replacing parts of a path', () => {
testWithImport(
Expand Down