babel-plugin-root-require

1.3.0 • Public • Published

babel-plugin-root-require

Transforms require module paths that have a prefix referring to the project root. Also works on require.resolve.

install

npm install babel-plugin-root-require

example

file structure

  • src (project root)
    • foo
      • a.js
    • bar
      • b.js
      • baz
        • c.js
    • d.js

javascript

// these paths will work from any file in the project root (src)
require('~/foo/a')
require('~/bar/b')
require('~/bar/baz/c')
require('~/d')
 
// foo/a.js
require('~/d') // -> require('../d')

.babelrc

{
  "plugins": [
    ["root-require", {
      "projectRoot": "src"
    }]
  ]
}

terminal

babel src --out-dir build

options

prefix = '~'

Prefix that refers to the project root

projectRoot = process.cwd()

Absolute path of project root or relative path from process.cwd() to the project root which prefix refers to. Module paths will not be resolved correctly if this option is not correct. In the typical case that the source code is under the src directory, but babel is executed in the parent directory of src, set projectRoot: src.

long = false

By default, transformed module paths will be the most efficient path from the requiring file to the module. When long is true, the prefix will be transformed to the relative path up the tree to the project root and the rest of the path is untouched. In some cases, the module path will be longer this way, because the path goes up the tree to the project root and then back down the tree to the module.

Dependencies (1)

Dev Dependencies (5)

Package Sidebar

Install

npm i babel-plugin-root-require

Weekly Downloads

5

Version

1.3.0

License

CC0-1.0

Last publish

Collaborators

  • m59