A Webpack loader that performs string or regex replacements on file contents during the build process. Version 3.3.0 is the latest stable release, with occasional minor/patch updates. It supports plain string replacement, RegExp replacement (via string with flags or RegExp object), multiple replacements in a single rule, callback-based dynamic replacement, and strict mode. Key differentiators: flexible configuration similar to String.prototype.replace, array of replacements, strict mode to ensure replacements occurred. Compared to alternatives like `replace-in-file-webpack-plugin` or `webpack-replace-loader`, this loader integrates directly into the loader chain and works per-file based on test patterns. Requires Webpack 5+ and Node.js 4+.
npm install string-replace-loaderVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic replacement of 'jQuery' with 'window.$' in all .js files.
Upgrade to Webpack 5 or use string-replace-loader@2.3.0 for Webpack 4 support.
Upgrade Node.js to v4+ or use version 1.3.0.
Set options.flags = 'g' or provide a RegExp with global flag.
Either use a RegExp object or escape special characters like '\\$' for '$'.
Order your replacements carefully or use separate rules.
Ensure the loader is applied only to files that should be replaced, using a specific test pattern. Also ensure your rules are not overlapping incorrectly.
Add a 'search' option to your loader configuration: options: { search: 'string', replace: 'newString' }Disable strict mode or ensure the search pattern exists in the matched files.
Use string-replace-loader@2.3.0 for Webpack 4 or upgrade to Webpack 5.