# @textpress/babel-plugin-root-import

> Babel Plugin to enable relative root-import

Latest version **5.1.2** (published 2017-09-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install @textpress/babel-plugin-root-import
pnpm add @textpress/babel-plugin-root-import
yarn add @textpress/babel-plugin-root-import
bun add @textpress/babel-plugin-root-import
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 5.1.2 |
| Published | 2017-09-01 |
| First published | 2017-09-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | agurtovoy, agolovanov |

## Links

- npm: https://www.npmjs.com/package/@textpress/babel-plugin-root-import
- Repository: https://github.com/textpress/babel-plugin-root-import
- Homepage: https://github.com/textpress/babel-plugin-root-import#readme
- Issues: https://github.com/textpress/babel-plugin-root-import/issues
- npm.io page: https://npm.io/package/@textpress/babel-plugin-root-import

## Dependencies (1)

- [slash](https://npm.io/package/slash.md) ^1.0.0

## Recent versions

- 5.1.2 (latest) — 2017-09-01
- 5.1.1 — 2017-09-01

## README

# Babel Root Import
Babel plugin to add the opportunity to use `import` and `require` with root based paths.<br>

## Example
```javascript
// Usually
import SomeExample from '../../../some/example.js';
const OtherExample = require('../../../other/example.js');

// With Babel-Root-Importer
import SomeExample from '~/some/example.js';
const OtherExample = require('~/other/example.js');
```

## Install
**npm**
```
npm install @textpress/babel-plugin-root-import --save-dev
```

**yarn**
```
yarn add @textpress/babel-plugin-root-import --dev
```

## Use
Add a `.babelrc` file and write:
```javascript
{
  "plugins": [
    ["@textpress/babel-plugin-root-import"]
  ]
}

```
or pass the plugin with the plugins-flag on CLI
```
babel-node myfile.js --plugins @textpress/babel-plugin-root-import
```

## Extras
### Custom root-path-suffix
If you want a custom root because for example all your files are in the src/js folder you can define this in your `.babelrc` file
```javascript
{
  "plugins": [
    ["@textpress/babel-plugin-root-import", {
      "rootPathSuffix": "src/js"
    }]
  ]
}
```

### Custom root-path-prefix
If you don't like the `~` syntax you can just use your own symbol (for example a `@` symbol or `\`)
```javascript
{
  "plugins": [
    ["@textpress/babel-plugin-root-import", {
      "rootPathPrefix": "@"
    }]
  ]
}

// Now you can use the plugin like:
import foo from '@/my-file';
```

### Multiple custom prefixes and suffixes
You can supply an array of the above. The plugin will try each prefix/suffix pair in the order they are defined.
```javascript
{
  "plugins": [
    ["@textpress/babel-plugin-root-import", [{
      "rootPathPrefix": "~", // `~` is the default so you can remove this if you want
      "rootPathSuffix": "src/js"
    }, {
      "rootPathPrefix": "@",
      "rootPathSuffix": "other-src/js"
    }, {
      "rootPathPrefix": "#",
      "rootPathSuffix": "../../src/in/parent" // since we suport relative paths you can also go into a parent directory
    }]]
  ]
}

// Now you can use the plugin like:
import foo from '~/my-file';
const bar = require('@/my-file');
```

### Don't let ESLint be confused
If you use [eslint-plugin-import](https://github.com/benmosher/eslint-plugin-import) to validate imports it may be necessary to instruct ESLint to parse root imports. You can use [eslint-import-resolver-babel-root-import](https://github.com/olalonde/eslint-import-resolver-babel-root-import)

```json
    "import/resolver": {
      "@textpress/babel-plugin-root-import": {}
    }
```

### Don't let Flow be confused

If you use Facebook's [Flow](https://flowtype.org/) for type-checking it is necessary to instruct it on how to map your chosen prefix to the root directory. Add the following to your `.flowconfig` file, replacing `~` with your chosen prefix and ensure that the mapping includes the corresponding suffix:
```
[options]
module.name_mapper='^{rootPathPrefix}/\(.*\)$' -> '<PROJECT_ROOT>{rootPathSuffix}/\1'
```

## FYI
Webpack delivers a similar feature, if you just want to prevent end-less import strings you can also define `aliases` in the `resolve` module, at the moment it doesn't support custom/different symbols and multiple/custom suffixes.
[READ MORE](http://xabikos.com/2015/10/03/Webpack-aliases-and-relative-paths/)

---
_Source: https://npm.io/package/@textpress/babel-plugin-root-import · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
