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 🙏

© 2026 – Pkg Stats / Ryan Hefner

rollup-plugin-module-replacement

v1.2.1

Published

Replace modules with Rollup

Readme

rollup-plugin-module-replacement

Build Status dependencies Status devDependencies Status Coverage Status

Replace modules when bundling packages with Rollup.

This can be useful for allowing different behaviour between builds.

Plugin can be used for simple aliasing too.

Furthermore it provides a way to chain aliasing with any resolving plugin (like rollup-plugin-node-resolve), see examples below.

Let's take a look at an example:

// rollup.config.js

import replacement from "rollup-plugin-module-replacement";

const appTarget = process.env.APP_TARGET || "VERSION_A";

export default {
  // ...
  plugins: [
    replacement({
      entries: [
        {
          find: /(.*)-APP_TARGET(\.*)/,
          replacement: importee =>
            importee.replace(/-APP_TARGET/, `-${appTarget}`)
        }
      ]
    })
  ]
};

If replacement is a String, find will be simply replaced with it. If replacement is a function, it is expected to return a String containing new path.

Plugin does not make any resolve logic under the hood. If you want files to be resolved with any plugin, see Advanced usage section below.

For Webpack users: This is a plugin to basically mimic the NormalModuleReplacementPlugin functionality in Rollup.

Installation

$ npm install --save-dev rollup-plugin-module-replacement
OR
$ yarn add -D rollup-plugin-module-replacement

Usage

// rollup.config.js
import replacement from "rollup-plugin-module-replacement";

export default {
  // ...
  plugins: [
    replacement({
      entries: [
        {
          find: /(.*)-APP_TARGET(\.*)/,
          replacement: importee =>
            importee.replace(/-APP_TARGET/, `-${appTarget}`)
        }
      ]
    })
  ]
};

The order of the entries is important, in that the first rules are applied first.

You can use either simple Strings or Regular Expressions to search in a more distinct and complex manner (e.g. to do partial replacements via subpattern-matching, see aboves example).

Advanced usage

In most situations you would like to keep preferred resolving method together with aliasing. It is a common issue with plugins like rollup-plugin-alias, because they do their resolve algorithm under the hood and it may not suit your needs.

So how to keep aliases together with your resolve algorithm like rollup-plugin-node-resolve?

It is very easy to do, see example below.

// rollup.config.js
import replacement from "rollup-plugin-module-replacement";
import resolve from "rollup-plugin-node-resolve";

const customResolver = resolve({
  extensions: [".mjs", ".js", ".jsx", ".json", ".sass", ".scss"]
});
const projectRootDir = path.resolve(__dirname);

export default {
  // ...
  plugins: [
    replacement(
      {
        entries: [
          {
            find: "src",
            replacement: path.resolve(projectRootDir, "src")
            // OR place `customResolver` here. See explanation below.
          }
        ]
      },
      customResolver
    ),
    resolve()
  ]
};

In example below we made an alias src and still keep node-resolve algorithm for your files that are "aliased" with src by passing customResolver option. Also we keep resolve() plugin separately in plugins list for other files that are not aliased with src.

customResolver option can be passed inside each entree too for granular control over resolving.

customResolver also can be your own function, not plugin.

In same manner you can chain other plugins by using rollup-plugin-module-replacement architecture.

License

MIT, see LICENSE for more information