Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
module-replace-webpack-plugin
Advanced tools
Replace any imported file/module using a simple webpack plugin
A webpack plugin to replace any imported file/module before build.
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.
This plugin is only tested on webpack version 3.x.
Support for other webpack versions could be taken up in the future.
npm i module-replace-webpack-plugin --save-dev
const ModuleReplaceWebpackPlugin = require('module-replace-webpack-plugin');
// webpack config
{
plugins: [
new ModuleReplaceWebpackPlugin({options})
]
}
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$/]
})
]
}
Contains config that will be used to replace import statements during build process.
example
module: [{
test: /lodash/,
require: './src/patchedLodash.js'
}],
NOTE:
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:
Contains the path to the file that will be used to replace the module with.
example require: './src/patchedLodash.js'
NOTE:
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
.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.
FAQs
Replace any imported file/module using a simple webpack plugin
The npm package module-replace-webpack-plugin receives a total of 2,633 weekly downloads. As such, module-replace-webpack-plugin popularity was classified as popular.
We found that module-replace-webpack-plugin demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.