Skip to content

Commit 28a081b

Browse files
authored
Lazily load modules that require firebase-functions (#3)
1 parent 1fc249b commit 28a081b

File tree

3 files changed

+41
-20
lines changed

3 files changed

+41
-20
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"@types/chai": "^4.1.2",
2323
"@types/mocha": "^5.0.0",
2424
"chai": "^4.1.2",
25-
"firebase-functions": "https://storage.googleapis.com/functions-dev-preview/firebase-functions-1.0.0-rc4.tgz",
25+
"firebase-functions": "https://storage.googleapis.com/functions-dev-preview/firebase-functions-1.0.0-rc5.tgz",
2626
"mocha": "^5.0.5",
2727
"sinon": "^4.4.9",
2828
"tslint": "^5.8.0",

src/features.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { makeChange, wrap, mockConfig } from './main';
2+
import * as analytics from './providers/analytics';
3+
import * as crashlytics from './providers/crashlytics';
4+
import * as database from './providers/database';
5+
import * as firestore from './providers/firestore';
6+
import * as pubsub from './providers/pubsub';
7+
import * as storage from './providers/storage';
8+
9+
export interface FeaturesList {
10+
mockConfig: typeof mockConfig;
11+
wrap: typeof wrap;
12+
makeChange: typeof makeChange;
13+
analytics: typeof analytics;
14+
crashlytics: typeof crashlytics;
15+
database: typeof database;
16+
firestore: typeof firestore;
17+
pubsub: typeof pubsub;
18+
storage: typeof storage;
19+
}
20+
21+
export const features: FeaturesList = {
22+
mockConfig,
23+
wrap,
24+
makeChange,
25+
analytics,
26+
crashlytics,
27+
database,
28+
firestore,
29+
pubsub,
30+
storage,
31+
};

src/index.ts

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,19 @@
2121
// SOFTWARE.
2222

2323
import { AppOptions } from 'firebase-admin';
24+
import { merge } from 'lodash';
2425

2526
import { FirebaseFunctionsTest } from './lifecycle';
26-
import { makeChange, wrap, mockConfig } from './main';
27-
import * as analytics from './providers/analytics';
28-
import * as crashlytics from './providers/crashlytics';
29-
import * as database from './providers/database';
30-
import * as firestore from './providers/firestore';
31-
import * as pubsub from './providers/pubsub';
32-
import * as storage from './providers/storage';
27+
import { features as lazyFeatures, FeaturesList } from './features';
3328

3429
export = (firebaseConfig?: AppOptions) => {
3530
const test = new FirebaseFunctionsTest();
3631
test.init(firebaseConfig);
37-
return {
38-
cleanup: test.cleanup,
39-
mockConfig,
40-
wrap,
41-
makeChange,
42-
analytics,
43-
crashlytics,
44-
database,
45-
firestore,
46-
pubsub,
47-
storage,
48-
};
32+
// Ensure other files get loaded after init function, since they load `firebase-functions`
33+
// which will issue warning if process.env.FIREBASE_CONFIG is not yet set.
34+
let features = require('./features') as typeof lazyFeatures;
35+
features = merge({}, features, {
36+
cleanup: () => test.cleanup,
37+
});
38+
return features;
4939
};

0 commit comments

Comments
 (0)