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
npm install string-replace-loader
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
250 Stars
87 Commits
57 Forks
8 Watching
5 Branches
11 Contributors
Updated on 15 Oct 2024
Minified
Minified + Gzipped
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-8.4%
45,554
Compared to previous day
Last week
2.3%
259,622
Compared to previous week
Last month
-0.3%
1,166,743
Compared to previous month
Last year
37.8%
15,250,282
Compared to previous year
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
12 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
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