JavaScript Dynamic Import with Tree Shaking (via async)

Apr 12, 2022

Static import

import { initializeApp, getApps } from "firebase/app"

Dynamic import

async getApp() {  const { initializeApp, getApps } = await import("firebase/app")  const apps = getApps()  let firebaseApp = null  let app = null  if (!apps.length) {    app = initializeApp(firebaseConfig)  } else {    app = apps[0]  }  return app }

Based on The unexpected impact of dynamic imports on tree shaking and this, seems like dynamic import doesn't support tree-shaking.

Alternative solution is create a local module, edit localFirebase.js.

export { initializeApp, getApps } from "firebase/app"


const { initializeApp, getApps } = await import("localFirebase.js")

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.