Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@firebase/database included in the final build while I only use firestore #489

Open
MaciejCaputa opened this issue Nov 23, 2021 · 11 comments
Assignees

Comments

@MaciejCaputa
Copy link

In my app I only provide firestore and I don't use a real-time database but I see it increasing my bundle size. Any advice? (I don't have it imported anywhere).

Screenshot 2021-11-23 at 23 58 47

@jhuleatt
Copy link
Collaborator

jhuleatt commented Nov 24, 2021

Hi @MaciejCaputa, I think this might be related to an issue I'm investigating #488 (comment).

Just curious, what build tool are you using, and can you please confirm that you have tree-shaking enabled in your build?

@robertn702
Copy link

Hi @jhuleatt I am still running into this issue using the following. Webpack is properly tree-shaking other libraries but reactfire is still loading firestore and storage.

Dependencies

The only firebase imports we are using are "firebase/auth" and "firebase/app".

This is our treemap of the firebase bundle from Lighthouse:

@alexduhem
Copy link

Hey there, got the same issue, I just pulled all my hairs out my head to know what libraries was causing this.

finally I just did a little hack with the webpack config by using https://webpack.js.org/configuration/externals/

config.externals = { 'firebase/database': 'root Math', };

this hack force remove the firebase/database dependency out of the bundle.
why putting Math ? because webpack will add a small line in the bundle : "e.exports=math", so I used a builtin package to export to not crash the whole app

@jhuleatt
Copy link
Collaborator

Thanks all for the additional information. We've just published ReactFire 4.2.2, which is the first release with a new build process (#534) that includes a specific list of externals, and I believe solves this issue:

reactfire/vite.config.ts

Lines 13 to 34 in f6d2351

const externals = [
'react',
'firebase',
'firebase/app',
'firebase/firestore',
'firebase/firestore/lite',
'firebase/auth',
'firebase/functions',
'firebase/storage',
'firebase/database',
'firebase/remote-config',
'firebase/performance',
'@firebase/app',
'@firebase/firestore',
'@firebase/firestore/lite',
'@firebase/auth',
'@firebase/functions',
'@firebase/storage',
'@firebase/database',
'@firebase/remote-config',
'@firebase/performance',
];

@alexduhem or others - can you please give version 4.2.2 a try and see if it fixes this issue without the need for workarounds?

@robertn702
Copy link

Hi @jhuleatt, Thanks for the update. I upgraded from v4.2.1 to v4.2.2 and ran webpack-bundle-analyzer. It looks like the unused packages from @firebase have been removed, but now the reactfire bundle is massive.

Dependencies


Before

reactfire

./node_modules/reactfire/dist/reactfire.esm.js (15.42 KB)
./node_modules/reactfire/dist (15.42 KB)

@firebase

./node_modules/@firebase (175.17 KB)
./node_modules/@firebase/auth/dist/esm2017/index.js + 1 modules (concatenated) (74.85 KB)
./node_modules/@firebase/firestore/dist/index.esm2017.js + 1 modules (concatenated) (69.98 KB)
./node_modules/@firebase/storage/dist/index.esm2017.js (8.79 KB)
./node_modules/@firebase/app/dist/esm/index.esm2017.js (8.09 KB)
./node_modules/@firebase/util/dist/index.esm2017.js (7.46 KB)
./node_modules/@firebase/component/dist/esm/index.esm2017.js (4.3 KB)
./node_modules/@firebase/logger/dist/esm/index.esm2017.js (1.7 KB)
./node_modules/@firebase/auth/dist/esm2017 (74.85 KB)
./node_modules/@firebase/firestore/dist (69.98 KB)
./node_modules/@firebase/storage/dist (8.79 KB)
./node_modules/@firebase/app/dist/esm (8.09 KB)
./node_modules/@firebase/util/dist (7.46 KB)
./node_modules/@firebase/component/dist/esm (4.3 KB)
./node_modules/@firebase/logger/dist/esm (1.7 KB)

After

reactfire

./node_modules/reactfire/dist (249.97 KB)
./node_modules/reactfire/dist/index.js + 14 modules (concatenated) (249.97 KB)

@firebase

./node_modules/@firebase (100.38 KB)
./node_modules/@firebase/auth/dist/esm2017/index.js + 1 modules (concatenated) (74.85 KB)
./node_modules/@firebase/util/dist/index.esm2017.js (11.43 KB)
./node_modules/@firebase/app/dist/esm/index.esm2017.js (8.09 KB)
./node_modules/@firebase/component/dist/esm/index.esm2017.js (4.3 KB)
./node_modules/@firebase/logger/dist/esm/index.esm2017.js (1.7 KB)
./node_modules/@firebase/auth/dist/esm2017 (74.85 KB)
./node_modules/@firebase/util/dist (11.43 KB)
./node_modules/@firebase/app/dist/esm (8.09 KB)
./node_modules/@firebase/component/dist/esm (4.3 KB)
./node_modules/@firebase/logger/dist/esm (1.7 KB)

@jhuleatt jhuleatt self-assigned this Aug 18, 2022
@jhuleatt
Copy link
Collaborator

jhuleatt commented Aug 18, 2022

@robertn702 thanks for trying it out! That's a big surprise to me. According to bundlephobia, the bundle is smaller than 4.2.1, not bigger: https://bundlephobia.com/package/[email protected]. Any chance your 4.2.1 build reports rxjs or rxfire separately, where 4.2.2 might just report them all combined as reactfire?

I'll look into this a bit more. Thanks again!

cc @jamesdaniels

@Gbuomprisco
Copy link

Gbuomprisco commented Sep 16, 2022

Hi @jhuleatt, I am unsure if the issue is on Next.js or Reactifire's end, but when using both, everything from Firebase is pulled in the final bundle just by importing the main app provider in _app.tsx.

I have provided a repository to reproduce this issue:

git clone https://github.com/Gbuomprisco/reactfire-bundle-issue
npm i
npm run analyze

When finished, the browser will open the output below:

image

Any help would be super appreciated!

@Laityned
Copy link

I am facing the same issue as @Gbuomprisco

@Gbuomprisco
Copy link

@Laityned if it can help, I worked it around by extending the webpack config in the next.js config file like below:

webpack: (config, { isServer }) => {
    // we remove unnecessary Firebase packages
    // only in production due to tree shaking
    if (isProduction) {
      decorateConfigWithFirebaseExternals(config);
    }

    return config;
  },
function decorateConfigWithFirebaseExternals(config) {
  config.externals = [
    ...(config.externals ?? []),
    {
      'firebase/functions': 'root Math',
      'firebase/database': 'root Math',
      'firebase/performance': 'root Math',
      'firebase/remote-config': 'root Math'
    },
  ];
}

@Laityned
Copy link

Thank you for the work around. I will definitely use it.
Furthermore, I think reactfire should still address the root cause of this problem.

I could help with a PR, if anyone has a clue why it is not tree shaking out firebase.

@Gbuomprisco
Copy link

Gbuomprisco commented Nov 23, 2022

The library is basically unmaintained, which is sad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants