Gathering detailed insights and metrics for file-replace-loader
Gathering detailed insights and metrics for file-replace-loader
Gathering detailed insights and metrics for file-replace-loader
Gathering detailed insights and metrics for file-replace-loader
replace-loader
Webpack loader that applies regex to file
string-replace-file-loader
Replace loader for Webpack
text-replace-file-loader
This loader calls file-loader on all require statements in a document and replaces those instances with the public file path
replace-css-url-loader
Webpack plugin to transform download cdn urls in css and replaced with local file path
npm install file-replace-loader
Typescript
Module System
Node Version
NPM Version
77.6
Supply Chain
99.5
Quality
77.4
Maintenance
100
Vulnerability
99.6
License
JavaScript (100%)
Total Downloads
1,011,976
Last Day
856
Last Week
6,463
Last Month
22,476
Last Year
273,467
11 Stars
71 Commits
4 Forks
15 Branches
2 Contributors
Latest Version
1.4.2
Package Id
file-replace-loader@1.4.2
Unpacked Size
28.12 kB
Size
8.48 kB
File Count
9
NPM Version
8.19.4
Node Version
16.20.2
Publised On
24 Apr 2024
Cumulative downloads
Total Downloads
Last day
1,140.6%
856
Compared to previous day
Last week
16.3%
6,463
Compared to previous week
Last month
-3%
22,476
Compared to previous month
Last year
-5.7%
273,467
Compared to previous year
2
2
file-replace-loader is webpack loader that allows you replace files in compile time by some condition.
file-replace-loader is free and will always remain free
A simple and quick way to support the project is to buy me a coffee.
It will take no more than 5 minutes and will allow the project to keep going
npm install --save-dev file-replace-loader
yarn add file-replace-loader
1const { resolve } = require('path'); 2 3module.exports = { 4 //... 5 module: { 6 rules: [{ 7 test: /\.config\.js$/, 8 loader: 'file-replace-loader', 9 options: { 10 condition: 'if-replacement-exists', 11 replacement: resolve('./config.local.js'), 12 async: true, 13 } 14 }] 15 } 16}
This example rule replaces all of imports /\.config.js$/
to config.local.js
file,
if replacement exists (condition if-replacement-exists
).
After this build a bundle file will contain code from config.local.js
and original sources
won't changed.
To describe replace rules for two or more files you can use function as replacement value.
How does it work?
test
rule, include
and exclude
rule options;replacement
option. If it is string then the loader just replace a file. If it is a function
then file-replace-loader checking what it returns. If the function returns a path to file then the loader
replaces, if returns nothing then current match skips.replacement
function returns a path then file-replace-loader looks to condition
. If condition is always
then it replace every match. If condition
is
if-replacement-exists
then loader checking existing file, etc;For example:
1const { resolve } = require('path'); 2 3module.exports = { 4 //... 5 module: { 6 rules: [{ 7 test: /\.js$/, 8 loader: 'file-replace-loader', 9 options: { 10 condition: 'always', // <-- Note that the rule applies for all files! 11 replacement(resourcePath) { 12 if (resourcePath.endsWith('foo.js')) { 13 return resolve('./bar.js'); 14 } 15 if (resourcePath.endsWith('foo-a.js')) { 16 return resolve('./bar-a.js'); 17 } 18 }, 19 async: true, 20 } 21 }] 22 } 23}
file-replace-loader passes to replacement
function resourcePath
for every matching.
file-replace-loader doesn't care what developer does with this path but if repalcement
function returns a new path then file-replace-loader replaces file.
If replacement
function returns nothing then file-replace-loading skip replace for current resourcePath
.
Example with mapping:
1const { resolve } = require('path'); 2 3module.exports = { 4 //... 5 module: { 6 rules: [{ 7 test: /\.js$/, 8 loader: 'file-replace-loader', 9 options: { 10 condition: 'always', // <-- Note that the rule applies for all files! But you can use other conditions too 11 replacement(resourcePath) { 12 const mapping = { 13 [resolve('./src/foo-a.js')]: resolve('./src/bar-a.js'), 14 [resolve('./src/foo-b.js')]: resolve('./src/bar-b.js'), 15 [resolve('./src/foo-c.js')]: resolve('./src/bar-c.js'), 16 }; 17 return mapping[resourcePath]; 18 }, 19 async: true, 20 } 21 }] 22 } 23}
NOTE: Make shure that all replacement files contains necessary imports and exports that other files are expecting.
file-replace-loader allows replace binary files.
For example:
1//webpack.config.js 2 3const { resolve } = require('path'); 4 5module.exports = { 6 //... 7 module: { 8 rules: [{ 9 test: /\.png$/, 10 use: [{ 11 loader: 'file-loader', 12 options: { 13 name: '[name].[ext]', 14 }, 15 }, { 16 loader: 'file-replace-loader', 17 options: { 18 condition: 'if-replacement-exists', 19 replacement: resolve('./src/replacement.png') 20 } 21 }] 22 }] 23 } 24}
file-replace-loader must executes before other loaders. This means that in webpack config file the loader must be last in list.
For example:
1//webpack.config.js 2 3const { resolve } = require('path'); 4 5// Correct 6 7module.exports = { 8 //... 9 module: { 10 rules: [{ 11 test: /\.config\.js$/, 12 use: [{ 13 loader: 'babel-loader', 14 }, { 15 loader: 'file-replace-loader', 16 options: { 17 condition: 'if-replacement-exists', 18 replacement: resolve('./config.local.js'), 19 async: true, 20 } 21 }] 22 }] 23 }, 24}
Above is correct example. file-replace-loader will executed before other loaders.
Let's see incorrect usage:
1//webpack.config.js 2 3const { resolve } = require('path'); 4 5// Error, because file-replace-loader will be execute after other loaders 6 7module.exports = { 8 //... 9 module: { 10 rules: [{ 11 test: /\.config\.js$/, 12 use: [{ 13 loader: 'file-replace-loader', 14 options: { 15 condition: 'if-replacement-exists', 16 replacement: resolve('./config.local.js'), 17 async: true, 18 } 19 }, { 20 loader: 'babel-loader', 21 }] 22 }] 23 }, 24}
In incorrect example above file-replace-loader first in rule list. This case throw an error because file-replace-loader should be last in list.
Option | Type | Required | Default | Possible values |
---|---|---|---|---|
condition Condition to replace | string |boolean | no | 'if-replacement-exists' | true ,false ,'always' ,'never' ,'if-replacement-exists' ,'if-source-is-empty' |
replacement Replacement file | string |function (resourcePath, options) | yes | — | Full path to file or function returning full path to file |
async Asynchronous file reading | boolean | no | true | true ,false |
progress Progress output | boolean | no | IS_DEBUG_MODE == true or IS_PROGRESS_MODE == true | true ,false |
See contributing guideline.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 3/14 approved changesets -- score normalized to 2
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
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-12-16
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