
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
file-replace-loader
Advanced tools
file-replace-loader is webpack loader that allows you replace files in compile time
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
const { resolve } = require('path');
module.exports = {
//...
module: {
rules: [{
test: /\.config\.js$/,
loader: 'file-replace-loader',
options: {
condition: 'if-replacement-exists',
replacement: resolve('./config.local.js'),
async: true,
}
}]
}
}
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:
const { resolve } = require('path');
module.exports = {
//...
module: {
rules: [{
test: /\.js$/,
loader: 'file-replace-loader',
options: {
condition: 'always', // <-- Note that the rule applies for all files!
replacement(resourcePath) {
if (resourcePath.endsWith('foo.js')) {
return resolve('./bar.js');
}
if (resourcePath.endsWith('foo-a.js')) {
return resolve('./bar-a.js');
}
},
async: true,
}
}]
}
}
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:
const { resolve } = require('path');
module.exports = {
//...
module: {
rules: [{
test: /\.js$/,
loader: 'file-replace-loader',
options: {
condition: 'always', // <-- Note that the rule applies for all files! But you can use other conditions too
replacement(resourcePath) {
const mapping = {
[resolve('./src/foo-a.js')]: resolve('./src/bar-a.js'),
[resolve('./src/foo-b.js')]: resolve('./src/bar-b.js'),
[resolve('./src/foo-c.js')]: resolve('./src/bar-c.js'),
};
return mapping[resourcePath];
},
async: true,
}
}]
}
}
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:
//webpack.config.js
const { resolve } = require('path');
module.exports = {
//...
module: {
rules: [{
test: /\.png$/,
use: [{
loader: 'file-loader',
options: {
name: '[name].[ext]',
},
}, {
loader: 'file-replace-loader',
options: {
condition: 'if-replacement-exists',
replacement: resolve('./src/replacement.png')
}
}]
}]
}
}
file-replace-loader must executes before other loaders. This means that in webpack config file the loader must be last in list.
For example:
//webpack.config.js
const { resolve } = require('path');
// Correct
module.exports = {
//...
module: {
rules: [{
test: /\.config\.js$/,
use: [{
loader: 'babel-loader',
}, {
loader: 'file-replace-loader',
options: {
condition: 'if-replacement-exists',
replacement: resolve('./config.local.js'),
async: true,
}
}]
}]
},
}
Above is correct example. file-replace-loader will executed before other loaders.
Let's see incorrect usage:
//webpack.config.js
const { resolve } = require('path');
// Error, because file-replace-loader will be execute after other loaders
module.exports = {
//...
module: {
rules: [{
test: /\.config\.js$/,
use: [{
loader: 'file-replace-loader',
options: {
condition: 'if-replacement-exists',
replacement: resolve('./config.local.js'),
async: true,
}
}, {
loader: 'babel-loader',
}]
}]
},
}
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.
FAQs
file-replace-loader is webpack loader that allows you replace files in compile time
The npm package file-replace-loader receives a total of 4,912 weekly downloads. As such, file-replace-loader popularity was classified as popular.
We found that file-replace-loader demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.