Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-replace-imports

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-replace-imports

Replace import statements

  • 1.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5.7K
increased by28.83%
Maintainers
1
Weekly downloads
 
Created
Source

babel-plugin-replace-imports

Replaces import statements.

Installation

npm install --save-dev babel-plugin-replace-imports

Usage

WARNING: this plugin is used only with options.

Via .babelrc

Add the following line to your .babelrc file:

{
  "plugins": [
    ["babel-plugin-replace-imports", {
      "test": /\/?regexp\//i,
      "replacer": "string"
    }]
  ]
}

Using in conjunction with the babel-loader

You can pass options to the babel loader by using the options property (see the babel options):

module: {
  rules: [
    {
      test: /\.js$/,
      use: {
        loader: "babel-loader",
        options: {
          plugins: ["babel-plugin-replace-imports", {
            "test": /\/?regexp\//i,
            "replacer": "string"
          }]
        }
      }
    }
  ]
}

Options

NameTypeDescription
testRegExpRegular expression is tested with the import instance.
replacerString|FunctionA string that replaces the substring specified by the specified test parameter. A number of special replacement patterns are supported (see String.prototype.replace()).
A function to be invoked to create the new substring to be used to replace the matches to the given regexp of the test parameter. The arguments supplied to this function are described in the String.prototype.replace().

Examples

Example 1.

Options:

{
  "plugins": [
    ["babel-plugin-replace-imports", {
      "test": /\.styl$/i,
      "replacer": "$&?theme-red"
    }]
  ]
}

In:

import 'reset.styl';
import 'reset';
import './views';
import '../styles.styl';
import styl from '../stylus/common.styl';

Out:

import 'reset.styl?theme-red';
import 'reset';
import './views';
import '../styles.styl?theme-red';
import styl from '../stylus/common.styl?theme-red';

Example 2.

Options:

{
  "plugins": [
    ["babel-plugin-replace-imports", {
      "test": /\.styl$/i,
      "replacer": [
        "$&?theme-red",
        "$&?theme-green",
        "$&?theme-blue"
      ]
    }]
  ]
}

In:

import 'reset.styl';
import 'reset';
import './views';
import '../styles.styl';
import styl from '../stylus/common.styl';

Out:

import 'reset.styl?theme-red';
import 'reset.styl?theme-green';
import 'reset.styl?theme-blue';
import 'reset';
import './views';
import '../styles.styl?theme-red';
import '../styles.styl?theme-green';
import '../styles.styl?theme-blue';
import styl from '../stylus/common.styl?theme-red';
import styl from '../stylus/common.styl?theme-green';
import styl from '../stylus/common.styl?theme-blue';

Example 3.

WARNING: only one replacement rule is processed. It depends on order of the option list.

Options:

{
  "plugins": [
    ["babel-plugin-replace-imports", [
      {
        "test": /\/stylus\//,
        "replacer": "/sass/"
      }, {
        "test": /\.styl$/i,
        "replacer": "$&?theme-red"
      }
    ]]
  ]
}

In:

import 'reset.styl';
import 'reset';
import './views';
import '../styles.styl';
import styl from '../stylus/common.styl';

Out: For each import statement was done only one replacement rule.

import 'reset.styl?theme-red';
import 'reset';
import './views';
import '../styles.styl?theme-red';
import styl from '../sass/common.styl';

Keywords

FAQs

Package last updated on 15 Jan 2018

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc