src*_*der 17 javascript gulp webpack webpack-4
似乎无法在webpack中找到任何调试选项或插件来显示究竟是什么进入了一个块.
我为什么需要这个?我注意到代码分割的字面意思使得一切都变得更大,然后将所有东西都放在一个文件中.这有点反直觉,因为我不相信webpack的bootstrap代码是那么重要; 我怀疑它可能是缩小/重复数据删除,但不知道哪些模块实际上被分块,很难做一些孤立的测试来确认.
我的构建过程使用gulp; 如果这有任何区别.
Boo*_*jaa 10
你可以写一个执行此操作的插件.
例如,
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;
})
};
}));
});
});
};
Run Code Online (Sandbox Code Playgroud)
有关更多详细信息,请查看此页面http://webpack.github.io/docs/plugins.html.它包含您可以挂钩的所有地方的文档.找到一个被调用的正确的钩子,chunks: Chunk[]或者chunk在里面,你将能够访问你想要的一切.
此外,stats对象包含post-build您需要的所有信息.它在done插件中可用.
似乎它具有--display-modules显示所有模块的关键知识:
$ webpack --display-modules
Run Code Online (Sandbox Code Playgroud)
然后,您将获得类似于以下内容的已用模块列表:
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]
Run Code Online (Sandbox Code Playgroud)
小智 5
这是 Boopathi Rajaa 答案的更新版本,适用于Webpack(我使用的是 4.37.0)的更高版本
这个更新的版本对我有用:
class PrintChunksPlugin {
apply (compiler) {
compiler.plugin('compilation', compilation => {
compilation.plugin('after-optimize-chunk-assets', chunks => {
console.log(chunks.map(chunk => ({
id: chunk.id,
name: chunk.name,
modules: Array.from(chunk._modules).map(module => module.id)
})))
})
})
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
plugins: [
new PrintChunksPlugin(),
]
Run Code Online (Sandbox Code Playgroud)
最大的区别是,他们现在的模块信息存储在_modules,而不是modules它是不是之前的可映射对象Array.from。我发现module.id它更接近我需要的东西,但module.request如果你需要它,它仍然存在。
解决方案是编写一个解析 .js.map 文件的脚本,因为它们包含一个sources可用于识别块中包含的所有文件的条目。
这是一个可以完成工作的小 gulp 脚本,
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);
});
});
Run Code Online (Sandbox Code Playgroud)
您需要修改脚本以满足您自己的配置。
以防万一它不明显,这webpack://source-code部分是由于 webpack 设置中的 devtool 设置造成的output,即:
devtoolModuleFilenameTemplate: "webpack://source-code/[resource-path]",
devtoolFallbackModuleFilenameTemplate: "webpack://source-code/[resource-path]?[hash]",
Run Code Online (Sandbox Code Playgroud)
和来自以下规范化脚本(我不喜欢 webpack 的“~”替换功能)webpack/internal。node_modules
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);
});
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10656 次 |
| 最近记录: |