Gathering detailed insights and metrics for string-replace-loader
Gathering detailed insights and metrics for string-replace-loader
Gathering detailed insights and metrics for string-replace-loader
Gathering detailed insights and metrics for string-replace-loader
@types/string-replace-loader
TypeScript definitions for string-replace-loader
webpack-replace-loader
A loader for replacing strings && 一个 Webpack 打包时用来替换字符串的 Loader
regexp-replace-loader
A webpack loader to replace a regexp pattern with a string
params-replace-loader
用于项目环境变量批量自动替换的webpack loader
npm install string-replace-loader
Typescript
Module System
Min. Node Version
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
252 Stars
87 Commits
56 Forks
10 Watchers
5 Branches
11 Contributors
Updated on Apr 03, 2025
Latest Version
3.2.0
Package Id
string-replace-loader@3.2.0
Unpacked Size
9.20 kB
Size
3.50 kB
File Count
8
NPM Version
10.9.0
Node Version
22.12.0
Published on
Jun 11, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
1
Perform replacements (plain and regular expression) in the contents loaded by the loader.
1$ yarn add --dev string-replace-loader
With release of 2.0.0 the loader is expected to be used in Node v4+ environment. Support for Node v3 and lower was dropped, but you can install and use the loader version of 1.3.0 in older environments.
With release of 3.0.0 the loader is expected to be used with Webpack v5+. Support for Webpack v4 and lower was dropped, but you can install and use the loader version of 2.3.0 in older environments.
Loader allows to perform replacements in a way String.prototype.replace() does (loader uses it internally).
It means that if you want to replace all occurrences, you should use RegExp-like string in options.search
with g
flag in options.flags
, etc.
Plain string replacement, no need to escape RegEx special characters.
In your webpack.config.js
:
1module.exports = { 2 // ... 3 module: { 4 rules: [ 5 { 6 test: /fileInWhichJQueryIsUndefined\.js$/, 7 loader: 'string-replace-loader', 8 options: { 9 search: '$', 10 replace: 'window.jQuery', 11 } 12 } 13 ] 14 } 15}
To achieve regular expression replacement you should either specify the search
option as
RegExp instance,
either specify it as string and add the flags
option (as an empty string if you do not want any flags).
In the latter case, search
and flags
are being passed to the
RegExp constructor
and this means that you should escape RegEx special characters in search
if you want it to be replaced as a string.
In your webpack.config.js
:
1module.exports = { 2 // ... 3 module: { 4 rules: [ 5 { 6 test: /fileInWhichJQueryIsUndefined\.js$/, 7 loader: 'string-replace-loader', 8 options: { 9 search: /\$/i, 10 replace: 'window.jQuery' 11 } 12 } 13 ] 14 } 15}
or
1module.exports = { 2 // ... 3 module: { 4 rules: [ 5 { 6 test: /fileInWhichJQueryIsUndefined\.js$/, 7 loader: 'string-replace-loader', 8 options: { 9 search: '\$', 10 replace: 'window.jQuery', 11 flags: 'i' 12 } 13 } 14 ] 15 } 16}
Also, you can pass an array of search-replace pairs this way:
In your webpack.config.js
:
1module.exports = { 2 // ... 3 module: { 4 rules: [ 5 { 6 test: /\.js$/, 7 loader: 'string-replace-loader', 8 options: { 9 multiple: [ 10 { search: 'jQuery', replace: 'window.$' }, 11 { search: '_', replace: 'window.lodash' } 12 ] 13 } 14 } 15 ] 16 } 17}
You can specify a callback function to have dynamic replacement values. The context of this function will be the context of the loader.
In your webpack.config.js
:
1module.exports = { 2 // ... 3 module: { 4 rules: [ 5 { 6 test: /\.js$/, 7 loader: 'string-replace-loader', 8 options: { 9 search: '^Hello, (.*)!$', 10 replace(match, p1, offset, string) { 11 console.log(`Replace "${match}" in file "${this.resource}".`) 12 return `Bonjour, ${p1.toUpperCase()}!` 13 }, 14 flags: 'g' 15 } 16 } 17 ] 18 } 19}
You can enable strict mode to ensure that the replacement was performed.
Loader will throw exception if nothing was replaced or if search
or replace
options were not specified.
In your webpack.config.js
:
1module.exports = { 2 // ... 3 module: { 4 rules: [ 5 { 6 test: /fileInWhichJQueryIsUndefined\.js$/, 7 loader: 'string-replace-loader', 8 options: { 9 search: 'jQuery', 10 replace: 'window.$', 11 strict: true 12 } 13 } 14 ] 15 } 16}
Feel free to open issues to propose stuff and participate. Pull requests are also welcome.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 5/11 approved changesets -- score normalized to 4
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
14 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More