How to configure vite/rollup to exclude firebase-admin from my clientside code?

Mát*_*lya 2 firebase typescript server-side-rendering firebase-admin vite

I've been using this vite-plugin-ssr with firebase functions happily until one day firebase-admin bailed on me. My setup was working until I deleted my node modules and reinstalled my dependencies. It seems that vite is bundling firebase-admin with the client even though there are no imports in clientside code referring to firebase-admin, only in the .page.server.ts files. How do I specify to vite or rollup to not bundle firebase-admin into the dist/client directory?

\n

I would really appreciate some pointers... I've spent 24hrs + on this one issue and at this point I am considering switching the SSR off, or copying the files to a fresh project. Basically I just want a clean setup with this simple stack:

\n
    \n
  • firebase functions
  • \n
  • vite + SSR (using vite-plugin-ssr)
  • \n
  • preact and typescript
  • \n
\n
> vite build "--config" "vite-ssr.config.ts"\n\nnode_modules/@firebase/util/node_modules/tslib/tslib.es6.js'fast-crc32c' is imported by fast-crc32c?commonjs-external, but could not be resolved \xe2\x80\x93 treating it as an external dependency\ntransforming (2472) \n\xe2\x9c\x93 2478 modules transformed.\n'default' is not exported by node_modules/firebase/app/dist/index.esm.js, imported by node_modules/firebase-admin/node_modules/@firebase/database/dist/index.esm.js\nfile: /Users/mateh/github/control-me/web-app/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.esm.js:1:7\n1: import firebase from 'firebase/app';\n          ^\n
Run Code Online (Sandbox Code Playgroud)\n

Things I have tried:

\n
    \n
  • Updated to the lasest version of all the firebase related dependencies
  • \n
  • Tried enabling allowSyntheticDefaultImports in tsconfig, did not make a difference
  • \n
  • manually edit the file in node_modules to import * from '@firebase/app'. The build will pass this way but the client will not be able to use the fast-crc32c module.
  • \n
  • 安装的fast-crc32c模块无济于事,firebase-admin代码中某处存在通用对象未定义错误,显然它不适合在浏览器的客户端上运行。
  • \n
  • 这两个函数和客户端都具有相同的依赖关系集。接下来,我尝试使用 rollup 为它起别名,以便 firebase安装在客户端 package.json 并且 firebase-admin is in the serverside functions package.json, issue still persists.
  • \n
\n

Mát*_*lya 6

将其添加到我的vite配置中修复了它:

build: {
    rollupOptions: {
      external: ['firebase-admin'],
Run Code Online (Sandbox Code Playgroud)