All Projects → entwicklerstube → Babel Plugin Root Import

entwicklerstube / Babel Plugin Root Import

Licence: mit
Add the opportunity to import modules by the root path

Programming Languages

javascript
184084 projects - #8 most used programming language
es6
455 projects

Projects that are alternatives of or similar to Babel Plugin Root Import

Babel Plugin Tailwind Components
Use Tailwind with any CSS-in-JS library
Stars: ✭ 320 (-70.48%)
Mutual labels:  babel, babel-plugin
Faster.js
faster.js is a Babel plugin that compiles idiomatic Javascript to faster, micro-optimized Javascript.
Stars: ✭ 429 (-60.42%)
Mutual labels:  babel, babel-plugin
Babel Plugin React Remove Properties
Babel plugin for removing React properties. 💨
Stars: ✭ 327 (-69.83%)
Mutual labels:  babel, babel-plugin
Babel Plugin Module Resolver
Custom module resolver plugin for Babel
Stars: ✭ 3,134 (+189.11%)
Mutual labels:  babel, babel-plugin
Babel Plugin Transform React Remove Prop Types
Remove unnecessary React propTypes from the production build. 🎈
Stars: ✭ 890 (-17.9%)
Mutual labels:  babel, babel-plugin
Effectfuljs
JavaScript embedded effects compiler
Stars: ✭ 287 (-73.52%)
Mutual labels:  babel, babel-plugin
I18nize React
Internationalize react apps within a lunch break
Stars: ✭ 389 (-64.11%)
Mutual labels:  babel, babel-plugin
babel-plugin-syntax-pipeline
Allow parsing of pipeline operator |>
Stars: ✭ 23 (-97.88%)
Mutual labels:  babel, babel-plugin
Todoist Js
!! OBSOLETE !! The (un)official Todoist javascript API library
Stars: ✭ 46 (-95.76%)
Mutual labels:  babel, tdd
Htm
Hyperscript Tagged Markup: JSX alternative using standard tagged templates, with compiler support.
Stars: ✭ 7,299 (+573.34%)
Mutual labels:  babel, babel-plugin
Babel Plugin Console
Babel Plugin that adds useful build time console functions 🎮
Stars: ✭ 278 (-74.35%)
Mutual labels:  babel, babel-plugin
Xwasm
[Work In Progress] WebAssembly Packager and WASM tooling for modern frontend
Stars: ✭ 45 (-95.85%)
Mutual labels:  babel, babel-plugin
Babel Blade
(under new management!) ⛸️Solve the Double Declaration problem with inline GraphQL. Babel plugin/macro that works with any GraphQL client!
Stars: ✭ 266 (-75.46%)
Mutual labels:  babel, babel-plugin
Babel Plugin Css Modules Transform
Extract css class names from required css module files, so we can render it on server.
Stars: ✭ 318 (-70.66%)
Mutual labels:  babel, babel-plugin
Grafoo
A GraphQL Client and Toolkit
Stars: ✭ 264 (-75.65%)
Mutual labels:  babel, babel-plugin
Ava
Node.js test runner that lets you develop with confidence 🚀
Stars: ✭ 19,458 (+1695.02%)
Mutual labels:  babel, tdd
babel-plugin-transform-html-import-to-string
Turn HTML imports (and export from) into constant strings
Stars: ✭ 22 (-97.97%)
Mutual labels:  babel, babel-plugin
babel-plugin-remove-test-ids
🐠 Babel plugin to strip `data-test-id` HTML attributes
Stars: ✭ 40 (-96.31%)
Mutual labels:  babel, babel-plugin
Babel Plugin Sitrep
Log all assignments and the return value of a function with a simple comment
Stars: ✭ 442 (-59.23%)
Mutual labels:  babel, babel-plugin
Babel Plugin Styled Components
Improve the debugging experience and add server-side rendering support to styled-components
Stars: ✭ 878 (-19%)
Mutual labels:  babel, babel-plugin

