📖 Scenario: You are building a Vue app that uses several components. You want to see which parts of your code are included in the final bundle and remove unused code to make your app faster.
🎯 Goal: Learn how to set up bundle analysis and apply tree shaking in a Vue project to optimize the final bundle size.
📋 What You'll Learn
Create a Vue app with multiple components
Add a configuration variable to enable bundle analysis
Use tree shaking by importing only needed components
Complete the Vue app with bundle analyzer plugin setup
💡 Why This Matters
🌍 Real World
Bundle analysis helps developers see what code is included in their app's final bundle. Tree shaking removes unused code, making apps faster and smaller.
💼 Career
Understanding bundle analysis and tree shaking is important for frontend developers to optimize app performance and load times.
Progress0 / 4 steps
1
Create Vue components for the app
Create three Vue components named Header.vue, Footer.vue, and MainContent.vue each exporting a simple template with a <template> tag and a root <div> containing text: 'Header Section', 'Footer Section', and 'Main Content Section' respectively.
Vue
Hint
Each component should have a <template> with a <div> containing the exact text.
2
Add a configuration variable to enable bundle analysis
In your vite.config.js file, create a constant named enableBundleAnalysis and set it to true to enable bundle analysis.
Vue
Hint
Just create a constant named enableBundleAnalysis and set it to true.
3
Import only needed components to apply tree shaking
In your main Vue app file App.vue, import only Header.vue and MainContent.vue components using import Header from './Header.vue' and import MainContent from './MainContent.vue'. Use these components in the template and do not import Footer.vue.
Vue
Hint
Import only the two components you want to use and include them in the template.
4
Complete vite.config.js with bundle analyzer plugin
In vite.config.js, import visualizer from 'rollup-plugin-visualizer'. Then export the config using defineConfig from 'vite'. Inside the config, add plugins array that includes visualizer() only if enableBundleAnalysis is true.
Vue
Hint
Use the spread operator to conditionally add the visualizer plugin to the plugins array.
Practice
(1/5)
1. What is the main purpose of tree shaking in a Vue project?
easy
A. To change the app's user interface
B. To remove unused code from the final bundle
C. To increase the size of the app bundle
D. To add more features automatically
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 B
Quick Check:
Tree shaking = remove unused code [OK]
Hint: Tree shaking cuts unused code to shrink bundles [OK]
Common Mistakes:
Thinking tree shaking adds features
Believing it increases bundle size
Confusing it with UI changes
2. Which of the following is the correct way to enable bundle analysis in a Vue CLI project?
easy
A. Add analyze: true in vue.config.js
B. Use npm run serve --analyze
C. Run vue-cli-service build --report
D. Set treeShake: false in package.json
Solution
Step 1: Recall Vue CLI bundle analysis command
The Vue CLI supports bundle analysis with the --report flag 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:
Run vue-cli-service build --report -> Option C
Quick Check:
Bundle analysis command = build --report [OK]
Hint: Use --report flag with build for analysis [OK]
Common Mistakes:
Adding unsupported config keys
Using serve instead of build
Disabling tree shaking by mistake
3. Given this Vue component import code, what will tree shaking do during production build?