Is there a way we can modify manifest.json generated in dev-server mode

Viewed 185

I am using webpack-manifest-plugin plugin to generate a manifest.json file. After that i have another plugin that runs after the complete build is done. There I want to modify the manifest.json.Now this works fine with prod build(we are doing writeFileSync to update).

But in the dev-server build manifest.json does not get updated(as it not present in the filesystem it is in memory). I want to update manifest.json after the complete build is done in dev-server-mode. In dev-server mode build will not use the local file system it will in memory.

compiler.hooks.done.tap('After Compilation', (stats) => {
     // some code to update the manifest.json
})

Please let me know any way to access manifest.json post build and update in dev-server. I tried afterEmit of webpack-manifest-plugin but I want it after the entire build is done.

1 Answers

Hi, as variant you can use plugin "copy-webpack-plugin" for webpack.

npm: https://www.npmjs.com/package/copy-webpack-plugin#filter

You can get manifest "content" and do whatever you want.

For instance: create file in development mode inside build folder.

const fs = require('fs')
const CopyPlugin = require('copy-webpack-plugin');

new CopyPlugin({
  patterns: [
    {
      from: 'public/manifest.json',
      transform(content) {
        // create manifest.json for development
        if (process.env.NODE_ENV === 'development') {
          fs.writeFile('./build/manifest.json', content, function (err) {
            if (err) throw err;
            console.log('Saved!');
          })
        }

        return content
      },
    },
  ],
})
Related