Webpack Plugin for modifying modules source.
Webpack Version | Plugin version | Status | Branch |
---|---|---|---|
^5.0.0 | ^2.0.0 | ✅ |
master |
^4.37.0 | ^1.1.0 | ✅ |
1.x |
npm i -D modify-source-webpack-plugin@next
yarn add -D modify-source-webpack-plugin@next
npm i -D modify-source-webpack-plugin
yarn add -D modify-source-webpack-plugin
import { ModifySourcePlugin } from 'modify-source-webpack-plugin';
const { ModifySourcePlugin } = require('modify-source-webpack-plugin');
webpack.config.js
module.exports = {
plugins: [new ModifySourcePlugin(options)]
};
Type: RegExp | ((module: webpack.NormalModule) => boolean)
Required
test
is RegExp or function, which used to determinate which modules should be modified.
RegExp
will be applied to full module path (based on userRequest
).
function
will be applied to NormalModule
.
module.exports = {
plugins: [
new ModifySourcePlugin({
rules: [
{
test: /index\.js$/
}
]
})
]
};
module.exports = {
plugins: [
new ModifySourcePlugin({
rules: [
{
test: module =>
module.source().source().includes('my-secret-module-marker')
}
]
})
]
};
Type: (source: string, filename: string) => string
Required
Function accept a source and filename. Should return a modified source.
WARNING: modify function should make syntax compatible changes, for example all unsupported syntax will break your build or create errors in runtime.
module.exports = {
plugins: [
new ModifySourcePlugin({
rules: [
{
test: /my-file\.js$/,
modify: (src, filename) =>
src +
`\n\n// This file (${filename}) is written by me. All rights reserved`
}
]
})
]
};
module.exports = {
plugins: [
new ModifySourcePlugin({
rules: [
{
test: /my-file\.js$/,
modify: src => src + `haha I break your build LOL`
}
]
})
]
};
Type: boolean