Skip to content

Commit 2a8aca5

Browse files
author
Tommy Leunen
committed
feat: Move the babelrc lookup behind a custom cwd value option
1 parent e7083ab commit 2a8aca5

2 files changed

Lines changed: 33 additions & 22 deletions

File tree

README.md

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@ A [babel](http://babeljs.io) plugin to add a new resolver for your modules when
77

88
The reason of this plugin is to simplify the require/import paths in your project. Therefore, instead of using complex relative paths like `../../../../utils/my-utils`, you would be able to write `utils/my-utils`. It will allow you to work faster since you won't need to calculate how many levels of directory you have to go up before accessing the file.
99

10-
Here's a full example:
1110
```js
12-
// Instead of using this;
13-
import MyUtilFn from '../../../../utils/MyUtilFn';
14-
// Use that:
11+
// Use this:
1512
import MyUtilFn from 'utils/MyUtilFn';
13+
// Instead of that:
14+
import MyUtilFn from '../../../../utils/MyUtilFn';
15+
16+
// And it also work with require calls
17+
// Use this:
18+
const MyUtilFn = require('utils/MyUtilFn');
19+
// Instead of that:
20+
const MyUtilFn = require('../../../../utils/MyUtilFn');
1621
```
17-
_Note:_ It also works with `require()`, and you can alias a NPM module.
1822

1923
## Usage
2024

@@ -30,7 +34,6 @@ Specify the plugin in your `.babelrc` with the custom root or alias. Here's an e
3034
```json
3135
{
3236
"plugins": [
33-
"transform-object-rest-spread",
3437
["module-resolver", {
3538
"root": ["./src"],
3639
"alias": {
@@ -41,19 +44,21 @@ Specify the plugin in your `.babelrc` with the custom root or alias. Here's an e
4144
]
4245
}
4346
```
44-
_Note:_ All paths must be relative to the `.babelrc` files.
4547

46-
_Note 2:_ If you're using a custom extension (other than .js, .jsx, .es and .es6), you can add the `extensions` array in the config.
47-
48-
_Note 3:_ The "root" option also support a glob configuration, like `./src/**/components`.
48+
### Options
4949

50+
- `root`: Array of root directories. Specify the paths or a glob path (eg. `./src/**/components`)
51+
- `alias`: Map of alias. You can also alias node_modules dependencies, not just local files.
52+
- `extensions`: Array of extensions used in the resolver. Override the default extensions (`['.js', '.jsx', '.es', '.es6']`).
53+
- `cwd`: By default, the working directory is the one used for the resolver, but you can override it for your project.
54+
- The custom value `babelrc` will make the plugin look for the closest babelrc configuration based on the file to parse.
5055

5156
### Updating from babel-plugin-module-alias
5257

5358
babel-plugin-module-resolver is a new version of the old babel-plugin-module-alias. Therefore, you also need to make a few modifications to your plugin configuration to make it work with this new plugin.
5459

5560
Updating is very easy, so for example if you had this configuration:
56-
```
61+
```json
5762
// This configuration is outdated, this is just an example
5863
{
5964
"plugins": [

src/index.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,6 @@ export default ({ types: t }) => {
111111
}
112112

113113
return {
114-
pre(file) {
115-
const startPath = (file.opts.filename === 'unknown')
116-
? './'
117-
: file.opts.filename;
118-
119-
const { file: babelFile } = findBabelConfig.sync(startPath);
120-
this.moduleResolverCWD = babelFile
121-
? path.dirname(babelFile)
122-
: process.cwd();
123-
},
124-
125114
manipulateOptions(babelOptions) {
126115
const findPluginOptions = babelOptions.plugins.find(plugin => plugin[0] === this)[1];
127116
if (findPluginOptions.root) {
@@ -132,6 +121,23 @@ export default ({ types: t }) => {
132121
return resolvedDirs.concat(dirPath);
133122
}, []);
134123
}
124+
125+
this.customCWD = findPluginOptions.cwd;
126+
},
127+
128+
pre(file) {
129+
if (this.customCWD === 'babelrc') {
130+
const startPath = (file.opts.filename === 'unknown')
131+
? './'
132+
: file.opts.filename;
133+
134+
const { file: babelFile } = findBabelConfig.sync(startPath);
135+
this.customCWD = babelFile
136+
? path.dirname(babelFile)
137+
: null;
138+
}
139+
140+
this.moduleResolverCWD = this.customCWD || process.cwd();
135141
},
136142

137143
visitor: {

0 commit comments

Comments
 (0)