Skip to content

Commit 8e95988

Browse files
authored
feat: Aliasing a npm module without 'npm:' (#73)
1 parent 3d4756b commit 8e95988

4 files changed

Lines changed: 21 additions & 9 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ Specify the plugin in your `.babelrc` with the custom root or alias. Here's an e
3232
["module-resolver", {
3333
"root": ["./src"],
3434
"alias": {
35-
"test": "./test"
35+
"test": "./test",
36+
"underscore": "lodash"
3637
}
3738
}]
3839
]

src/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,15 @@ export function mapModule(source, file, pluginOpts) {
5757
return null;
5858
}
5959

60+
// remove legacy "npm:" prefix for npm packages
61+
aliasPath = aliasPath.replace(/^(npm:)/, '');
6062
const newPath = source.replace(moduleSplit.join('/'), aliasPath);
63+
64+
// alias to npm module don't need relative mapping
65+
if (aliasPath[0] !== '.') {
66+
return newPath;
67+
}
68+
// relative alias
6169
return mapToRelative(file, newPath);
6270
}
6371

src/mapToRelative.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ export default function mapToRelative(currentFile, module) {
2020

2121
moduleMapped = toPosixPath(moduleMapped);
2222

23-
// Support npm modules instead of directories
24-
if (moduleMapped.indexOf('npm:') !== -1) {
25-
const [, npmModuleName] = moduleMapped.split('npm:');
26-
return npmModuleName;
27-
}
28-
2923
if (moduleMapped[0] !== '.') moduleMapped = `./${moduleMapped}`;
3024

3125
return moduleMapped;

test/index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ describe('alias', () => {
6868
alias: {
6969
utils: './src/mylib/subfolder/utils',
7070
'awesome/components': './src/components',
71-
abstract: 'npm:concrete'
71+
abstract: 'npm:concrete',
72+
underscore: 'lodash'
7273
}
7374
}]
7475
]
@@ -138,11 +139,19 @@ describe('alias', () => {
138139
});
139140
});
140141

141-
describe('should support remapping to node modules with "npm:"', () => {
142+
describe('(legacy) should support aliasing a node module with "npm:"', () => {
142143
testRequireImport(
143144
'abstract/thing',
144145
'concrete/thing',
145146
transformerOpts
146147
);
147148
});
149+
150+
describe('should support aliasing a node modules', () => {
151+
testRequireImport(
152+
'underscore/map',
153+
'lodash/map',
154+
transformerOpts
155+
);
156+
});
148157
});

0 commit comments

Comments
 (0)