Purpose of Webpack-Manifest-Plugin in Webpack
https://stackoverflow.com/questions/57661590/purpose-of-webpack-manifest-plugin-in-webpack/60581499
Question:
In official doc says when applying Code Splitting and generating chunk files, if chunk code changes, then the filename of it will change. However index.html which uses the chunk code files can't change the filename in its tag, so in this case the manifest.json which is generated by webpack-manifest-plugin will help mapping [name].js to [name].[hash].js. </p> <p>But opposing to what the doc says, it seems that every time I run webpack to build my codes, new codes are generated with new hash value in its file(in case something in code changed), and HTML-Webpack-Plugin will autometically inject <script> tag with new name of code's file. This seems that there is no need to use webpack-manifest-plugin, I don't even see where the manifest.json is used. </p> <h3> <a name="answer" href="#answer" class="anchor"> </a> Answer: </h3> <p>HtmlWebpackPlugin "knows" that your asset bundle.js maps to bundle.some-hash.js because it uses the Webpack's Manifest. This manifest is not emitted though. It's just data that Webpack uses to keep track of how all the modules map to the output bundles. </p> <p>WebpackManifestPlugin uses Webpack's manifest data data to emit a JSON file (that you can call manifest.json or whatever you want). </p> <p>Since you are using HtmlWebpackPlugin with the inject: true option (it's the default one), HtmlWebpackPlugin injects your bundle bundle.some-hash.js into your template. So you probably have no need to use WebpackManifestPlugin for your application. </p> <p>If you did not use HtmlWebpackPlugin, or if you used it with inject: false, then you would need to find a way to "manually" inject the assets generated by Webpack. </p> <p>So, the manifest.json is not for Webpack. It's for you. </p> <p>Let's say for example that you have a Python Flask web application and your web pages are built with Jinja templates. You could use Webpack to generate all of your static assets, and use the manifest.json to resolve the asset generated by Webpack. This Flask extension does just that. This means that in your jinja template you can write this: </p> <p><code><img src="{{ asset_for('images/hamburger.svg') }}" alt="Hamburger"></code> </p> <p>and get this: </p> <p><code><img src="images/hamburger.d2cb0dda3e8313b990e8dcf5e25d2d0f.svg" alt="Hamburger"></code> </p> <p>Another use case would be if you want fine control where the bundles are injected into your templates. For that, have a look at (this example)[<a href="https://github.com/jantimon/html-webpack-plugin/tree/main/examples/custom-insertion-position">https://github.com/jantimon/html-webpack-plugin/tree/main/examples/custom-insertion-position</a>] in the html-webpack-plugin repo. </p>
Top comments (0)