Bundle analysis helps you see what code is in your app's final files. Tree shaking removes unused code to make your app smaller and faster.
Bundle analysis and tree shaking in Vue
npm install --save-dev webpack-bundle-analyzer // In vue.config.js const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin; module.exports = { configureWebpack: { plugins: [new BundleAnalyzerPlugin()] } };
This example uses webpack-bundle-analyzer with Vue CLI's webpack config.
Tree shaking happens automatically in production builds if your code uses ES modules.
npm run build
// Then open the report in browser to see bundle sizes// Example of tree shaking import { ref } from 'vue'; const count = ref(0); // If you don't use other parts of Vue, they won't be included in the final bundle.
This config adds the bundle analyzer plugin to Vue's webpack setup. After building, it creates a static HTML report showing the size of each part of your app's bundle.
// vue.config.js const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin; module.exports = { configureWebpack: { plugins: [new BundleAnalyzerPlugin({ analyzerMode: 'static', openAnalyzer: false, reportFilename: 'bundle-report.html' })] } }; // Then run: // npm run build // After build, open 'bundle-report.html' in your browser to see the bundle analysis.
Tree shaking works best with ES module syntax (import/export).
Some libraries may not support tree shaking well if they use older module formats.
Bundle analysis helps you find large dependencies or unused code to optimize.
Bundle analysis shows what code is in your app's final files.
Tree shaking removes unused code automatically in production builds.
Use these tools to make your Vue app smaller and faster.
Practice
tree shaking in a Vue project?Solution
Step 1: Understand tree shaking concept
Tree shaking is a process that removes code not used in the app to reduce bundle size.Step 2: Identify its effect on Vue projects
In Vue, tree shaking helps make the app faster by removing unused components or functions.Final Answer:
To remove unused code from the final bundle -> Option BQuick Check:
Tree shaking = remove unused code [OK]
- Thinking tree shaking adds features
- Believing it increases bundle size
- Confusing it with UI changes
Solution
Step 1: Recall Vue CLI bundle analysis command
The Vue CLI supports bundle analysis with the--reportflag during build.Step 2: Check other options for correctness
Options A, C, and D are incorrect or invalid ways to enable bundle analysis.Final Answer:
Runvue-cli-service build --report-> Option CQuick Check:
Bundle analysis command = build --report [OK]
- Adding unsupported config keys
- Using serve instead of build
- Disabling tree shaking by mistake
import { ref, reactive, unusedFunc } from 'vue';
export default {
setup() {
const count = ref(0);
const state = reactive({ name: 'Vue' });
return { count, state };
}
}Solution
Step 1: Identify used and unused imports
The code usesrefandreactive, butunusedFuncis never used.Step 2: Understand tree shaking effect
Tree shaking removes unused imports likeunusedFuncto reduce bundle size.Final Answer:
unusedFuncis removed from the final bundle -> Option DQuick Check:
Unused imports removed by tree shaking [OK]
- Assuming all imports stay in bundle
- Thinking tree shaking removes used code
- Believing tree shaking is manual
Solution
Step 1: Analyze import style impact on bundle size
Importing whole libraries (likeimport * as lodash from 'lodash') prevents tree shaking from removing unused parts.Step 2: Check other options for relevance
Options B, C, and D either reduce bundle size or are unrelated to bundle size increase.Final Answer:
You imported entire libraries instead of specific functions -> Option AQuick Check:
Whole library imports block tree shaking [OK]
- Thinking production mode increases bundle
- Believing manual removal is needed
- Ignoring import style effects
Solution
Step 1: Use dynamic imports to load components only when needed
This helps tree shaking and code splitting to reduce initial bundle size.Step 2: Enable production build with bundle analysis
This activates tree shaking and lets you see what code is included to optimize further.Final Answer:
Use dynamic imports for components and enable production build with bundle analysis -> Option AQuick Check:
Dynamic imports + production build = smaller bundles [OK]
- Importing all components globally
- Disabling production optimizations
- Relying on manual file deletion
Ready to prove it?
Pass the test and this pattern moves unknown
