Registry / web-framework / modify-source-webpack-plugin

modify-source-webpack-plugin

JSON →
library4.1.0jsnpmunverified

Webpack plugin that allows modification of module source code during compilation via hook functions. Current version 4.1.0 works with Webpack 4 and 5. It uses webpack loader mechanism internally to transform sources, supporting watch mode re-compilation. Key differentiators: cleaner API compared to using raw loaders for conditional modifications; provides access to module path and source; handles loader option serialization issues internally. Release cadence is ad-hoc with breaking changes between major versions (rejection of global variable in v4). Ships TypeScript definitions.

npm install modify-source-webpack-plugin
INSTALL
IMPORT
SIG · MODIFY-SOURCE-WEBP
M
modify-source-webpack-plugin
web-frameworkjavascriptv4.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ModifySourcePlugin
import ModifySourcePlugin from 'modify-source-webpack-plugin'
const { ModifySourcePlugin } = require('modify-source-webpack-plugin')
Default export; named import works only for type import. ESM/CJS compatible.
ModifySourcePlugin
const ModifySourcePlugin = require('modify-source-webpack-plugin')
const { default: ModifySourcePlugin } = require('modify-source-webpack-plugin')
CommonJS require returns the default export directly.
PluginOptions
import type { PluginOptions } from 'modify-source-webpack-plugin'
import { PluginOptions } from 'modify-source-webpack-plugin'
TypeScript type export; use type-only import to avoid runtime error.

Replaces all occurrences of 'foo' with 'bar' in .js files during webpack compilation.

// webpack.config.js const ModifySourcePlugin = require('modify-source-webpack-plugin'); module.exports = { plugins: [ new ModifySourcePlugin({ rules: [ { test: /\.js$/, modify: (source, path) => source.replace('foo', 'bar'), }, ], }), ], };
Debug
Known issues
breakingv4 replaces global variable approach with direct function passing via loader options. Custom loader rules referencing global variable will break.
fix
Update to v4 API: pass functions in rule.modify instead of using global variable.
affects: >=4.0.0 <5.0.0
breakingv3 changes the path argument from filename (e.g., 'index.js') to full path (e.g., '/src/index.js').
fix
Update modify functions to handle full file paths instead of just filenames.
affects: >=3.0.0 <4.0.0
deprecatedNamespace property in rules is deprecated since v4; use modify directly instead.
fix
Replace namespace: { modify: fn } with modify: fn in rule options.
affects: >=4.0.0
gotchaLoader options may be rehydrated by webpack; do not rely on object references in options.
fix
Use simple values or serialize complex data; the plugin handles internal passing.
affects: >=0.0.0
gotchaTypeScript types may not cover all edge cases; check actual API in source or GitHub issues.
fix
Consult the plugin's source code or typings file for undocumented options.
affects: >=4.0.0
Errors
Common errors & fixes
Module not found: Error: Can't resolve 'modify-source-webpack-plugin'
Package not installed or missing from dependencies.
fix
Run npm install --save-dev modify-source-webpack-plugin
TypeError: ModifySourcePlugin is not a constructor
Spawned from incorrect import; e.g., using named import with Node require.
fix
Use const ModifySourcePlugin = require('modify-source-webpack-plugin') or default import.
Error: ModifySourcePlugin: rules is not iterable
Rules option missing or not an array.
fix
Ensure rules is an array of rule objects, e.g., rules: [].
Cannot read property 'replace' of undefined
Modify function returns undefined instead of a string.
fix
Always return the modified source string from the modify function.
Upgrade
Version history
4.1.0latest on npm
Audit
Dependencies
webpackrequiredpeer dependency; requires webpack 4.37+ or 5.x
Agent activity
4 hits · last 30 days
node
4
Resources
modify-source-webpack-plugin — npm install modify-source-webpack-plugin · libregistry