Babel plugin to add the opportunity to use import and require with root based paths.
Build Status Dependency Status https://github.com/entwicklerstube/babel-plugin-root-import Codacy Badge

Example

// Without this plugin...
import SomeExample from '../../../some/example.js';
const OtherExample = require('../../../other/example.js');
import('../../../other/dynamic').then((mod) => {
  // ...
});

// With babel-plugin-root-import you can write...
import SomeExample from '~/some/example.js';
const OtherExample = require('~/other/example.js');
import('~/other/dynamic').then((mod) => {
  // ...
});

Install

Install with your package manager of choice.

npm install babel-plugin-root-import --save-dev

or

yarn add babel-plugin-root-import --dev

Use

Add it to your plugins array in your babel config, e.g. a .babelrc file.

{
  "plugins": [
    ["babel-plugin-root-import"]
  ]
}

For recent react-native versions, add it as a plugin in babel.config.js:

module.exports = (api) => {
  api.cache(true);

  return {
    plugins: ['babel-plugin-root-import'],
  };
};

For the rest of this readme, it's implied that you'll configure the plugin as above when using react-native.

Config

You can configure this plugin by changing the string plugin name to a two-item array. Note that this array is nested inside the plugins array. Here's an example with the default config.

  "plugins": [
    [
      "babel-plugin-root-import",
      {
        "rootPathSuffix": "./",
        "rootPathPrefix": "~/"
      }
    ]
  ],

Multiple rules may be specified by creating an object with { "paths": [firstItem, secondItem] }, e.g.

  "plugins": [
    [
      "babel-plugin-root-import",
      {
        "paths": [
          {
            "rootPathSuffix": "./src/components",
            "rootPathPrefix": "~/"
          },
          {
            "rootPathSuffix": "./src/utils",
            "rootPathPrefix": "!/"
          },
        ]
      }
    ]
  ],

Custom rootPathSuffix

By default, the import will be relative to the working directory of the process running babel. Typically this means you'll have import paths like ~/src/foo.js. You can change the prefix of "./" to e.g. "src" or "src/js" with this config option.

{
  "plugins": [
    ["babel-plugin-root-import", {
      "rootPathSuffix": "src/js"
    }]
  ]
}

The paths "src/js" and "./src/js" behave the same.

Custom rootPathPrefix

