Webpack Plugin to rewrite the source before compilation. Files will be restored to their original state after compilation.
You may want to alter the source code before compilation. (E.g. if you have templated variables.)
npm i -D rewrite-source-webpack-plugin
In your webpack.config.json
, you can rewrite source files either by path or a regular expression:
plugins: [
...
new RewriteSourcePlugin([
{
test: /src\/App.js/,
rewrite: (src) => {
return src.replace(/{{foo}}/g, 'A template variable');
}
},
{
path: './src/index.html',
rewrite: (src) => {
return src.replace(/{{title}}/g, 'My page title');
}
},
{
path: './src/index.html',
rewrite: (src) => {
return src.replace(/{{description}}/g, 'My page description');
}
},
]),
new HtmlWebPackPlugin({
template: "./src/index.html",
filename: "./index.html"
}),
],