Skip to content

Commit

Permalink
feat(core): Register AngularFire and Angular versions with the JS SDK
Browse files Browse the repository at this point in the history
If the JS SDK has the `registerVersion` API, we should register the version of Angular and AngularFire.
  • Loading branch information
jamesdaniels authored Jan 22, 2020
1 parent 25e5b04 commit d454cf0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/core/firebase.app.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { InjectionToken, NgModule, Optional, NgZone } from '@angular/core';
import { InjectionToken, NgModule, Optional, NgZone, VERSION as NG_VERSION, Version } from '@angular/core';
import { auth, database, messaging, storage, firestore, functions } from 'firebase/app';
// @ts-ignore (https://github.com/firebase/firebase-js-sdk/pull/1206)
import firebase from 'firebase/app'; // once fixed can pull in as "default as firebase" above
Expand Down Expand Up @@ -42,8 +42,11 @@ export class FirebaseApp {
firestore: () => FirebaseFirestore;
functions: (region?: string) => FirebaseFunctions;
remoteConfig: () => FirebaseRemoteConfig;
registerVersion?: (library: string, version: string) => void;
}

export const VERSION = new Version('ANGULARFIRE2_VERSION');

export function _firebaseAppFactory(options: FirebaseOptions, zone: NgZone, nameOrConfig?: string|FirebaseAppConfig|null) {
const name = typeof nameOrConfig === 'string' && nameOrConfig || '[DEFAULT]';
const config = typeof nameOrConfig === 'object' && nameOrConfig || {};
Expand All @@ -52,7 +55,12 @@ export function _firebaseAppFactory(options: FirebaseOptions, zone: NgZone, name
const existingApp = firebase.apps.filter(app => app && app.name === config.name)[0] as any;
// We support FirebaseConfig, initializeApp's public type only accepts string; need to cast as any
// Could be solved with https://github.com/firebase/firebase-js-sdk/pull/1206
return (existingApp || zone.runOutsideAngular(() => firebase.initializeApp(options, config as any))) as FirebaseApp;
const app = (existingApp || zone.runOutsideAngular(() => firebase.initializeApp(options, config as any))) as FirebaseApp;
if (app.registerVersion) {
app.registerVersion('angularfire', VERSION.full);
app.registerVersion('angular', NG_VERSION.full);
}
return app;
}

const FirebaseAppProvider = {
Expand Down
7 changes: 7 additions & 0 deletions tools/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,12 @@ function replaceDynamicImportsForUMD() {
writeFileSync('./dist/packages-dist/remote-config/remote-config.d.ts', readFileSync('./dist/packages-dist/remote-config/remote-config.d.ts', 'utf8').replace("implements remoteConfig.Value", "implements Partial<remoteConfig.Value>"));
}

function writeCoreVersion() {
writeFileSync('./dist/packages-dist/bundles/core.umd.js', readFileSync('./dist/packages-dist/bundles/core.umd.js', 'utf8').replace('ANGULARFIRE2_VERSION', VERSIONS.ANGULARFIRE2_VERSION));
writeFileSync('./dist/packages-dist/firebase.app.module.js', readFileSync('./dist/packages-dist/firebase.app.module.js', 'utf8').replace('ANGULARFIRE2_VERSION', VERSIONS.ANGULARFIRE2_VERSION));
writeFileSync('./dist/packages-dist/es2015/firebase.app.module.js', readFileSync('./dist/packages-dist/es2015/firebase.app.module.js', 'utf8').replace('ANGULARFIRE2_VERSION', VERSIONS.ANGULARFIRE2_VERSION));
}

function measure(module) {
const path = `${process.cwd()}/dist/packages-dist/bundles/${module}.umd.js`;
const file = readFileSync(path);
Expand Down Expand Up @@ -408,6 +414,7 @@ function buildLibrary(globals) {
switchMap(() => from(createTestUmd(globals))),
tap(() => {
replaceDynamicImportsForUMD();
writeCoreVersion();
const coreStats = measure('core');
const analyticsStats = measure('analytics');
const authStats = measure('auth');
Expand Down

0 comments on commit d454cf0

Please sign in to comment.