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-pluginVerified import paths — ran on the pinned version, not inferred.
Replaces all occurrences of 'foo' with 'bar' in .js files during webpack compilation.
Update to v4 API: pass functions in rule.modify instead of using global variable.
Update modify functions to handle full file paths instead of just filenames.
Replace namespace: { modify: fn } with modify: fn in rule options.Use simple values or serialize complex data; the plugin handles internal passing.
Consult the plugin's source code or typings file for undocumented options.
Run npm install --save-dev modify-source-webpack-plugin
Use const ModifySourcePlugin = require('modify-source-webpack-plugin') or default import.Ensure rules is an array of rule objects, e.g., rules: [].
Always return the modified source string from the modify function.