Bird
Raised Fist0
Vueframework~20 mins

Bundle analysis and tree shaking in Vue - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Bundle Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
What is the main purpose of tree shaking in Vue projects?
Tree shaking helps optimize Vue applications by:
AAdding extra debugging information to the bundle
BAutomatically updating Vue components when data changes
CRemoving unused code from the final bundle to reduce size
DImproving CSS styling by removing unused styles
Attempts:
2 left
💡 Hint

Think about how to make your app load faster by cutting down code.

❓ component_behavior
intermediate
1:30remaining
What will the bundle size be if you import only one function from a large utility library in Vue?
Consider you import only the debounce function from a large utility library in your Vue component. How does tree shaking affect the bundle size?
AThe bundle includes only the <code>debounce</code> function and its dependencies
BThe entire utility library is included in the bundle
CNo code from the utility library is included
DThe bundle size doubles because of the import
Attempts:
2 left
💡 Hint

Tree shaking removes unused parts of libraries.

🔧 Debug
advanced
2:00remaining
Why does your Vue bundle include unused components despite tree shaking?
You notice your Vue app bundle is large and includes components you never use. Which of these is the most likely cause?
AYou used <code>v-if</code> to conditionally render components
BYou used dynamic imports with <code>defineAsyncComponent</code>
CYou only imported components inside <code>setup()</code> function
DYou imported components using a wildcard import like <code>import * as components from './components'</code>
Attempts:
2 left
💡 Hint

Think about how imports affect what code is included.

📝 Syntax
advanced
1:30remaining
Which import style enables tree shaking in Vue projects?
Select the import statement that allows tree shaking to remove unused code:
Aimport { ref, reactive } from 'vue'
Bimport * as Vue from 'vue'
Cconst Vue = require('vue')
Dimport Vue from 'vue'
Attempts:
2 left
💡 Hint

Tree shaking works best with named imports.

❓ state_output
expert
2:00remaining
What does the bundle analyzer show after enabling tree shaking in a Vue app?
After enabling tree shaking and analyzing your Vue app bundle, what change will you see in the bundle analyzer report?
AMore modules included due to added dependencies
BSmaller bundle size with fewer unused modules visible
CNo change in bundle size or modules
DBundle size increases because of added source maps
Attempts:
2 left
💡 Hint

Tree shaking removes unused code, so the bundle should shrink.

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

  1. Step 1: Understand tree shaking concept

    Tree shaking is a process that removes code not used in the app to reduce bundle size.
  2. Step 2: Identify its effect on Vue projects

    In Vue, tree shaking helps make the app faster by removing unused components or functions.
  3. Final Answer:

    To remove unused code from the final bundle -> Option B
  4. 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

  1. Step 1: Recall Vue CLI bundle analysis command

    The Vue CLI supports bundle analysis with the --report flag during build.
  2. Step 2: Check other options for correctness

    Options A, C, and D are incorrect or invalid ways to enable bundle analysis.
  3. Final Answer:

    Run vue-cli-service build --report -> Option C
  4. 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?
import { ref, reactive, unusedFunc } from 'vue';

export default {
  setup() {
    const count = ref(0);
    const state = reactive({ name: 'Vue' });
    return { count, state };
  }
}
medium
A. All imports including unusedFunc are included in the bundle
B. Only unusedFunc is included, others removed
C. Tree shaking does not affect imports
D. unusedFunc is removed from the final bundle

Solution

  1. Step 1: Identify used and unused imports

    The code uses ref and reactive, but unusedFunc is never used.
  2. Step 2: Understand tree shaking effect

    Tree shaking removes unused imports like unusedFunc to reduce bundle size.
  3. Final Answer:

    unusedFunc is removed from the final bundle -> Option D
  4. Quick Check:

    Unused imports removed by tree shaking [OK]
Hint: Unused imports get removed in production build [OK]
Common Mistakes:
  • Assuming all imports stay in bundle
  • Thinking tree shaking removes used code
  • Believing tree shaking is manual
4. You notice your Vue app bundle is larger than expected. Which of these is a likely cause related to tree shaking?
medium
A. You imported entire libraries instead of specific functions
B. You enabled production mode in build
C. You removed unused imports manually
D. You used ref and reactive properly

Solution

  1. Step 1: Analyze import style impact on bundle size

    Importing whole libraries (like import * as lodash from 'lodash') prevents tree shaking from removing unused parts.
  2. Step 2: Check other options for relevance

    Options B, C, and D either reduce bundle size or are unrelated to bundle size increase.
  3. Final Answer:

    You imported entire libraries instead of specific functions -> Option A
  4. Quick Check:

    Whole library imports block tree shaking [OK]
Hint: Import only needed parts to enable tree shaking [OK]
Common Mistakes:
  • Thinking production mode increases bundle
  • Believing manual removal is needed
  • Ignoring import style effects
5. You want to reduce your Vue app's bundle size by removing unused components and libraries. Which combination of steps is best?
hard
A. Use dynamic imports for components and enable production build with bundle analysis
B. Import all components globally and disable production optimizations
C. Manually delete unused files without build tools
D. Use development build and avoid code splitting

Solution

  1. Step 1: Use dynamic imports to load components only when needed

    This helps tree shaking and code splitting to reduce initial bundle size.
  2. Step 2: Enable production build with bundle analysis

    This activates tree shaking and lets you see what code is included to optimize further.
  3. Final Answer:

    Use dynamic imports for components and enable production build with bundle analysis -> Option A
  4. Quick Check:

    Dynamic imports + production build = smaller bundles [OK]
Hint: Combine dynamic imports and production build for best results [OK]
Common Mistakes:
  • Importing all components globally
  • Disabling production optimizations
  • Relying on manual file deletion
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard