npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

babel-plugin-replace-imports

v1.0.2

Published

Replace import statements

Downloads

22,031

Readme

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

Name | Type | Description | -------- | ------------------ | --- | test | RegExp | Regular expression is tested with the import instance. | replacer | String\|Function | A 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';