Skip to content

Commit

Permalink
feat: Aliasing a npm module without 'npm:' (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
tleunen authored Aug 19, 2016
1 parent 3d4756b commit 8e95988
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
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
);
});
});

0 comments on commit 8e95988

Please sign in to comment.