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

Fix Firestore exports for Node #5532

Merged
merged 9 commits into from
Oct 19, 2021
Merged

Fix Firestore exports for Node #5532

merged 9 commits into from
Oct 19, 2021

Conversation

hsubox76
Copy link
Contributor

Create require and import subfields for node exports where the latter points to mjs ESM builds for Node. Rename main-esm fields so rollup builds ESM node bundles with mjs extension.

Fixes #5499

@changeset-bot
Copy link

changeset-bot bot commented Sep 22, 2021

🦋 Changeset detected

Latest commit: c5e701f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@firebase/firestore Minor
firebase Patch
@firebase/firestore-compat Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions
Copy link
Contributor

github-actions bot commented Sep 22, 2021

Changeset File Check ✅

  • No modified packages are missing from the changeset file.
  • No changeset formatting errors detected.

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Sep 22, 2021

Size Analysis Report

Affected Products

Diffs between base commit (e456d00) and head commit (39be567) are too large (397,500 characters) to display.

Please check below links to see details from the original test log.

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Sep 23, 2021

Binary Size Report

Affected SDKs

  • @firebase/analytics

    Type Base (e456d00) Head (39be567) Diff
    browser 18.9 kB 18.9 kB +40 B (+0.2%)
    esm5 23.5 kB 23.5 kB +37 B (+0.2%)
    main 24.6 kB 24.6 kB +41 B (+0.2%)
    module 18.9 kB 18.9 kB +40 B (+0.2%)
  • @firebase/app

    Type Base (e456d00) Head (39be567) Diff
    browser 7.69 kB 7.74 kB +46 B (+0.6%)
    esm5 9.32 kB 9.37 kB +43 B (+0.5%)
    main 10.2 kB 10.2 kB +47 B (+0.5%)
    module 7.69 kB 7.74 kB +46 B (+0.6%)
  • @firebase/auth/cordova

    Type Base (e456d00) Head (39be567) Diff
    browser 178 kB 178 kB +147 B (+0.1%)
    module 178 kB 178 kB +147 B (+0.1%)
  • @firebase/auth/internal

    Type Base (e456d00) Head (39be567) Diff
    browser 163 kB 163 kB +135 B (+0.1%)
    esm5 211 kB 211 kB +147 B (+0.1%)
    main 178 kB 178 kB +153 B (+0.1%)
    module 163 kB 163 kB +135 B (+0.1%)
  • @firebase/auth/react-native

    Type Base (e456d00) Head (39be567) Diff
    browser 143 kB 143 kB +43 B (+0.0%)
    module 143 kB 143 kB +43 B (+0.0%)
  • @firebase/database

    Type Base (e456d00) Head (39be567) Diff
    browser 247 kB 247 kB +40 B (+0.0%)
    esm5 275 kB 275 kB +37 B (+0.0%)
    main 280 kB 280 kB +41 B (+0.0%)
    module 247 kB 247 kB +40 B (+0.0%)
  • @firebase/firestore

    Type Base (e456d00) Head (39be567) Diff
    browser 225 kB 225 kB +303 B (+0.1%)
    esm5 282 kB 282 kB +313 B (+0.1%)
    main 512 kB 423 kB -89.3 kB (-17.4%)
    module 225 kB 225 kB +303 B (+0.1%)
    react-native 225 kB 225 kB +302 B (+0.1%)
  • @firebase/firestore-lite

    Type Base (e456d00) Head (39be567) Diff
    browser 71.6 kB 71.6 kB +48 B (+0.1%)
    esm5 84.7 kB 84.8 kB +45 B (+0.1%)
    main 147 kB 123 kB -23.7 kB (-16.1%)
    module 71.6 kB 71.6 kB +48 B (+0.1%)
    react-native 71.8 kB 71.8 kB +50 B (+0.1%)
  • @firebase/functions

    Type Base (e456d00) Head (39be567) Diff
    browser 8.79 kB 8.85 kB +56 B (+0.6%)
    esm5 10.9 kB 10.9 kB +53 B (+0.5%)
    main 11.6 kB 11.7 kB +57 B (+0.5%)
    module 8.79 kB 8.85 kB +56 B (+0.6%)
  • @firebase/installations

    Type Base (e456d00) Head (39be567) Diff
    browser 17.2 kB 17.3 kB +40 B (+0.2%)
    esm5 22.3 kB 22.3 kB +37 B (+0.2%)
    main 23.1 kB 23.1 kB +41 B (+0.2%)
    module 17.2 kB 17.3 kB +40 B (+0.2%)
  • @firebase/messaging

    Type Base (e456d00) Head (39be567) Diff
    browser 20.6 kB 20.8 kB +141 B (+0.7%)
    esm5 26.0 kB 26.1 kB +134 B (+0.5%)
    main 26.7 kB 26.8 kB +126 B (+0.5%)
    module 20.6 kB 20.8 kB +141 B (+0.7%)
  • @firebase/performance

    Type Base (e456d00) Head (39be567) Diff
    browser 28.4 kB 28.4 kB +40 B (+0.1%)
    esm5 30.1 kB 30.1 kB +37 B (+0.1%)
    main 30.5 kB 30.6 kB +41 B (+0.1%)
    module 28.4 kB 28.4 kB +40 B (+0.1%)
  • @firebase/remote-config

    Type Base (e456d00) Head (39be567) Diff
    browser 18.8 kB 18.8 kB +40 B (+0.2%)
    esm5 23.7 kB 23.7 kB +37 B (+0.2%)
    main 24.9 kB 24.9 kB +41 B (+0.2%)
    module 18.8 kB 18.8 kB +40 B (+0.2%)
  • @firebase/storage

    Type Base (e456d00) Head (39be567) Diff
    browser 52.1 kB 52.2 kB +43 B (+0.1%)
    esm5 58.3 kB 58.4 kB +40 B (+0.1%)
    main 53.5 kB 53.6 kB +126 B (+0.2%)
    module 52.1 kB 52.2 kB +43 B (+0.1%)
  • firebase

    Click to show 27 binary size changes.
    Type Base (e456d00) Head (39be567) Diff
    firebase-analytics-compat.js 25.9 kB 26.0 kB +94 B (+0.4%)
    firebase-analytics.js 107 kB 107 kB +294 B (+0.3%)
    firebase-app-compat.js 21.2 kB 21.3 kB +28 B (+0.1%)
    firebase-app.js 59.0 kB 59.2 kB +155 B (+0.3%)
    firebase-auth-compat.js 122 kB 122 kB +121 B (+0.1%)
    firebase-auth-cordova.js 459 kB 460 kB +502 B (+0.1%)
    firebase-auth-react-native.js 430 kB 430 kB +152 B (+0.0%)
    firebase-auth.js 409 kB 410 kB +382 B (+0.1%)
    firebase-compat.js 748 kB 749 kB +756 B (+0.1%)
    firebase-database-compat.js 168 kB 168 kB +63 B (+0.0%)
    firebase-database.js 603 kB 603 kB +149 B (+0.0%)
    firebase-firestore-compat.js 277 kB 277 kB +298 B (+0.1%)
    firebase-firestore-lite.js 244 kB 244 kB +135 B (+0.1%)
    firebase-firestore.js 762 kB 763 kB +946 B (+0.1%)
    firebase-functions-compat.js 7.90 kB 7.94 kB +47 B (+0.6%)
    firebase-functions.js 30.5 kB 30.6 kB +171 B (+0.6%)
    firebase-messaging-compat.js 37.8 kB 37.9 kB +140 B (+0.4%)
    firebase-messaging-sw.js 102 kB 102 kB +141 B (+0.1%)
    firebase-messaging.js 100 kB 101 kB +403 B (+0.4%)
    firebase-performance-compat.js 30.7 kB 30.8 kB +85 B (+0.3%)
    firebase-performance-standalone-compat.es2017.js 82.4 kB 82.5 kB +80 B (+0.1%)
    firebase-performance-standalone-compat.js 53.9 kB 54.0 kB +127 B (+0.2%)
    firebase-performance.js 118 kB 119 kB +298 B (+0.3%)
    firebase-remote-config-compat.js 27.5 kB 27.5 kB +83 B (+0.3%)
    firebase-remote-config.js 108 kB 109 kB +311 B (+0.3%)
    firebase-storage-compat.js 38.1 kB 38.2 kB +56 B (+0.1%)
    firebase-storage.js 140 kB 141 kB +393 B (+0.3%)

