可视化构建包分析报告
一、webpack
使用 webpack-bundle-analyzer
插件即可。
安装:npm install webpack-bundle-analyzer -D
使用:new BundleAnalyzerPlugin(options?: object)
Name Type Description
analyzerMode One of: server, static, json, disabled Default: server. In server mode analyzer will start HTTP server to show bundle report. In static mode single HTML file with bundle report will be generated. In json mode single JSON file with bundle report will be generated. In disabled mode you can use this plugin to just generate Webpack Stats JSON file by setting generateStatsFile to true.
analyzerHost {String} Default: 127.0.0.1. Host that will be used in server mode to start HTTP server.
analyzerPort {Number} or auto Default: 8888. Port that will be used in server mode to start HTTP server. If analyzerPort is auto, the operating system will assign an arbitrary unused port
analyzerUrl {Function} called with { listenHost: string, listenHost: string, boundAddress: server.address}. server.address comes from Node.js Default: http:// l i s t e n H o s t : {listenHost}: listenHost:{boundAddress.port}. The URL printed to console with server mode.
reportFilename {String} Default: report.html. Path to bundle report file that will be generated in static mode. It can be either an absolute path or a path relative to a bundle output directory (which is output.path in webpack config).
reportTitle {String|function} Default: function that returns pretty printed current date and time. Content of the HTML title element; or a function of the form () => string that provides the content.
defaultSizes One of: stat, parsed, gzip Default: parsed. Module sizes to show in report by default. Size definitions section describes what these values mean.
openAnalyzer {Boolean} Default: true. Automatically open report in default browser.
generateStatsFile {Boolean} Default: false. If true, webpack stats JSON file will be generated in bundle output directory
statsFilename {String} Default: stats.json. Name of webpack stats JSON file that will be generated if generateStatsFile is true. It can be either an absolute path or a path relative to a bundle output directory (which is output.path in webpack config).
statsOptions null or {Object} Default: null. Options for stats.toJson() method. For example you can exclude sources of your modules from stats file with source: false option. See more options here.
excludeAssets {null|pattern|pattern[]} where pattern equals to {String|RegExp|function} Default: null. Patterns that will be used to match against asset names to exclude them from the report. If pattern is a string it will be converted to RegExp via new RegExp(str). If pattern is a function it should have the following signature (assetName: string) => boolean and should return true to exclude matching asset. If multiple patterns are provided asset should match at least one of them to be excluded.
logLevel One of: info, warn, error, silent Default: info. Used to control how much details the plugin outputs.
配置webpack
// webpack.config.js 文件const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
module.exports={plugins: [new BundleAnalyzerPlugin({analyzerMode: "static",analyzerPort: 8888,reportFilename: "report.html",openAnalyzer: true,generateStatsFile: false,})// 默认配置的具体配置项// new BundleAnalyzerPlugin({// analyzerMode: 'server',// analyzerHost: '127.0.0.1',// analyzerPort: '8888',// reportFilename: 'report.html',// defaultSizes: 'parsed',// openAnalyzer: true,// generateStatsFile: false,// statsFilename: 'stats.json',// statsOptions: null,// excludeAssets: null,// logLevel: info// })]
}
配置package.json 文件
-V, --version 输出版本号
-m, --mode 分析器模式。应该是“server”、“static”或“ json”。
Inserver
mode analyzer will start HTTP server to show bundle report.
Instatic
mode single HTML file with bundle report will be generated.
Injson
mode single JSON file with bundle report will be generated. (default: server)
在“server”模式下,分析器将启动 HTTP 服务器来显示捆绑报告。
在“static”模式下,将生成包含报告的单个 HTML 文件。
在“json”模式下,将生成带有捆绑报告的单个 JSON 文件(默认值: 服务器)。
-h, --host 将在“server”模式下用于启动 HTTP 服务器的主机。(默认值: 127.0.0.1)
-p, --port 端口将用于在“server”模式启动 HTTP 服务器。应该是一个数字或“auto”(默认值: 8888)
-r, --report 将在“ static”模式下生成的绑定报告文件的路径(默认值: report.html)
-t, --title在 html 报告的 title 元素中使用的字符串<br/> -s, --default-sizes 默认情况下在树映射中显示的模块大小。<br/> 可能的值: stat、 parsed、 gzip (默认值: parsed)<br/> -O, --no-open 不要在默认浏览器中自动打开报表。<br/> -e, --exclude 应该从报告中排除的资产。<br/> 可以多次指定。<br/> -l, --log-level 日志分级。<br/> 可能的值: 调试,信息,警告,错误,静默(默认值: 信息)<br/> -h, --help 输出用量信息输出用量信息
{"scripts": {"build": "node build/build.js","build:analyzer": "use_analyzer=true npm run build --report",}
}
效果:
执行命令:npm run build:analyzer
二、Rollup(vite)
使用 rollup-plugin-visualizer
插件即可。
安装:npm install -D rollup-plugin-visualizer
使用:
filename (string, default stats.{ext depending template}) - name of the file with diagram to generate
title (string, default Rollup Visualizer) - title tag value
open (boolean, default false) - Open generated file in default user agent
template (string, default treemap) - Which diagram type to use: sunburst, treemap, network, raw-data, list.
gzipSize (boolean, default false) - Collect gzip size from source code and display it at chart.
brotliSize (boolean, default false) - Collect brotli size from source code and display it at chart.
Advanced options (touch only if you really need it):
emitFile (boolean, default false) - Use rollup’s emitFile to generate file. Useful if you want to control all output in one place (via rollup output options). This also could be usefull with svelte as it calls vite several times.
sourcemap (boolean, default false) - Use sourcemaps to calculate sizes (e.g. after UglifyJs or Terser). Always add plugin as last option.
projectRoot (string | RegExp, default process.cwd()) - This is some common root(s) path to your files. This is used to cut absolute files paths out.
include (Filter | Filter[], default undefined) - Filter for inclusion
exclude (Filter | Filter[], default undefined) - Filter for exclusion
Filter type is { bundle?: picomatchPattern, file?: picomatchPattern }
Note about include and exclude - If options.include is omitted or has zero length, filter will return true by default. Otherwise, an ID must match one or more of the picomatch patterns, and must not match any of the options.exclude patterns. This entries will not be included in stats at all.
配置vite
import { visualizer } from "rollup-plugin-visualizer";module.exports = {plugins: [visualizer({filename: './node_modules/.cache/report.html',open: true})],
};
配置package.json 文件
{"scripts": {"build:only": "vite build","build:mode": "vite build --mode report",}
}
效果:
执行命令:npm run build:mode