Using Firebase with webpack? You might be able to save 220kb
MARCH 2020 UPDATE: The new Firebase JS SDK Alpha makes your bundle up to 80% smaller! It now fully adopts tree-shaking and lets you import only what you truly need.
In a lot of tutorials I came across the comfortable solution of just adding import * as firebase from ‘firebase’ for your firebase import. As it turns out, there are 4 modular packages for Firebase. Here they are along with their sizes:
- firebase/app: 22.1kb
- firebase/auth: 147.6kb
- firebase/database: 139.8kb
- firebase/storage: 51.2kb
Usually you might not need all of these Firebase services and can
import just what is needed — with the help of webpack’s Tree Shaking.
import * as firebase from 'firebase';
import * as firebase from 'firebase/app'; import 'firebase/storage'; import 'firebase/database'; // ... firebase.initializeApp(firebaseConfig);
This will tell webpack (or whatever bundler you use) that they should also include storage and database. You can then still use
firebase.storage().ref(...) in your code.
Let’s assume you only want to take advantage of Firebase’s great Database. Instead of adding 360.7kb, you’d only have to add the firebase/database that imports 139.8kb.
Resulting in 220.9kb saved (61%)!