diff --git a/README.md b/README.md index 5e0db17..76360fe 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,8 @@ Specify the plugin in your `.babelrc` with the custom root or alias. Here's an e ["module-resolver", { "root": ["./src"], "alias": { - "test": "./test" + "test": "./test", + "underscore": "lodash" } }] ] diff --git a/src/index.js b/src/index.js index d3f3876..c41bb35 100644 --- a/src/index.js +++ b/src/index.js @@ -57,7 +57,15 @@ export function mapModule(source, file, pluginOpts) { return null; } + // remove legacy "npm:" prefix for npm packages + aliasPath = aliasPath.replace(/^(npm:)/, ''); const newPath = source.replace(moduleSplit.join('/'), aliasPath); + + // alias to npm module don't need relative mapping + if (aliasPath[0] !== '.') { + return newPath; + } + // relative alias return mapToRelative(file, newPath); } diff --git a/src/mapToRelative.js b/src/mapToRelative.js index a6b74d0..9138911 100644 --- a/src/mapToRelative.js +++ b/src/mapToRelative.js @@ -20,12 +20,6 @@ export default function mapToRelative(currentFile, module) { moduleMapped = toPosixPath(moduleMapped); - // Support npm modules instead of directories - if (moduleMapped.indexOf('npm:') !== -1) { - const [, npmModuleName] = moduleMapped.split('npm:'); - return npmModuleName; - } - if (moduleMapped[0] !== '.') moduleMapped = `./${moduleMapped}`; return moduleMapped; diff --git a/test/index.js b/test/index.js index 34b98e7..5933a50 100644 --- a/test/index.js +++ b/test/index.js @@ -68,7 +68,8 @@ describe('alias', () => { alias: { utils: './src/mylib/subfolder/utils', 'awesome/components': './src/components', - abstract: 'npm:concrete' + abstract: 'npm:concrete', + underscore: 'lodash' } }] ] @@ -138,11 +139,19 @@ describe('alias', () => { }); }); - describe('should support remapping to node modules with "npm:"', () => { + describe('(legacy) should support aliasing a node module with "npm:"', () => { testRequireImport( 'abstract/thing', 'concrete/thing', transformerOpts ); }); + + describe('should support aliasing a node modules', () => { + testRequireImport( + 'underscore/map', + 'lodash/map', + transformerOpts + ); + }); });