Installations
npm install file-replace-loader
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
16.20.2
NPM Version
8.19.4
Score
77.6
Supply Chain
99.5
Quality
77.4
Maintenance
100
Vulnerability
99.6
License
Releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
vyushin
Download Statistics
Total Downloads
1,011,976
Last Day
856
Last Week
6,463
Last Month
22,476
Last Year
273,467
GitHub Statistics
11 Stars
71 Commits
4 Forks
15 Branches
2 Contributors
Sponsor this package
Package Meta Information
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
Total Downloads
Cumulative downloads
Total Downloads
1,011,976
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
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
2
Dev Dependencies
2
file-replace-loader
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
Table of contents
- Features
- Installation
- Usage
- Multiple replace
- Using with binary files
- Using with other loaders
- Loader options
- Contributing
- License
Features
- Compatibility with webpack 3.x, 4.x, 5.x;
- Support watch webpack mode;
- Replace files in compile time without change source files;
- Multiple replacement;
- Sync and async modes;
- Compatibility with other loaders;
- Support binary files.
Installation
NPM
npm install --save-dev file-replace-loader
Yarn
yarn add file-replace-loader
Usage
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.
Multiple replace
To describe replace rules for two or more files you can use function as replacement value.
How does it work?
- Webpack runs file-replace-loader according to
test
rule,include
andexclude
rule options; - file-replace-loader looks on
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. - If
replacement
function returns a path then file-replace-loader looks tocondition
. If condition isalways
then it replace every match. Ifcondition
isif-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.
Using with binary files
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}
Using with other loaders
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.
Loader options
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 |
Contributing
See contributing guideline.
License
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
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
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 30 are checked with a SAST tool
Score
3.8
/10
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 MoreOther packages similar to 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