{"id":20221,"library":"modify-source-webpack-plugin","title":"Modify Source Webpack Plugin","description":"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.","status":"active","version":"4.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/artembatura/modify-source-webpack-plugin","tags":["javascript","webpack","plugin","hooks","module","file","source","modify","edit","typescript"],"install":[{"cmd":"npm install modify-source-webpack-plugin","lang":"bash","label":"npm"},{"cmd":"yarn add modify-source-webpack-plugin","lang":"bash","label":"yarn"},{"cmd":"pnpm add modify-source-webpack-plugin","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"peer dependency; requires webpack 4.37+ or 5.x","package":"webpack","optional":false}],"imports":[{"note":"Default export; named import works only for type import. ESM/CJS compatible.","wrong":"const { ModifySourcePlugin } = require('modify-source-webpack-plugin')","symbol":"ModifySourcePlugin","correct":"import ModifySourcePlugin from 'modify-source-webpack-plugin'"},{"note":"CommonJS require returns the default export directly.","wrong":"const { default: ModifySourcePlugin } = require('modify-source-webpack-plugin')","symbol":"ModifySourcePlugin","correct":"const ModifySourcePlugin = require('modify-source-webpack-plugin')"},{"note":"TypeScript type export; use type-only import to avoid runtime error.","wrong":"import { PluginOptions } from 'modify-source-webpack-plugin'","symbol":"PluginOptions","correct":"import type { PluginOptions } from 'modify-source-webpack-plugin'"}],"quickstart":{"code":"// webpack.config.js\nconst ModifySourcePlugin = require('modify-source-webpack-plugin');\n\nmodule.exports = {\n  plugins: [\n    new ModifySourcePlugin({\n      rules: [\n        {\n          test: /\\.js$/,\n          modify: (source, path) => source.replace('foo', 'bar'),\n        },\n      ],\n    }),\n  ],\n};","lang":"javascript","description":"Replaces all occurrences of 'foo' with 'bar' in .js files during webpack compilation."},"warnings":[{"fix":"Update to v4 API: pass functions in rule.modify instead of using global variable.","message":"v4 replaces global variable approach with direct function passing via loader options. Custom loader rules referencing global variable will break.","severity":"breaking","affected_versions":">=4.0.0 <5.0.0"},{"fix":"Update modify functions to handle full file paths instead of just filenames.","message":"v3 changes the path argument from filename (e.g., 'index.js') to full path (e.g., '/src/index.js').","severity":"breaking","affected_versions":">=3.0.0 <4.0.0"},{"fix":"Replace namespace: { modify: fn } with modify: fn in rule options.","message":"Namespace property in rules is deprecated since v4; use modify directly instead.","severity":"deprecated","affected_versions":">=4.0.0"},{"fix":"Use simple values or serialize complex data; the plugin handles internal passing.","message":"Loader options may be rehydrated by webpack; do not rely on object references in options.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Consult the plugin's source code or typings file for undocumented options.","message":"TypeScript types may not cover all edge cases; check actual API in source or GitHub issues.","severity":"gotcha","affected_versions":">=4.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Run npm install --save-dev modify-source-webpack-plugin","cause":"Package not installed or missing from dependencies.","error":"Module not found: Error: Can't resolve 'modify-source-webpack-plugin'"},{"fix":"Use const ModifySourcePlugin = require('modify-source-webpack-plugin') or default import.","cause":"Spawned from incorrect import; e.g., using named import with Node require.","error":"TypeError: ModifySourcePlugin is not a constructor"},{"fix":"Ensure rules is an array of rule objects, e.g., rules: [].","cause":"Rules option missing or not an array.","error":"Error: ModifySourcePlugin: rules is not iterable"},{"fix":"Always return the modified source string from the modify function.","cause":"Modify function returns undefined instead of a string.","error":"Cannot read property 'replace' of undefined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}