New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

babel-plugin-transform-import-paths

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-transform-import-paths

Babel plugin to transform import paths

latest
Source
npmnpm
Version
1.0.3
Version published
Weekly downloads
57
54.05%
Maintainers
1
Weekly downloads
 
Created
Source

Babel Plugin Transform Import Paths

Similar to Webpack's resolve.alias, this plugin transforms import, export, and require paths.

This plugin is inspired by babel-plugin-root-imports

  • Babel v7 has changed their plugin options API, which broke configuring multiple path in babel-plugin-root-imports.
  • I wanted a succinct syntax similar to Webpack's resolve.alias configuration.
  • Typescript doesn't support transforming paths during compile (despite supporting the baseUrl and paths configuration options for resolving modules). This will allow using Babel v7 to transpile Typescript and transforming paths. Or, you can transpile using Typescript and then run Babel on the Typescript-transpiled code just to transform the paths.

Install

npm

npm install --save-dev babel-plugin-transform-import-paths

yarn

yarn add --dev babel-plugin-transform-import-paths

Usage

In your .baberc or .babelrc.js file:

{
  "plugins": [
    ["babel-plugin-transform-import-paths", {
      "~": "src",
    }]
  ]
}

Example

// Before
import { Menu } from "~/components";
// After
import { Menu } from "../../../components";

Pass multiple paths

// .babelrc or .babelrc.js
{
  "plugins": [
    ["babel-plugin-transform-import-paths", {
      "~": "src",
      "_": "src/something/nested",
      "MyFiles": "src/my-files"
    }]
  ]
}

// src/some/nested/file/index.js
import Foo from "~/location-of-foo";
import Bar from "_/location-of-bar";
import Baz from "MyFiles";

// After
import Foo from "../../location-of-foo";
import Bar from "../../something/nested/location-of-bar";
import Baz from "../../my-files";

Keywords

babel

FAQs

Package last updated on 15 May 2020

Did you know?

Socket

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