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

module-replace-webpack-plugin

v0.0.12

Published

Replace any imported file/module using a simple webpack plugin

Downloads

120,799

Readme

Module Replace Webpack Plugin

A webpack plugin to replace any imported file/module before build.

License: MIT License: MIT

There are times when you would want to monkey patch a third party library, for instance lodash. Creating a forked version or putting in a PR are both sometimes either too cumbersome or not practically possible. Another possible way is to patch it and ask all devs to start referencing to lodash as

const lodash = require('./patchedLodash')

OR

import lodash from './patchedLodash'

Although this can get the job done, a more elegant way could be to let the devs import lodash the usual way, but include a plugin in the webpack build process to replace all lodash imports with your patched version.

So in other words, webpack will change all occurrences of

const lodash = require('lodash')

to

const lodash = require('./patchedLodash')

OR

import lodash from 'lodash'

to

import lodash from './patchedLodash'

when building your application.

Version

This plugin is only tested on webpack version 3.x.

Support for other webpack versions could be taken up in the future.

Installation

npm i module-replace-webpack-plugin --save-dev

Usage

const ModuleReplaceWebpackPlugin = require('module-replace-webpack-plugin');

// webpack config
{
  plugins: [
    new ModuleReplaceWebpackPlugin({options})
  ]
}

Example Webpack Config

const ModuleReplaceWebpackPlugin = require('module-replace-webpack-plugin');
const path = require('path');

module.exports = {
  entry: './index.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist')
  },
  plugins: [
    new ModuleReplaceWebpackPlugin({
      modules: [{
        test: /lodash/,
        replace: './src/patchedLodash.js'
      }],
      exclude: [/patchedLodash.js$/]
    })
  ]
}

Options

modules (Array of objects) (required)

Contains config that will be used to replace import statements during build process.

example

module: [{
  test: /lodash/,
  require: './src/patchedLodash.js'
}],

NOTE:

  • This options is an array and is a required.

test (regex)

Regex that will be used to test the import statements.

for example test: /lodash/, will match import { map } from 'lodash' and import lodash from 'lodash'

NOTE:

  • If multiple objects are provided that match the same module, the config for the first one that matches will be used.

replace (string) (required)

Contains the path to the file that will be used to replace the module with.

example require: './src/patchedLodash.js'

NOTE:

  • This path should be provided from the root directory.
  • file extension is mandatory
  • if resolve options is provided in the webpack config, this will first find the file in root and then try to find the file in paths defined under resolve.

exclude (Array of regex)

If the file that is being build has an import statement that matches the test regex, that file can be forced to not replace that import statement by including its path in the exclude setting.

In other words, mention regex for files for which you dont want this plugin to run.