Vue CLI Webpack Bundle Analyzer Plugin
This package provides a Vue CLI plugin that integrates `webpack-bundle-analyzer` into Vue projects, enabling interactive treemap visualizations of webpack output file sizes. It helps developers understand bundle composition and identify large modules efficiently. The current stable version is 4.0.0, which updated the underlying `webpack-bundle-analyzer` dependency to version 4. The plugin primarily operates via `vue.config.js` `pluginOptions` and offers features like separate reports for modern and legacy builds when using Vue CLI's modern mode. It's an essential tool for build optimization in Vue applications, offering a more integrated experience than configuring `webpack-bundle-analyzer` manually. Its release cadence is tied to updates in `webpack-bundle-analyzer` and Vue CLI itself, ensuring compatibility and access to the latest analyzer features.
Common errors
-
Cannot read properties of undefined (reading 'webpackBundleAnalyzer')
cause The `pluginOptions` object or `pluginOptions.webpackBundleAnalyzer` is missing or malformed in `vue.config.js`.fixEnsure your `vue.config.js` includes `module.exports = { pluginOptions: { webpackBundleAnalyzer: { /* ... */ } } };` and that `webpackBundleAnalyzer` is an object. -
Analyzer report is not opening in the browser after build.
cause The `openAnalyzer` option is set to `false` or the build environment prevents browser launches.fixCheck `pluginOptions.webpackBundleAnalyzer.openAnalyzer` in `vue.config.js`. Set it to `true` if you want it to open automatically (default for production). Alternatively, manually open the generated HTML report file from your output directory. -
Error: ENOENT: no such file or directory, stat 'legacy-report.html'
cause Attempting to access a report file that wasn't generated, possibly due to not building in modern mode or a mismatch in the `reportFilename` configuration.fixVerify if you built with `vue-cli-service build --modern`. If not, the report won't be prefixed with `legacy-` or `modern-`. Adjust the filename you are looking for, or ensure your build command includes `--modern` if two reports are desired.
Warnings
- breaking Version 4.0.0 updated the underlying `webpack-bundle-analyzer` to version 4. This may introduce breaking changes in the available configuration options for `webpackBundleAnalyzer` in `vue.config.js` or alter report behavior.
- breaking In version 2.0.0, the `reportFilename` config option changed how it's handled. It now accepts a full file path rather than just a name. Configurations relying on the older behavior might need adjustment.
- gotcha When building for production, `openAnalyzer` defaults to `true`. This can cause a browser window to open automatically after a successful build, potentially disrupting CI/CD pipelines or automated build processes.
- gotcha In development mode, `webpack-bundle-analyzer` can only calculate the `stat` size due to its requirement for physical files to compute `parsed` and `gzipped` sizes. The more accurate `parsed` and `gzipped` sizes are only available after a full production build.
- gotcha When using Vue CLI's `--modern` flag for builds, the plugin generates two distinct reports, prefixed with `legacy-` and `modern-` respectively. If you're expecting a single report, be aware of this dual output.
Install
-
npm install vue-cli-plugin-webpack-bundle-analyzer -
yarn add vue-cli-plugin-webpack-bundle-analyzer -
pnpm add vue-cli-plugin-webpack-bundle-analyzer
Imports
- Configuration options
import { WebpackBundleAnalyzerPlugin } from 'vue-cli-plugin-webpack-bundle-analyzer';module.exports = { pluginOptions: { webpackBundleAnalyzer: { /* options */ } } }; - Webpack chain plugin injection
config.plugin('webpack-bundle-analyzer').use(BundleAnalyzerPlugin).init(Plugin => new Plugin(options)); - Type definition (implied)
/** @type {import('@vue/cli-service').ProjectOptions} */ module.exports = { pluginOptions: { webpackBundleAnalyzer: { /* options */ } } };
Quickstart
vue add webpack-bundle-analyzer
// Then, in vue.config.js, configure options:
// vue.config.js
module.exports = {
pluginOptions: {
webpackBundleAnalyzer: {
openAnalyzer: false, // Prevents opening the analyzer report in the browser automatically
analyzerMode: 'static', // Generates a static HTML file
reportFilename: 'bundle-report.html' // Custom report filename
}
}
};
// To generate the report, build your project:
// npm run build
// (or yarn build)