0
0
Firebasecloud~10 mins

Modular SDK (v9+) tree-shaking in Firebase - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Modular SDK (v9+) tree-shaking
Import only needed functions
Build app with modular imports
Bundle tool analyzes imports
Remove unused code (tree-shaking)
Deploy smaller optimized bundle
App runs faster with less code
The modular SDK lets you import only what you need. The bundler removes unused parts, making your app smaller and faster.
Execution Sample
Firebase
import { initializeApp } from 'firebase/app';
import { getAuth } from 'firebase/auth';

const app = initializeApp(config);
const auth = getAuth(app);
This code imports only the app initialization and auth functions, so unused Firebase features are excluded from the final bundle.
Process Table
StepActionImport UsedBundle Size ImpactResult
1Import initializeAppinitializeAppIncludes core app codeCore Firebase initialized
2Import getAuthgetAuthIncludes auth code onlyAuth module ready
3No other importsNoneUnused code removedSmaller bundle size
4Bundle tool analyzes importsinitializeApp, getAuthTree-shaking removes unusedOptimized bundle created
5Deploy appOptimized importsFaster load and runApp runs with minimal code
💡 All unused Firebase modules excluded, final bundle contains only imported features.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
appundefinedinitialized with configinitializedinitializedinitialized
authundefinedundefinedinitialized with appinitializedinitialized
Key Moments - 3 Insights
Why does importing only specific functions reduce bundle size?
Because the bundler removes unused code not imported, as shown in execution_table step 3 where unused modules are excluded.
What happens if you import the whole Firebase SDK instead of modular imports?
The bundle includes all Firebase code, making it larger and slower, unlike the optimized modular imports in step 4.
How does tree-shaking know which code to remove?
It analyzes the import statements and removes code not referenced, as described in step 4 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is included after importing getAuth at step 2?
AOnly core app code
BAuth module code only
CAll Firebase modules
DNo code included yet
💡 Hint
Check the 'Import Used' and 'Bundle Size Impact' columns at step 2 in execution_table.
At which step does the bundler remove unused Firebase code?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Look for 'Unused code removed' in the 'Bundle Size Impact' column in execution_table.
If you added import for Firestore, how would the bundle size change?
ABundle size increases
BBundle size stays the same
CBundle size decreases
DBundle size is unpredictable
💡 Hint
Adding imports adds code, so see how importing getAuth increased bundle size in execution_table step 2.
Concept Snapshot
Modular SDK (v9+) uses selective imports.
Import only needed Firebase functions.
Bundler removes unused code (tree-shaking).
Result: smaller, faster app bundles.
Avoid importing entire SDK to optimize performance.
Full Transcript
The Modular SDK in Firebase version 9 and above allows developers to import only the specific functions they need, such as initializeApp and getAuth. This selective importing helps bundlers identify unused code and remove it during the build process, a technique called tree-shaking. As a result, the final app bundle is smaller and loads faster. The execution steps show importing initializeApp first, then getAuth, with the bundler removing all other unused Firebase modules before deployment. This approach improves app performance by including only necessary Firebase features.