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
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ import MyUtilFn from '../../../../utils/MyUtilFn';
// Use that:
import MyUtilFn from 'utils/MyUtilFn';
```

_Note:_ It also work for `require()`.

_Note 2:_ You can use the `npm:` prefix in your plugin configuration to map a node module.

_Note:_ It also works with `require()`, and you can alias a NPM module.

## Usage

Expand All @@ -28,7 +24,6 @@ Install the plugin
$ npm install --save-dev babel-plugin-module-resolver
```


Specify the plugin in your `.babelrc` with the custom root or alias. Here's an example:
```json
{
Expand All @@ -43,6 +38,7 @@ Specify the plugin in your `.babelrc` with the custom root or alias. Here's an e
]
}
```
_Note:_ If you're using a custom extension (other than .js, .jsx, .es and .es6), you can add the `extensions` array in the config.

## ESLint plugin

Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
"require",
"import"
],
"dependencies": {
"resolve": "^1.1.7"
},
"devDependencies": {
"babel-cli": "^6.10.1",
"babel-core": "^6.10.4",
Expand Down
15 changes: 12 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path';
import resolve from 'resolve';
import mapToRelative from './mapToRelative';

function createAliasFileMap(pluginOpts) {
Expand All @@ -10,6 +11,13 @@ function createAliasFileMap(pluginOpts) {
), {});
}

function replaceExt(p, ext) {
const filename = path.basename(p, path.extname(p)) + ext;
return path.join(path.dirname(p), filename);
}

const defaultBabelExtensions = ['.js', '.jsx', '.es', '.es6'];

export function mapModule(source, file, pluginOpts) {
// Do not map source starting with a dot
if (source[0] === '.') {
Expand All @@ -21,9 +29,10 @@ export function mapModule(source, file, pluginOpts) {
for (let i = 0; i < rootDirs.length; i++) {
try {
// check if the file exists (will throw if not)
const p = path.resolve(rootDirs[i], source);
require.resolve(p);
return mapToRelative(file, p);
const extensions = pluginOpts.extensions || defaultBabelExtensions;
const fileAbsPath = resolve.sync(`./${source}`, { basedir: path.resolve(rootDirs[i]), extensions });
// map the source and keep its extension if the import/require had one
return mapToRelative(file, replaceExt(fileAbsPath, path.extname(source)));
} catch (e) {
// empty...
}
Expand Down
Empty file.
18 changes: 17 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function testRequireImport(source, output, transformerOpts) {
});
}

describe('modulesDirectories', () => {
describe('root', () => {
const transformerOpts = {
plugins: [
[plugin, {
Expand All @@ -44,6 +44,14 @@ describe('modulesDirectories', () => {
);
});

describe('should rewrite the file while keeping the extension', () => {
testRequireImport(
'sub/sub1.css',
'./test/examples/components/sub/sub1.css',
transformerOpts
);
});

describe('should not rewrite a path outisde of the root directory', () => {
testRequireImport(
'example-file',
Expand Down Expand Up @@ -104,6 +112,14 @@ describe('alias', () => {
});
});

describe('should alias the path with its extension', () => {
testRequireImport(
'awesome/components/my-comp.css',
'./src/components/my-comp.css',
transformerOpts
);
});

describe('should not alias a unknown path', () => {
describe('when requiring a node module', () => {
testRequireImport(
Expand Down