Skip to content

Commit

Permalink
feat: Move the babelrc lookup behind a custom cwd value option
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommy Leunen committed Nov 6, 2016
1 parent e7083ab commit 2a8aca5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
27 changes: 16 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ A [babel](http://babeljs.io) plugin to add a new resolver for your modules when

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.

Here's a full example:
```js
// Instead of using this;
import MyUtilFn from '../../../../utils/MyUtilFn';
// Use that:
// Use this:
import MyUtilFn from 'utils/MyUtilFn';
// Instead of that:
import MyUtilFn from '../../../../utils/MyUtilFn';

// And it also work with require calls
// Use this:
const MyUtilFn = require('utils/MyUtilFn');
// Instead of that:
const MyUtilFn = require('../../../../utils/MyUtilFn');
```
_Note:_ It also works with `require()`, and you can alias a NPM module.

## Usage

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

_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.

_Note 3:_ The "root" option also support a glob configuration, like `./src/**/components`.
### Options

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

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

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.

Updating is very easy, so for example if you had this configuration:
```
```json
// This configuration is outdated, this is just an example
{
"plugins": [
Expand Down
28 changes: 17 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,6 @@ export default ({ types: t }) => {
}

return {
pre(file) {
const startPath = (file.opts.filename === 'unknown')
? './'
: file.opts.filename;

const { file: babelFile } = findBabelConfig.sync(startPath);
this.moduleResolverCWD = babelFile
? path.dirname(babelFile)
: process.cwd();
},

manipulateOptions(babelOptions) {
const findPluginOptions = babelOptions.plugins.find(plugin => plugin[0] === this)[1];
if (findPluginOptions.root) {
Expand All @@ -132,6 +121,23 @@ export default ({ types: t }) => {
return resolvedDirs.concat(dirPath);
}, []);
}

this.customCWD = findPluginOptions.cwd;
},

pre(file) {
if (this.customCWD === 'babelrc') {
const startPath = (file.opts.filename === 'unknown')
? './'
: file.opts.filename;

const { file: babelFile } = findBabelConfig.sync(startPath);
this.customCWD = babelFile
? path.dirname(babelFile)
: null;
}

this.moduleResolverCWD = this.customCWD || process.cwd();
},

visitor: {
Expand Down

0 comments on commit 2a8aca5

Please sign in to comment.