Skip to content

Commit 17e1dd5

Browse files
committed
Use Lazy Fire to delay importing Firebase modules
1 parent 3fa82af commit 17e1dd5

File tree

5 files changed

+247
-149
lines changed

5 files changed

+247
-149
lines changed

Diff for: CHANGELOG.md

+23
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,29 @@ See below for details.
2323

2424
### Changed
2525

26+
- **BREAKING**: Use `lazyfire` for ESM-enabled web environments to make Firebase modules load on demand. It ensures maximum performance, but requires installation of additional dependency and change of application initialization.
27+
28+
So, if you're using webpack or another ESM-enabled bundler, install `lazyfire`:
29+
30+
```bash
31+
npm install lazyfire --save
32+
# Or using Yarn:
33+
yarn add lazyfire
34+
```
35+
36+
And then change `firebase.initializeApp` to `configureApp`:
37+
38+
```diff
39+
-import * as firebase from 'firebase/app'
40+
-import 'firebase/firestore'
41+
+import { configureApp } from 'lazyfire'
42+
43+
-firebase.initializeApp({
44+
+configureApp({
45+
// Firebase app configuration
46+
})
47+
```
48+
2649
- **BREAKING**: Make TypeScript 3.8 the minimal supported version.
2750

2851
- **BREAKING**: `AnyUpdateValue` type was removed.

Diff for: README.md

+30-4
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,53 @@ yarn add typesaurus
3434
_Note that Typesaurus requires `firebase` package to work in the web environment and `firebase-admin` to work in Node.js. These packages aren't listed as dependencies,
3535
so that they won't install automatically along with the Typesaurus package._
3636

37+
Additionally, when using with ESM-enabled bundler (like webpack), you'll need to install `lazyfire` that enables asynchronous importing of Firebase modules in the web environment:
38+
39+
```sh
40+
npm install lazyfire --save
41+
# Or using Yarn:
42+
yarn add lazyfire
43+
```
44+
45+
[Read more about Lazy Fire](https://github.com/kossnocorp/lazyfire).
46+
3747
## Configuration
3848

39-
Typesaurus does not require additional configuration, however **when using with ESM-enabled bundler, you should transpile `node_modules`**. TypeScript preserves many modern languages features when it compiles to ESM code. So if you have to support older browsers, use Babel to process the dependencies code
49+
Typesaurus does not require additional configuration, however **when using with ESM-enabled bundler (like webpack), you should transpile `node_modules`**. TypeScript preserves many modern languages features when it compiles to ESM code. So if you have to support older browsers, use Babel to process the dependencies code.
4050

4151
## Get started
4252

4353
### Initialization
4454

45-
To start working with Typesaurus, initialize Firebase normally.
55+
To start working with Typesaurus, you'll need to initialize Firebase.
4656

47-
In the web environment ([see Firebase docs](https://firebase.google.com/docs/web/setup#add-sdks-initialize)):
57+
#### Web environment
58+
59+
In the web environment when using ESM-enabled bundler (like webpack), use [Lazy Fire](https://github.com/kossnocorp/lazyfire) to configure the Firebase application:
60+
61+
```ts
62+
import { configureApp } from 'lazyfire'
63+
64+
configureApp({
65+
// Firebase app configuration
66+
})
67+
```
68+
69+
#### Legacy web environment
70+
71+
In the web environment with ESM-disabled ([see Firebase docs](https://firebase.google.com/docs/web/setup#add-sdks-initialize)):
4872

4973
```ts
5074
import * as firebase from 'firebase/app'
5175
import 'firebase/firestore'
5276

5377
firebase.initializeApp({
54-
// Project configuration
78+
// Firebase app configuration
5579
})
5680
```
5781

82+
#### Node.js environment
83+
5884
In Node.js ([see Firebase docs](https://firebase.google.com/docs/admin/setup#initialize-sdk)):
5985

6086
```ts

Diff for: package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typesaurus",
3-
"version": "8.0.0-alpha.22",
3+
"version": "8.0.0-alpha.23",
44
"description": "Type-safe ODM for Firestore",
55
"keywords": [
66
"Firebase",
@@ -26,7 +26,7 @@
2626
"babel-jest": "^26.6.3",
2727
"babel-loader": "^8.2.2",
2828
"babel-preset-power-assert": "^3.0.0",
29-
"firebase": "8.2.5",
29+
"firebase": "8",
3030
"firebase-admin": "9.4.2",
3131
"firebase-tools": "^9.3.0",
3232
"jest": "^26.6.3",
@@ -37,6 +37,7 @@
3737
"karma-mocha": "^1.3.0",
3838
"karma-sourcemap-loader": "^0.3.8",
3939
"karma-webpack": "4",
40+
"lazyfire": "^0.2.0",
4041
"mocha": "^6.2.0",
4142
"nanoid": "^3.1.20",
4243
"power-assert": "^1.6.1",

Diff for: src/adaptor/browser/lazy.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
import type { DocOptions, ServerTimestampsStrategy } from '../../types'
66
import { getAll } from '../utils'
77
import type firebase from 'firebase'
8+
import { ensureApp } from 'lazyfire'
89

910
export default async function adaptor() {
10-
const { default: firebase } = await import('firebase/app')
11+
const { app, firebase } = await ensureApp()
1112
await import('firebase/firestore')
1213

13-
const firestore = firebase.firestore()
14+
const firestore = app.firestore()
1415
// At the moment, the browser's Firestore adaptor doesn't support getAll.
1516
// Get rid of the fallback when the issue is closed:
1617
// https://github.com/firebase/firebase-js-sdk/issues/1176

0 commit comments

Comments
 (0)