If you don't like the ~ syntax you can use your own symbol (for example an # symbol or \ or anything you want). Using @ is not recommended as NPM allows @ in package names. ~ is the default since it's very unlikely to conflict with anything (and wouldn't be expanded to HOME anyway).

{
  "plugins": [
    ["babel-plugin-root-import", {
      "rootPathPrefix": "#/"
    }]
  ]
}

// Now you can use the plugin like this
import foo from '#/my-file';

If you set it to e.g. "#/" then it'll require the slash in the import path.

Custom root

By default everything is resolved relative to the current working directory. You can change this with the root config option. To use it effectively, you'll need to configure babel with one of the JavaScript config file variants, rather than JSON.

For example, the following .babelrc.js file causes imports to resolve relative to the directory .babelrc.js is in.

const rootImportOpts = {
  root: __dirname,
  rootPathPrefix: '~/',
  rootPathSuffix: 'src/js',
};

module.exports = {
  plugins: [['babel-plugin-root-import', rootImportOpts]],
};

babel.config.js

const rootImportOpts = {
  root: __dirname,
  rootPathPrefix: '~/',
  rootPathSuffix: 'src/js',
};

module.exports = (api) => {
  api.cache(true);

  const plugins = [['babel-plugin-root-import', rootImportOpts]];

  return { plugins };
};

Function root variant

This .babelrc.js aliases @/foo to ./internals/foo.js since it's always relative to the file doing the import (contrived example).

const rootImportOpts = {
  root: (sourcePath) => path.dirname(sourcePath),
  rootPathPrefix: '@/',
  rootPathSuffix: 'internals',
};

module.exports = {
  plugins: [['babel-plugin-root-import', rootImportOpts]],
};

Transform paths for custom functions

If you have the need to transform paths also for other function calls you can configure them. But please be aware that this is kind of error prone because custom function names in Javascript are not static and can differ.

{
  "plugins": [
    ["babel-plugin-root-import", {
      "functions": ["jest.mock"]
    }]
  ]
}

// Now you can use the plugin also for jest.mock calls:
jest.mock('~/myfile')

Don't let ESLint be confused

If you use eslint-plugin-import to validate imports it may be necessary to instruct ESLint to parse root imports. You can use eslint-import-resolver-babel-plugin-root-import

  "settings": {
    "import/resolver": {
      "babel-plugin-root-import": {}
    }
  }

You may also specify a prefix/suffix if it doesn't correctly find your babel config.

  "settings": {
    "import/resolver": {
      "babel-plugin-root-import": {
        "rootPathPrefix": "~",
        "rootPathSuffix": "src"
      }
    }
  }

Don't let Flow be confused

If you use Facebook's Flow 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 {rootPathPrefix} with your chosen prefix (minus a trailing slash if any) and {rootPathSuffix} with your chosen suffix.

[options]
module.name_mapper='^{rootPathPrefix}/\(.*\)$' -> '<PROJECT_ROOT>/{rootPathSuffix}/\1'

Don't let VSCode be confused

For features like go-to-definition, VSCode needs to be able to resolve require/import paths to files on disk. This only works with one rootPathSuffix, but you may define multiple rootPathPrefix entries.

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "{rootPathPrefix}/*": ["src/*"]
    }
  }
}

For example, with ~/x/y.js -> ./src/x/y.js:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "~/*": ["src/*"]
    }
  }
}

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

Want to revert back to relative paths?

Sometimes tooling might not be up to scratch, meaning you lose features such as navigation in your IDE. In such cases you might want to revert back to using relative paths again. If you have a significant amount of files, it might be worth looking into tooling to help you with the conversion.

Change Log

6.6.0 - 2020-10-23

  • support template literals in dynamic import/require

6.4.1 - 2019-07-18

  • fixes unicode paths on windows

6.4.0 - 2019-07-18

  • add support for require.resolve
  • add support to configure additional require-like functions

6.3.0 - 2019-07-17

Adds 'root' config option.

6.2.0 - 2019-05-09

  • Remove the 2 characters restriction

6.1.0 - 2018-06-23

  • Supports babel 7

5.0.0 - 2017-02-10

  • More consistent name: babel-plugin-root-import #63
  • Renamed everything
  • Publish with new name on npm

4.1.5 - 2016-11-17

  • Compile new version and release again

4.1.4 - 2016-11-15

  • Improve support for relative paths (e.g. referencing parent folders via ../) (thanks to @Hizoul)

4.1.3 - 2016-09-14

  • Support paths (thanks to @sivael)

4.1.0 - 2016-08-20

  • Use relative paths instead of absolute ones (thanks to @nescalante)

4.0.0 - 2016-06-29

3.2.2 - 2016-02-20

  • Fix custom suffix in path, missing / in generated paths

3.2.0 - 2016-02-19

  • Support Windows-Filesystem
  • Add possibility to configure a custom rootPath-Symbol (instead of ~ you can use whatever you like)

3.1.0 - 2015-12-01

  • Add possibility config the custom root path

3.0.1 - 2015-11-30

  • Updated plugin to new babel6 API
  • Splitted tests and functions into two scopes with single tests
  • Removed the "extra-root" param for the .babelrc since this is no yet supported in babel6

2.0.1 - 2015-11-15

Breaking Change to Babel 5

  • Updated to Babel 6
  • Added integration tests

1.0.1 - 2015-08-07

  • Added / updated tests
  • Implemented ESlint
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].