DEV Community

muhammed
muhammed

Posted on

manifest.json use cases > a note for myself

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&#39;s file. This seems that there is no need to use webpack-manifest-plugin, I don&#39;t even see where the manifest.json is used. </p> <h3> <a name="answer" href="#answer" class="anchor"> </a> Answer: </h3> <p>HtmlWebpackPlugin &quot;knows&quot; that your asset bundle.js maps to bundle.some-hash.js because it uses the Webpack&#39;s Manifest. This manifest is not emitted though. It&#39;s just data that Webpack uses to keep track of how all the modules map to the output bundles. </p> <p>WebpackManifestPlugin uses Webpack&#39;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&#39;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 &quot;manually&quot; inject the assets generated by Webpack. </p> <p>So, the manifest.json is not for Webpack. It&#39;s for you. </p> <p>Let&#39;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>&lt;img src=&quot;{{ asset_for(&#39;images/hamburger.svg&#39;) }}&quot; alt=&quot;Hamburger&quot;&gt;</code> </p> <p>and get this: </p> <p><code>&lt;img src=&quot;images/hamburger.d2cb0dda3e8313b990e8dcf5e25d2d0f.svg&quot; alt=&quot;Hamburger&quot;&gt;</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)