/webpack-content-replacer-plugin

Plugin that allow content modification after emit

Primary LanguageJavaScriptMIT LicenseMIT

Webpack content replacer

Replace content in file in webpack workflow

Build Status codecov Dependencies dependencies Status devDependencies Status MIT License

Install Instructions

$ npm i webpack-content-replacer-plugin

Note: This plugin needs NodeJS >= 6

Usage Instructions

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',
        },
      ],
    })
  ]
}

Options

  • silent (bool): Display/hide info logs
  • modifiedFile (string): File to be modified
  • modifications (string): Array of modifications' object having regex and modification property
  • buildTrigger (string): Webpack build step (c.f plugin documentation). See below accepted steps:
    • after-emit
    • done
    • failed

Call at specific step

var config = {
  plugins: [
    new ContentReplacer({
      buildTrigger: 'after-emit',
      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',
        },
      ],
    })
  ]
}

Use silent mode

var config = {
  plugins: [
    new ContentReplacer({
      silent: true,
      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',
        },
      ],
    })
  ]
}

Contributing

All contributions are welcome. Please make a pull request and make sure things still pass after running npm test