Skip to content

Commit 3d4756b

Browse files
authored
fix: Fix root mapping with custom extensions (#72)
1 parent 20752f7 commit 3d4756b

5 files changed

Lines changed: 34 additions & 10 deletions

File tree

README.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ import MyUtilFn from '../../../../utils/MyUtilFn';
1414
// Use that:
1515
import MyUtilFn from 'utils/MyUtilFn';
1616
```
17-
18-
_Note:_ It also work for `require()`.
19-
20-
_Note 2:_ You can use the `npm:` prefix in your plugin configuration to map a node module.
21-
17+
_Note:_ It also works with `require()`, and you can alias a NPM module.
2218

2319
## Usage
2420

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

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

4743
## ESLint plugin
4844

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
"require",
2727
"import"
2828
],
29+
"dependencies": {
30+
"resolve": "^1.1.7"
31+
},
2932
"devDependencies": {
3033
"babel-cli": "^6.10.1",
3134
"babel-core": "^6.10.4",

src/index.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from 'path';
2+
import resolve from 'resolve';
23
import mapToRelative from './mapToRelative';
34

45
function createAliasFileMap(pluginOpts) {
@@ -10,6 +11,13 @@ function createAliasFileMap(pluginOpts) {
1011
), {});
1112
}
1213

14+
function replaceExt(p, ext) {
15+
const filename = path.basename(p, path.extname(p)) + ext;
16+
return path.join(path.dirname(p), filename);
17+
}
18+
19+
const defaultBabelExtensions = ['.js', '.jsx', '.es', '.es6'];
20+
1321
export function mapModule(source, file, pluginOpts) {
1422
// Do not map source starting with a dot
1523
if (source[0] === '.') {
@@ -21,9 +29,10 @@ export function mapModule(source, file, pluginOpts) {
2129
for (let i = 0; i < rootDirs.length; i++) {
2230
try {
2331
// check if the file exists (will throw if not)
24-
const p = path.resolve(rootDirs[i], source);
25-
require.resolve(p);
26-
return mapToRelative(file, p);
32+
const extensions = pluginOpts.extensions || defaultBabelExtensions;
33+
const fileAbsPath = resolve.sync(`./${source}`, { basedir: path.resolve(rootDirs[i]), extensions });
34+
// map the source and keep its extension if the import/require had one
35+
return mapToRelative(file, replaceExt(fileAbsPath, path.extname(source)));
2736
} catch (e) {
2837
// empty...
2938
}

test/examples/components/sub/sub1.css

Whitespace-only changes.

test/index.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function testRequireImport(source, output, transformerOpts) {
1919
});
2020
}
2121

22-
describe('modulesDirectories', () => {
22+
describe('root', () => {
2323
const transformerOpts = {
2424
plugins: [
2525
[plugin, {
@@ -44,6 +44,14 @@ describe('modulesDirectories', () => {
4444
);
4545
});
4646

47+
describe('should rewrite the file while keeping the extension', () => {
48+
testRequireImport(
49+
'sub/sub1.css',
50+
'./test/examples/components/sub/sub1.css',
51+
transformerOpts
52+
);
53+
});
54+
4755
describe('should not rewrite a path outisde of the root directory', () => {
4856
testRequireImport(
4957
'example-file',
@@ -104,6 +112,14 @@ describe('alias', () => {
104112
});
105113
});
106114

115+
describe('should alias the path with its extension', () => {
116+
testRequireImport(
117+
'awesome/components/my-comp.css',
118+
'./src/components/my-comp.css',
119+
transformerOpts
120+
);
121+
});
122+
107123
describe('should not alias a unknown path', () => {
108124
describe('when requiring a node module', () => {
109125
testRequireImport(

0 commit comments

Comments
 (0)