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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}]
]
Expand Down
8 changes: 8 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
6 changes: 0 additions & 6 deletions src/mapToRelative.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 11 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ describe('alias', () => {
alias: {
utils: './src/mylib/subfolder/utils',
'awesome/components': './src/components',
abstract: 'npm:concrete'
abstract: 'npm:concrete',
underscore: 'lodash'
}
}]
]
Expand Down Expand Up @@ -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
);
});
});