Socket
Socket
Sign inDemoInstall

babel-plugin-module-resolverino

Package Overview
Dependencies
25
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    babel-plugin-module-resolverino

Module resolver plugin for Babel


Version published
Weekly downloads
2
increased by100%
Maintainers
1
Created
Weekly downloads
 

Changelog

Source

1.0.0 (2017-04-01)

Bug Fixes

  • Fix double plugin bug (#3) (944a002)
  • Use CWD for the glob patterns (#4) (1fcd018)

Features

BREAKING CHANGES

  • "npm:" support is dropped.
  • package-like aliases are additionally resolved using root and cwd.
  • relative aliases are left as-is to be consistent with how getRealPath treats relative paths.

<a name="0.0.1"></a>

Readme

Source

babel-plugin-module-resolverino

Greenkeeper badge 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.

Fork notice

This plugin is a fork of the awesome babel-plugin-module-resolver plugin. Without it there would be no resolverino.

The differences are mainly some additional features and bugfixes. The configuration that worked in babel-plugin-module-resolver should mostly work under babel-plugin-module-resolverino (small adjustments may be required).

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

Usage

Install the plugin

$ npm install --save-dev babel-plugin-module-resolverino

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.

Regular expression alias

It is possible to specify an alias using a regular expression. To do that, either start an alias with '^' or end it with '$':

{
  "plugins": [
    ["module-resolver", {
      "alias": {
        "^@namespace/foo-(.+)": "packages/\\1"
      }
    }]
  ]
}

Using the config from this example '@namespace/foo-bar' will become 'packages/bar'.

You can reference the n-th matched group with '\\n' ('\\0' refers to the whole matched path).

To use the backslash character (\) just escape it like so: '\\\\' (double escape is needed because of JSON already using \ for escaping).

Usage with Flow

To allow Flow to find your modules, add configuration options to .flowconfig.

For example, a React component is located at src/components/Component.js

// Before
import '../../src/components/Component';

// After - Flow cannot find this now
import 'components/Component';

Instruct Flow where to resolve modules from:

# .flowconfig

[options]
module.system.node.resolve_dirname=node_modules
module.system.node.resolve_dirname=src

Be sure to add any sub-directories if you refer to files further down the directory tree:

// Located at src/store/actions
import 'actions/User'
module.system.node.resolve_dirname=src/store

More configuration options are located in the Flow documentation

Editors autocompletion

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

Keywords

FAQs

Last updated on 01 Apr 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc