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 dev server not serve the updated file #9

Open ericxinzhang opened 5 years ago

ericxinzhang commented 5 years ago

It took me whole day to find out the cause of this issue as the symptom was very confusing.

My scenario is that I have an environment specific config file stored in source code repo. In webpack.config.js I copy the file to dist directory first using copy-webpack-plugin, and then update file content using replace-in-file-webpack-plugin.

The file written to dist directory is correct. However somehow webpack dev server never reads the file on disk, but keep returning the original file copied by copy-webpack-plugin. I tried the webpack dev server settings like cache, writeToDisk, none worked. I am using webpack-dev-server 3.2.1, also tried upgrading to latest 3.6.0, both are having this problem.

I removed replace-in-file-webpack-pluginand used the transform in copy-webpack-plugin, it's working fine now.

I am not sure if it's a bug of webpack dev server itself, or there is a way that the plugin can tell webpack dev server some file get updated.

scil commented 4 years ago

this plugin can works with webpack dev server: https://github.com/ali322/content-replace-webpack-plugin

const to_replace = [  '__webpack_require__\\(0\\)\\.',      '__webpack_require__.'  ];

...

    new ContentReplacePlugin({
      rules: {
        '*.js': content => content.replace(new RegExp(to_replace[0],'g'), to_replace[1])
      }
    })