How to get a list of all files (or modules) included per chunk in Webpack

srcspider picture srcspider · Apr 30, 2015 · Viewed 17.6k times · Source

Can't seem to find any debug option or plugin in webpack to show what exactly went into a chunk.

Why do I need this? I've noticed cases where code splitting literally makes everything much larger then just sticking everything in a single file. This is a bit counter intuitive since I don't believe the bootstrap code from webpack is that significant; I suspect it might be minification/deduping, but with out knowing which modules are actually being chunked togheter it's very hard to do some isolated tests to confirm.

My build process uses gulp; if that makes any difference.

Answer

Boopathi Rajaa picture Boopathi Rajaa · May 5, 2015

You can write a plugin that does this.

For example,

var PrintChunksPlugin = function() {};
PrintChunksPlugin.prototype.apply = function(compiler) {
    compiler.plugin('compilation', function(compilation, params) {
        compilation.plugin('after-optimize-chunk-assets', function(chunks) {
            console.log(chunks.map(function(c) {
                return {
                    id: c.id,
                    name: c.name,
                    includes: c.modules.map(function(m) {
                        return m.request;
                    })
                };
            }));
        });
    });
};

For more details, check this page http://webpack.github.io/docs/plugins.html . It contains documentation for all the places you can hook into. Find the proper hook which gets called with chunks: Chunk[] or chunk, and inside this you'll be able to access everything you want.

Also, the stats object contains all the post-build information you'd ever need. It's available in the done plugin.