GT2/GT2-iOS/node_modules/babel-plugin-module-resolver
Ren Ren Juan 30eef7d036 correct tree err 2018-02-12 17:26:06 +00:00
..
lib correct tree err 2018-02-12 17:26:06 +00:00
node_modules/glob correct tree err 2018-02-12 17:26:06 +00:00
CHANGELOG.md correct tree err 2018-02-12 17:26:06 +00:00
LICENSE.md correct tree err 2018-02-12 17:26:06 +00:00
README.md correct tree err 2018-02-12 17:26:06 +00:00
package.json correct tree err 2018-02-12 17:26:06 +00:00

README.md

babel-plugin-module-resolver

Maintenance Status NPM version Build Status Linux Build Status Windows Coverage Status

A babel plugin to add a new resolver for your modules when compiling your code using Babel. The plugin allows you to add new "root" directories that contains your modules. It also allows you to setup custom alias which can also be directories or specific files, or even other npm modules.

Description

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.

// 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');

Usage

If you're coming from babel-plugin-module-alias, please read this section: Updating from babel-plugin-module-alias.

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:

{
  "plugins": [
    ["module-resolver", {
      "root": ["./src"],
      "alias": {
        "test": "./test",
        "underscore": "lodash"
      }
    }]
  ]
}

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:

// This configuration is outdated, this is just an example
{
  "plugins": [
    ["module-alias", [
      { "src": "./src/utils", "expose": "utils" },
      { "src": "./src/components", "expose": "components" },
      { "src": "./src/actions", "expose": "actions" },
      { "src": "npm:lodash", "expose": "underscore" }
    ]]
  ]
}

You ony have to update the plugin options to be like this:

{
  "plugins": [
      ["module-resolver", {
        "root": ["./src"],
        "alias": {
          "underscore": "lodash"
        }
      }]
    ]
}

ESLint plugin

If you're using ESLint, you should use the eslint-plugin-import, and this eslint-import-resolver-babel-module in order to remove falsy unresolved modules.

Editors autocompletion

  • Atom: Uses atom-autocomplete-modules and enable the babel-plugin-module-resolver option.
  • IntelliJ/WebStorm: You can add custom resources root directories, make sure it matches what you have in this plugin.

License

MIT, see LICENSE.md for details.