/webpack-content-replacer-plugin

Plugin that replace content in file in a webpack workflow

Primary LanguageJavaScriptMIT LicenseMIT

![Project logo][project-logo]

Webpack content replacer

Build Status codecov Node dependency Dependencies dependencies Status devDependencies Status MIT License

Replace content in file in webpack workflow

Edit file by replacing content at different build step in a webpack workflow.

For example, you can change text color, text content or also insert configuration values from environment variables.

Installing

npm i webpack-content-replacer-plugin

Getting started in webpack

Require webpack-content-replacer-plugin

var ContentReplacer = require('webpack-content-replacer-plugin')

Add the plugin to your plugin list

var config = {
  plugins: [
    new ContentReplacer({
      modifiedFile: './relative_path/file_to_be_modified.ext',
      modifications: [
        {
          regex: /%content_to_be_deleted%/g,
          modification: 'new_content',
        },
        {
          regex: /%content2_to_be_deleted%/g,
          modification: 'new_content2',
        },
      ],
    })
  ]
}

Configuration

modifiedFile

  • Type: String
  • Default: no default value
  • Required: true

Specify the file which will be modified.

Example:

var config = {
  plugins: [
    new ContentReplacer({
      modifiedFile: './build/index.html',
      ...
    })
  ]
}

modifications

  • Type: Array<Modification>
  • Default: no default value
  • Required: true

Specify the modifications to be applied to the file.

Example:

var config = {
  plugins: [
    new ContentReplacer({
      modifiedFile: './build/index.html',
      [
        {
          regex: /text/g,
          modification: 'new text'
        }
      ],
      ...
    })
  ]
}
modification
  • Type: Object
  • Default: no default value
  • Required: true

Example:

{
  regex: /text/g,
  modification: 'new text'
}

buildTrigger

  • Type: String
  • Default: after-emit
  • Allowed values: after-emit, done, failed

Specify webpack build step (c.f plugin documentation).

Example:

var config = {
  plugins: [
    new ContentReplacer({
      modifiedFile: './build/index.html',
      [
        {
          regex: /text/g,
          modification: 'new text'
        }
      ],
      buildTrigger: 'done',
      ...
    })
  ]
}

silent

  • Type: Boolean
  • Default: false

Display/hide info logs during the build.

Example:

var config = {
  plugins: [
    new ContentReplacer({
      silent: true,
      modifiedFile: './build/index.html',
      [
        {
          regex: /text/g,
          modification: 'new text'
        }
      ],
      buildTrigger: 'done'
      ],
    })
  ]
}

Developing

git clone https://github.com/{your fork}/webpack-content-replacer.git
cd webpack-content-replacer/
npm install

Replace {your fork} by your github username.

Contributing

If you'd like to contribute, please fork the repository and use a feature branch. All contributions are welcome. Please make a pull request and make sure things still pass after running npm test. Ensure you've read the contribution guidelines for more information and respect the code of conduct

Contributors

Thanks goes to these wonderful people (emoji key):


Sebastien Correaud

🚇 💻 📖 ⚠️ 👀

YQBird

📖 ⚠️ 👀

This project follows the all-contributors specification. Contributions of any kind welcome!

Licensing

The code in this project is licensed under MIT license.