Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

babel-plugin-module-alias

tleunen20.3kMITdeprecated1.6.0

WARNING: This project has been renamed to babel-plugin-module-resolver. Install babel-plugin-module-resolver for new features

Babel plugin to rewrite the path in require() and ES6 import

babel, babel-plugin, module, alias, rewrite, resolve, rename, mapping, require, import

readme

babel-plugin-module-alias Build Status Coverage Status

A babel plugin to rewrite (map, alias, resolve) directories as different directories during the Babel process. It's particularly useful when you have files you don't want to use with relative paths (especially in big projects).

Description

Instead of using relative paths in your project, you'll be able to use an alias. Here an simple example:

// Instead of using this;
import MyUtilFn from '../../../../utils/MyUtilFn';
// Use that:
import MyUtilFn from 'utils/MyUtilFn';

With this plugin, you'll be able to map files or directories to the path you want.

Note: It also work for require().

Note 2: You can use the npm: prefix in your plugin configuration to map a node module.

Usage

Install the plugin

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

Specify the plugin in your .babelrc with the custom mapping.

{
  "plugins": [
    ["module-alias", [
      { "src": "./src/utils", "expose": "utils" },
      { "src": "./src/components", "expose": "awesome/components" },
      { "src": "npm:lodash", "expose": "underscore" }
    ]]
  ]
}

If you're using eslint-plugin-import, you should use eslint-import-resolver-babel-module-alias to avoid having false errors.

License

MIT, see LICENSE.md for details.

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

5.0.2 (2024-04-18)

Bug Fixes

  • Revert reselect update to fix usage in node 16 (#446) (8923c48)

5.0.1 (2024-04-17)

5.0.0 (2023-01-09)

⚠ BREAKING CHANGES

  • Remove support for Node < 16

  • Update dependencies and find-babel-config to fix json5 vulnerabilities (#441) (c43e71c)

4.1.0 (2020-12-15)

Features

4.0.0 (2019-12-11)

⚠ BREAKING CHANGES

  • The minimum node.js version that is now officially supported is Node 8.

Features

  • Add support for alias with array of paths (#376) (f2173ee)

3.2.0 (2019-02-11)

Features

  • dev: add loglevel to configure console logging (#351) (d4a596c)

3.1.3 (2019-01-21)

3.1.2 (2019-01-16)

Bug Fixes

3.1.1 (2018-03-24)

Bug Fixes

3.1.0 (2018-02-08)

Bug Fixes

  • Fix path transformation for dot files that are siblings (#253) (7dc2da6)

Features

  • Ability to declare aliases in an array to preserve the order (#243) (d03715d)
  • Add stripExtensions option to determine which extensions get removed (#247) (fdf5da9)
  • Allow using a function to define the result of an alias (#245) (9299d9a)
  • run plugin also on Program exit to handle dynamically added imports from other transforms (#269) (12a2d07)

3.0.0 (2017-11-08)

Features

  • Add jest.setMock to the list of transformed jest functions (#225) (a7c04b0)
  • Add require.requireActual and require.requireMock to the list of transformed Jest functions (#227) (47fd5ba)
  • Make alias config take precedence over root config (#221) (3c711f8)

BREAKING CHANGES

  • This makes alias taking precedence over the root config because it's usually more specific.

3.0.0-beta.5 (2017-08-17)

Bug Fixes

3.0.0-beta.4 (2017-07-10)

Bug Fixes

  • Prevent double application of the transform (#176) (feefe9e)

Features

3.0.0-beta.3 (2017-05-16)

3.0.0-beta.2 (2017-05-16)

Bug Fixes

  • package: update pkg-up to version 2.0.0 (#168) (ccba432)
  • Fix path resolution when the alias is not a proper sub path (#177) (dcd5dca)

Features

  • Add the .mjs extension to the default extensions (#172) (ea9e457)
  • Run the plugin in the "enter" phase of the Program (#179) (c4794f9)

3.0.0-beta.0 (2017-04-23)

Feat

  • warn when the package from resolved alias is not available (#160) (c937f79)

Features

  • Add support for new ES dynamic import() (#143) (ad65935)
  • Convert root to array if root is a string (#153) (2bcea0c), closes #152
  • Support 'packagejson' as a custom cwd option (#149) (52600af)

BREAKING CHANGES

  • The "npm:" prefix has been removed.

2.7.0 (2017-03-30)

Reverts

  • Force a 2.7.0 by reverting all changes since 2.5.0 (577afad)

2.6.2 (2017-03-30)

Bug Fixes

2.6.1 (2017-03-29)

Bug Fixes

  • Fix regExps.find undefined error (2171200)

2.6.0 (2017-03-29)

Bug Fixes

Features

2.5.0 (2017-02-05)

Bug Fixes

  • Add only directories in a glob root config (#120) (ac87924)
  • Remove unnecessary trailing /index in the local path resolution (#117) (ac0c671)

Features

  • Add support for jest doMock and dontMock functions (#114) (0bc01ec)
  • Export manipulateOptions (85270e1)
  • Export the default extensions used by the plugin (1c4fbcc)

2.4.0 (2016-11-30)

Features

2.3.0 (2016-11-06)

Bug Fixes

  • Fix wrong location of modified path (e7083ab), closes #94

Features

  • Move the babelrc lookup behind a custom cwd value option (2a8aca5)

2.2.0 (2016-08-27)

Bug Fixes

  • Fix plugin on windows (d6b0a4b)
  • Fix the root resolver when both a file and directory have the same name (a4cff68)

Features

  • Add glob support in the root config (#78) (1f6245b)

2.1.1 (2016-08-22)

Bug Fixes

  • Fix resolver with filename containing a dot (#75) (bb6c903), closes #74

2.1.0 (2016-08-19)

Bug Fixes

  • Fix root mapping with custom extensions (#72) (3d4756b)

Features

  • Aliasing a npm module without 'npm:' (#73) (8e95988)

2.0.0 (2016-08-14)

Features

  • Add support for custom root directories (#69) (3dd29a0), closes #46

BREAKING CHANGES

  • There's a new way to specify alias in the plugin options. See README for more info