Test Logs

Copy link
Contributor

@egilmorez egilmorez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

"node": "./dist/lite/index.node.cjs.js",
"node": {
"require": "./dist/lite/index.node.cjs.js",
"import": "./dist/lite/index.node.esm2017.js"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import needs to point to a file with mjs extension.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, fixed.

"node": "./dist/index.node.cjs.js",
"node": {
"require": "./dist/index.node.cjs.js",
"import": "./dist/index.node.mjs"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is the build rule for creating this file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It gets the name from whatever is in the main-esm field in package.json

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So here is the build config

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Did you test it in Nodejs - running import {} from @firebase/firestore ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested it with svelte-kit's SSR(?) build step using firebase/firestore and it works. It does not work when just creating a raw JS file and running it in Node on the command line and it also doesn't work when using @firebase/firestore in either case. The error is odd:

node --experimental-json-modules node.js
(node:78153) ExperimentalWarning: Importing JSON modules is an experimental feature. This feature could change at any time
file:///Users/chholland/repos/sveltekit-firebase/node_modules/@firebase/firestore/dist/index.node.mjs:8
import { version as version$2 } from '@grpc/grpc-js/package.json';
         ^^^^^^^
SyntaxError: The requested module '@grpc/grpc-js/package.json' does not provide an export named 'version'

I think it's not able to deal with a dependency (grpc-js) still being in cjs format, and svelte-kit uses esbuild so something in the build step is fixing whatever the problem is, unless you import from @firebase/firestore directly in which case it doesn't work. So some special steps are needed to make it work in Node but I'm not sure what they are.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added the workaround in Option 2 here: https://www.stefanjudis.com/snippets/how-to-import-json-files-in-es-modules-node-js/#option-2%3A-leverage-the-commonjs-%60require%60-function-to-load-json-files

It required some changes to tsconfig.json to avoid some TS compiler complaints but it compiles correctly and works in both a Node and a Sveltekit test project. It also works in both projects if I don't change tsconfig and I just tsignore the offending lines (import module from 'module' and const require = module.createRequire(import.meta.url);) so any thoughts on which way is better?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use ts-ignore with comments here because we don't want these changes to affect browser code which shares the same tsconfig with Node code.

@Feiyang1
Copy link
Member

Feiyang1 commented Oct 4, 2021

LGTM. Does cjs build work correctly ?

@hsubox76
Copy link
Contributor Author

hsubox76 commented Oct 5, 2021

Yes, CJS build works in a raw Node 12 project.

@hsubox76
Copy link
Contributor Author

hsubox76 commented Oct 5, 2021

@schmidt-sebastian This is the PR that would require dropping Node 8 support (so that we can use import.meta for the require workaround - see earlier comments). I believe you suggested that would be ok with a minor bump.

@schmidt-sebastian
Copy link
Contributor

We do have precedent for that. I believe we ran some numbers last time that basically showed that there were no users on Node 6 that used an updated SDK version. I will check tomorrow if I can find some details.

@benmccann
Copy link

Thank you for this! This looks like a good change to me. I had actually suggested making such a change here: #4846 (comment)

I'm the primary maintainer of SvelteKit at the moment and happy to advise on getting SvelteKit better supported if there's anything I can offer insights on. SvelteKit uses Vite which is used by Vue, Astro, and others. SvelteKit leverages the server-side rendering features of Vite much more heavily than other platforms at the moment and so we're the first to hit some of these issues. I expect that as these features are finalized (SSR is in beta) and rolled out to the Vue user base that these issues will start popping up much more regularly since there are many more Vue users than SvelteKit users.

Would it be possible make this change to all Firebase packages? E.g. #4846 is this same issue, but reported for firebase/functions

Copy link
Member

@Feiyang1 Feiyang1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, pending @schmidt-sebastian's approval.

@Feiyang1
Copy link
Member

@benmccann Yes, we are planning to do that.

Copy link
Contributor

@schmidt-sebastian schmidt-sebastian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our engines field is already "node": "^8.13.0 || >=10.10.0"

@hsubox76
Copy link
Contributor Author

Our engines field is already "node": "^8.13.0 || >=10.10.0"

Oh, should I also get rid of the ^8.13.0 in this PR?

@@ -2,7 +2,7 @@
"name": "@firebase/firestore",
"version": "3.1.0",
"engines": {
"node": "^8.13.0 || >=10.10.0"
"node": ">=10.10.0"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing Node 8 from this line.

@hsubox76 hsubox76 merged commit 4d36404 into master Oct 19, 2021
@hsubox76 hsubox76 deleted the ch-firestore-exports branch October 19, 2021 16:52
This was referenced Oct 19, 2021
@firebase firebase locked and limited conversation to collaborators Nov 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Firebase v9.0.1 with Sveltekit node adapter – build failure
6 participants