A customizable Webpack/Rspack loader to modify source files on-the-fly with your own callback function.
npm install source-modifier-loader --save-dev
# or
yarn add source-modifier-loader --devmodule.exports = {
module: {
rules: [
{
test: /\.scss$/,
use: [
{
loader: 'source-modifier-loader',
options: {
modify: (source, resourcePath) => {
return source.replace('foo', 'bar');
},
},
}
],
},
],
},
};| Name | Type | Description |
|---|---|---|
modify |
(source: string, resourcePath: string) => string | void | null | undefined |
A callback function to modify source contents. |
source(string): Original file content.resourcePath(string): Absolute path to the file being processed.
If the callback returns a string, the original content will be replaced by it. If the callback returns null, undefined, or anything except a string, the original content remains unchanged.
Use this loader when you need to:
- Dynamically modify file contents based on file path or source code.
- Conditionally insert or alter specific content in source files before other loaders.
- Easily implement custom transformations without writing separate loaders.
This loader includes tests written with Jest:
npm test- ✅ Webpack 5
- ✅ Rspack
Tested and fully compatible with both bundlers.
Install dependencies:
npm installRun tests:
npm testBuild the loader:
npm run build