You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.5 KiB

4 years ago
  1. ## Webpack plugin: replace-in-file-webpack-plugin
  2. This is a [webpack](http://webpack.github.io/) 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.
  3. Installation
  4. ============
  5. Install the plugin with npm:
  6. ```shell
  7. $ npm install replace-in-file-webpack-plugin --save-dev
  8. ```
  9. Basic Usage
  10. ===========
  11. Add the plugin to your webpack and config as follows:
  12. ```javascript
  13. const ReplaceInFileWebpackPlugin = require('replace-in-file-webpack-plugin');
  14. const webpackConfig = {
  15. entry: 'index.js',
  16. output: {
  17. path: __dirname + '/dist',
  18. filename: 'index_bundle.js'
  19. },
  20. plugins: [
  21. new ReplaceInFileWebpackPlugin([{
  22. dir: 'dist',
  23. files: ['index.html', 'main.html'],
  24. rules: [{
  25. search: '@class',
  26. replace: 'main-class'
  27. },{
  28. search: /@title/,
  29. replace: 'webpack'
  30. }]
  31. }, {
  32. dir: 'dist/style',
  33. test: /\.html$/,
  34. rules: [{
  35. search: /version/ig,
  36. replace: '1.0.0'
  37. },{
  38. search: '@title',
  39. replace: function(match){
  40. }
  41. }]
  42. },{
  43. dir: 'dist/style',
  44. test: [/\.css$/, /\.txt/],
  45. rules: [{
  46. search: /version/ig,
  47. replace: '1.0.0'
  48. },{
  49. search: '@title',
  50. replace: 'webpack'
  51. }]
  52. }])
  53. ]
  54. };
  55. ```
  56. Configuration
  57. =============
  58. You can pass an array of configuration options to `ReplaceInFileWebpackPlugin`. Each configuration has following items:
  59. - `dir`: Optional. Base dir to find the files, if not provided, use the root of webpack context.
  60. - `files`: Optional. Files in `dir` to find for replacement.
  61. - `test`: Optional. Regex expression or Regex expressions array to match files in `dir`.
  62. - `rules`: Required. Replace content rules array. Each rule has `search` and `replace` properties.
  63. - `search`: Required. String or Regex expression used for searching content in files.
  64. - `replace`: Required. String or funcion used for replacing the searching content.
  65. # License
  66. This project is licensed under [MIT](https://github.com/oyslin/replace-in-file-webpack-plugin/blob/master/LICENSE).