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

It seems that no debugging option or plugin was found in webpack to show what exactly fell into the piece.

Why do I need it? I have noticed cases where splitting the code literally does much more than just paste everything into one file. This is a bit contrasting intuitive since I don't think the bootstrap code from webpack is significant; I suspect this may be minification / deduplication, but knowing which modules are actually mixed up, it is very difficult to do some isolated tests to confirm.

My build process uses gulp; if that matters.

+8
javascript webpack gulp
source share
3 answers

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; }) }; })); }); }); }; 

See http://webpack.imtqy.com/docs/plugins.html for more details. It contains documentation for all the places you can connect to. Find the correct hook that is called with chunks: Chunk[] or chunk , and inside this you can access everything you want.

In addition, the statistics object contains all the post-build information you will ever need. It is available in the done plugin.

+8
source share

The solution is to write a script that parses the .js.map files, since they contain a sources record that can be used to recognize all the files that were included in the piece.


Here is a small gulp script that will do the job,

 var debugWebpackMapFile = function (file) { var cleanupRules = { // executed in order '/src/client/javascript/node_modules/': '', '/src/client/javascript/': 'static/' }; return new Promise(function (resolve, reject) { var match = /\/([^\/]+).js.map$/.exec(file); if (match != null) { var filename = match[1]; console.log("\n " + filename + "\n =======================\n"); var mapjson = JSON.parse(fs.readFileSync(file)); var dependencies = []; var sourcefiles = []; _.each(mapjson.sources, function (srcfile) { srcfile = srcfile.replace('webpack://source-code', '', srcfile); var match = /^\/node_modules\/([^\/]+)/.exec(srcfile); if (match == null) { match = /^(\/src\/.*\.js)(\?.*)?/.exec(srcfile); if (match != null) { // project source file srcfile = match[1]; _.each(cleanupRules, function (to, from) { srcfile = srcfile.replace(from, to); }); // the sources are in random order in the map file, // so we'll need to sort before displaying anything sourcefiles.push(srcfile); } } else { // dependency var pkg = match[1]; if (dependencies.indexOf(pkg) == -1) { dependencies.push(pkg); } } }); sourcefiles.sort(); _.each(sourcefiles, function (srcfile) { console.log(" " + srcfile); }); if (dependencies.length > 0) { console.log("\n ---- 3rd Party ------------------\n"); dependencies.sort(); _.each(dependencies, function (pkg) { console.log(" " + pkg); }); } } console.log("\n\n"); resolve(); }); } gulp.task('js:debug', function (cb) { var conf = webpackConf.mainjs; glob(conf.output.path + '/*.map', {}, function (er, files) { var promises = []; _.each(files, function (file) { promises.push(debugWebpackMapFile(file)); }); Promise.all(promises).lastly(cb); }); }); 

You will need to modify the script to fit your own configuration.

Just in case, this is not obvious, part of webpack://source-code is due to the devtool settings in the webpack output settings, namely:

 devtoolModuleFilenameTemplate: "webpack://source-code/[resource-path]", devtoolFallbackModuleFilenameTemplate: "webpack://source-code/[resource-path]?[hash]", 

webpack/internal and node_modules refer to the following normalization script (I don't like the webpack replacement function "~").

 var normalizeMaps = function (conf, cb) { glob(conf.output.path + '/*.map', {}, function (er, files) { var promises = []; _.each(files, function (file) { promises.push(replaceInFile(file, [ [ /\/~/g, '/node_modules' ], [ /\.\//g, ''], [ /source-code\/\(webpack\)/g, 'source-code/webpack/internal' ] ])); }); Promise.all(promises).lastly(cb); }); }; 
0
source share

The key seems to know how --display-modules to show all modules:

 $ webpack --display-modules 

Then you will get a list of used modules as follows:

  Asset Size Chunks Chunk Names javascripts/webpack/app.js 211 kB 0 [emitted] javascripts/webpack/app stylesheets/webpack/app.js 4.13 kB 1 [emitted] stylesheets/webpack/app stylesheets/webpack/app.scss 2.99 kB 1 [emitted] stylesheets/webpack/app [0] ./app/webpack/js/behaviors/lory-icon-slider.js.coffee 1.1 kB {0} [optional] [built] [1] ./app/webpack/css/components (\.scss|\.css)$ 160 bytes {1} [built] [2] ./app/webpack/js/behaviors (\.js|\.js.jsx|\.js.coffee)$ 252 bytes {0} [built] [3] ./~/pickmeup/css/pickmeup.scss 41 bytes {1} [built] [4] ./app/webpack/css/app.js 205 bytes {1} [built] [5] ./app/webpack/js/app.js 250 bytes {0} [built] [6] ./app/webpack/js/behaviors/pickmeup-fixed-calendar.js 3.47 kB {0} [optional] [built] [7] ./~/lory.js/dist/jquery.lory.js 25 kB {0} [built] [8] ./~/pickmeup/js/pickmeup.js 41.4 kB {0} [built] [9] (webpack)/buildin/global.js 509 bytes {0} [built] Child extract-text-webpack-plugin: [0] ./~/css-loader/lib/css-base.js 1.51 kB {0} [built] [1] ./~/css-loader!./~/sass-loader/lib/loader.js!./~/pickmeup/css/pickmeup.scss 3.23 kB {0} [built] 
0
source share

All Articles