-
Notifications
You must be signed in to change notification settings - Fork 928
v9 export issues with Vite #5140
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
Comments
I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight. |
Oops, my previous comment is also out-of-date. The real issue here is that Vite failed to automatically externalize firebase v9 during SSR. For the Vite issue here, because it can be worked around by configuring I'll try to improve the heuristics anyway. |
Workaround:
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
// hydrate the <div id="svelte"> element in src/app.html
target: '#svelte',
vite: {
ssr: {
external: ['firebase']
}
}
},
};
export default config; |
This workaround works perfectly well, thank you @sodatea! |
It works on the dev server but it throws the commonjs error when you try to build the project |
A long while back, i decided against Vite for production build and went with Rollup, instead. Things likely have changed, but that’s a suitable and proven workflow. Note that Vite uses Rollup underneath, so it’s just bypassing one layer. The downside? More custom configuration is needed. Watching this space. If you manage to get Firebase modular + Vite builds working, I might reconsider my choice. |
It works but you have to change all "import {} from 'firebase/..'" to "import {} from '@firebase/..'". Internally the first import just does a "export * from '@firebase/..'". |
Well, this one gets a little bit tricky.
which means, in Node Apparently, the CJS bundle isn't statically analyzable, thus no named exports available to the users. |
I don't know if SvelteKit supports emitting CJS bundles for SSR. If it does, the abovementioned workaround should suffice. If not, I think the issue needs to be addressed in firebase. |
For the firebase team, there are several ways to fix this issue:
Note that the extensions need to be changed accordingly. |
First, thanks for the report. Some context - Firebase has different implementations for Nodejs and Browser, so we have separate bundles for them. Currently, we only provide cjs bundle for Nodejs, and only esm bundle for Browser. @sodatea supporting |
@Feiyang1 Have you considered limiting the But for imports, you could document that people should use the scoped packages directly. This is the way I’ve grown to use them, and it’s mentioned in this issue. But it’s not in the docs. |
@Feiyang1 I just took a look at What's breaking currently is that Node.js failed to convert CommonJS re-exports to ES module re-exports (due to the bug linked above). |
Sharing here from the linked SvelteKit issue. |
@sodatea Thanks! I think I have a good understanding of the issue now. The issue should really be addressed in The downside is the build becomes more complicated in order to address dual package hazard. For now, I'm only changing the @akauppi I can't say I haven't thought about it, but it's a really big change from the documentation perspective, and potentially confusing for some developers (you import different packages than what you installed). And for the vast majority of, if not all, use cases, there shouldn't be any difference in using @jthegedus Thanks for sharing, but I'm afraid the info is misleading. The Firebase JS SDK can be totally used for SSR. In fact, many people are doing that today and AngularFire has been using the JS SDK for SSR for a long time, and it's not changing with v9. If there is any issue with SSR with the v9 SDK, please open issues with us, and we will try to fix them. |
@Feiyang1 I’ve gathered some ES packaging experience with Rollup. Would you like me to try a review/revise of the build setup for the Admin SDK? I’m thinking ES first and creating CJS from those sources, via Rollup. I have time for this this week, but would like to sync with Firebase before and after the task, to understand the aims of the product. |
The topic is more complex than your statements imply and simply suggesting it is fine without discussing the implications is more misleading in my opinion. I do not use the AngularFire library, but I would image it handles the specific challenges for which SSR becomes difficult that I am raising. For people not using the AngularFire library and just using the base SDKs, the interactions of SSR, Authentication and tokens, Security Rules & Firestore queries should be understood before simply using the Client SDK in SSR context. |
@akauppi Thanks for the offer, I'd start with chatting with @hiranya911 who is the maintainer of the Admin SDK. @jthegedus These are all valid points regarding the caveats for SSR. I just wanted to point out that the blanket statement You can use either the JS SDK or the Admin SDK to do SSR with pros and cons(limitations) respectively. Which one to use is really based on your specific use case. |
Precisely. I am finding that few people actually know what these specific pros/cons are, and people are assuming the result of using the JS SDK in SSR without addressing the limitations is the same as it is on the client, which is not the case. Back to the OP issue, I observe this even when not using the SDK in SSR contexts in the SvelteKit dev server for |
This should have been fixed in |
This will probably need to be raised in the Admin SDK repo as well, but I get a deprecation warning for the exports folder mapping - https://nodejs.org/api/deprecations.html#DEP0148 |
@jthegedus Is it only an issue on the Admin SDK? I just checked our package.json, and we don't use trailing '/'s anywhere. See https://unpkg.com/browse/[email protected]/package.json |
This is the full warning output:
Depedencies of dependencies it seems. Though a warning I did not receive on |
[REQUIRED] Describe your environment
Operating System version: macOS 11
Browser version: Safari 14
Firebase SDK version: 9
Firebase Product: all
[REQUIRED] Describe the problem
I am working with SvelteKit which uses Vite for bundling and I am not sure if it's the firebase "export * from '@firebase/app';" or if it's Vite. But when importing firebase like this "import { initializeApp } from 'firebase/app';" Vite says it's a commonjs package. If it's a dev depencency and trying to build it reports as followed:
For the dev server it works fine. When installing firebase as a normal dependency and running build everything works fine but when trying to run the dev server this error pops up:
Like I said I am not sure if it's because of the all export or if it's Vite but changing the "import { initializeApp } from 'firebase/app';" to "import { initializeApp } from '@firebase/app';" doesn't throw an error in both cases.
The text was updated successfully, but these errors were encountered: