oyslin / replace-in-file-webpack-plugin

A webpack plugin to quickly replace text in one or more files.
MIT License
34 stars 14 forks source link

Webpack plugin: replace-in-file-webpack-plugin

This is a webpack plugin that can replace content in file(s) after compilation is done. This is useful when you want to replace content in any kind of files(html, css, js etc) which are not processed by loaders.

Installation

Install the plugin with npm:

$ npm install replace-in-file-webpack-plugin --save-dev

Basic Usage

Add the plugin to your webpack and config as follows:

    const ReplaceInFileWebpackPlugin = require('replace-in-file-webpack-plugin');
    const webpackConfig = {
        entry: 'index.js',
        output: {
            path: __dirname + '/dist',
            filename: 'index_bundle.js'
        },
        plugins: [
            new ReplaceInFileWebpackPlugin([{
                dir: 'dist',
                files: ['index.html', 'main.html'],
                rules: [{
                    search: '@class',
                    replace: 'main-class'
                },{
                    search: /@title/,
                    replace: 'webpack'
                }]
            }, {
                dir: 'dist/style',
                test: /\.html$/,
                rules: [{
                    search: /version/ig,
                    replace: '1.0.0'
                },{
                    search: '@title',
                    replace: function(match){

                    }
                }]
            },{
                dir: 'dist/style',
                test: [/\.css$/, /\.txt/],
                rules: [{
                    search: /version/ig,
                    replace: '1.0.0'
                },{
                    search: '@title',
                    replace: 'webpack'
                }]
            }])
        ]
    };

Configuration

You can pass an array of configuration options to ReplaceInFileWebpackPlugin. Each configuration has following items:

License

This project is licensed under MIT.