Bird
0
0

Which import style in Angular best supports tree shaking to exclude unused code?

easy📝 Syntax Q3 of 15
Angular - Performance Optimization
Which import style in Angular best supports tree shaking to exclude unused code?
Aimport * as Library from 'library';
Bimport { specificFunction } from 'library';
Cconst Library = require('library');
Dimport 'library';
Step-by-Step Solution
Solution:
  1. Step 1: Analyze import styles

    Named imports (import { specificFunction } from 'library';) allow bundlers to identify and remove unused exports.
  2. Step 2: Understand why others fail

    Namespace imports (B) and side-effect imports (D) include entire modules, preventing tree shaking.
  3. Final Answer:

    import { specificFunction } from 'library'; -> Option B
  4. Quick Check:

    Named imports enable tree shaking [OK]
Quick Trick: Use named imports for effective tree shaking [OK]
Common Mistakes:
  • Using namespace imports which include whole modules
  • Using require() which is CommonJS and not tree-shakeable
  • Importing modules only for side effects

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes