soundcloud / chunk-manifest-webpack-plugin

Allows exporting a manifest that maps entry chunk names to their output files, instead of keeping the mapping inside the webpack bootstrap.
MIT License
394 stars 73 forks source link

Add example #1

Open diurnalist opened 9 years ago

diurnalist commented 9 years ago

Add an example that shows why this plugin is helpful, and how it is used.

okonet commented 9 years ago

:+1: Please! This would be really helpful!

deepsweet commented 9 years ago

I can't get it work, chunks IDs in manifest.json changes on every build. or I don't understand the idea :)

kentcdodds commented 9 years ago

Could definitely use an example. I get what this line means:

Webpack can then read this mapping, assuming it is provided somehow on the client

But I'm not sure how to provide the mapping to webpack...

deepsweet commented 9 years ago

have found this:

You may remember from before that webpack creates an ID for each bundle, to minimise the size of all its files. So, every compiled bundle webpack produces will internally have an ID. By default, webpack will store these IDs in the common bundle. The problem with this is that whenever you change any bundle, it’ll mean the common bundle will be updated (to include the new common manifest), thus cache-busting it needlessly.

Thanks to the ChunkManifestPlugin, webpack can be told to not write this manifest directly into the common bundle. Instead it writes the manifest out and then, when it runs in the browser, will look for a global variable webpackBundleManifest

and then:

<script>
//<![CDATA[
window.webpackManifest = {"0":"0-bundle-bdbd995368b007bb18a7.js","2":"2-bundle-7ad34cf6445d875d8506.js","3":"3-bundle-f8745c8bc2319252b6de.js","4":"4-bundle-ec8f5ae62f2e8da11aa1.js","5":"authenticated-bundle-933816ada9534488d12f.js","6":"public-bundle-8eb73d97201bd2e4951b.js"}
//]]>
</script>
<script src="https://abc.cloudfront.net/assets/common-71a050793d79ce393b1e.js"></script>
<script src="https://abc.cloudfront.net/assets/public-bundle-8eb73d97201bd2e4951b.js"></script>

as far as I understand it webpack will try to find IDs map in window.webpackManifest instead of storing it in common chunk.

diurnalist commented 9 years ago

@deepsweet @kentcdodds yes, you just assign that manifest JSON to a global variable, presumably as some sort of <script> tag in your HTML response.

Thanks for the link to the post! Does a better job of explaining.

okonet commented 9 years ago

I've written an article on how to set it up. Would be happy for any feedback: https://medium.com/@okonetchnikov/long-term-caching-of-static-assets-with-webpack-1ecb139adb95

@deepsweet your issue is also mentioned in the article. You should use OccurenceOrderPlugin to fix this.

I've also added a repository with a sample webpack.config: https://github.com/okonet/webpack-long-term-cache-demo

chrisabrams commented 9 years ago

Thanks a lot for this!

ibufu commented 8 years ago

Is there a plugin can help us inject webpackManifest to html automatically?

okonet commented 8 years ago

@ibufu https://github.com/szrenwei/inline-manifest-webpack-plugin

feus4177 commented 7 years ago

@ibufu if you're talking about injecting window.webpackManifest = {...}, I found https://github.com/jouni-kantola/inline-chunk-manifest-html-webpack-plugin to be useful. I realize this is a little late but hopefully others who come after me will find it useful.

jouni-kantola commented 7 years ago

I've published inline-chunk-manifest-html-webpack-plugin to npm. https://www.npmjs.com/package/inline-chunk-manifest-html-webpack-plugin