babel-plugin-module-resolver
Module resolver plugin for Babel
Last updated 5 months ago by tleunen .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install babel-plugin-module-resolver 
SYNC missed versions from official npm registry.

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. This plugin allows you to add new "root" directories that contain your modules. It also allows you to setup a custom alias for directories, specific files, or even other npm modules.

Description

This plugin can simplify the require/import paths in your project. For example, instead of using complex relative paths like ../../../../utils/my-utils, you can 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');

Getting started

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"
      }
    }]
  ]
}

.babelrc.js version Specify the plugin in your .babelrc.js file with the custom root or alias. Here's an example:


const plugins = [
  [
    require.resolve('babel-plugin-module-resolver'),
    {
      root: ["./src/"],
      alias: {
        "test": "./test"
      }
    }
    
  ]

];

Good example: // https://gist.github.com/nodkz/41e189ff22325a27fe6a5ca81df2cb91

Documentation

babel-plugin-module-resolver can be configured and controlled easily, check the documentation for more details

Are you a plugin author (e.g. IDE integration)? We have documented the exposed functions for use in your plugins!

ESLint plugin

If you're using ESLint, you should use eslint-plugin-import, and eslint-import-resolver-babel-module to remove falsy unresolved modules. If you want to have warnings when aliased modules are being imported by their relative paths, you can use eslint-plugin-module-resolver.

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.
  • VS Code: Configure the path mapping in jsconfig.json (tsconfig.json for TypeScript), e.g.:
{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "*": ["src/*"],
      "test/*": ["test/*"],
      "underscore": ["lodash"]
    }
  }
}

License

MIT, see LICENSE.md for details.

Who is using babel-plugin-module-resolver ?

Are you also using it? Send a PR!

Current Tags

  • 3.0.0-beta.5                                ...           beta (2 years ago)
  • 3.2.0                                ...           latest (5 months ago)

23 Versions

  • 3.2.0                                ...           5 months ago
  • 3.1.3                                ...           6 months ago
  • 3.1.2                                ...           6 months ago
  • 3.1.1                                ...           a year ago
  • 3.1.0                                ...           a year ago
  • 3.0.0                                ...           2 years ago
  • 3.0.0-beta.5                                ...           2 years ago
  • 3.0.0-beta.4                                ...           2 years ago
  • 2.7.1                                ...           2 years ago
  • 3.0.0-beta.3                                ...           2 years ago
  • 3.0.0-beta.0                                ...           2 years ago
  • 2.7.0                                ...           2 years ago
  • 2.6.2                                ...           2 years ago
  • 2.6.1                                ...           2 years ago
  • 2.6.0                                ...           2 years ago
  • 2.5.0                                ...           2 years ago
  • 2.4.0                                ...           3 years ago
  • 2.3.0                                ...           3 years ago
  • 2.2.0                                ...           3 years ago
  • 2.1.1                                ...           3 years ago
  • 2.1.0                                ...           3 years ago
  • 2.0.0                                ...           3 years ago
  • 0.0.1                                ...           3 years ago
Maintainers (1)
Downloads
Today 124
This Week 28,736
This Month 110,122
Last Day 7,015
Last Week 41,532
Last Month 151,318
Dependencies (5)
Dependents (636)

Copyright 2014 - 2016 © taobao.org |