11import firebase from 'firebase/app'
2+ < %
3+ const serviceMapping = {
4+ auth : 'auth' ,
5+ realtimeDb : 'database' ,
6+ firestore : 'firestore' ,
7+ storage : 'storage' ,
8+ functions : 'functions' ,
9+ messaging : 'messaging' ,
10+ performance : 'performance' ,
11+ analytics : 'analytics' ,
12+ remoteConfig : 'remote-config'
13+ }
14+
15+ function writeStaticImports ( ) {
16+ return Object . keys ( serviceMapping )
17+ . map ( service => writeImportStatement ( service , true ) )
18+ . filter ( Boolean )
19+ . join ( '\n' )
20+ }
21+
22+ function writeImportStatement ( service , staticImport = false ) {
23+ const serviceOptions = options . services [ service ]
24+ const importStatically = serviceOptions && serviceOptions . static
25+ const importDynamically = serviceOptions && ! importStatically
26+
27+ if ( importStatically && staticImport ) {
28+ return `import 'firebase/${ serviceMapping [ service ] } '`
29+ }
30+
31+ if ( ! importDynamically || staticImport ) {
32+ return
33+ }
34+
35+ const webpackComments = [ ]
36+
37+ // Add Chunk Name Comment
38+ if ( process . env . NODE_ENV !== 'production' && ! serviceOptions . chunkName ) {
39+ webpackComments . push ( `webpackChunkName: 'firebase-${ serviceMapping [ service ] } '` )
40+ }
41+ if ( serviceOptions . chunkName ) {
42+ webpackComments . push ( `webpackChunkName: '${ serviceOptions . chunkName } '` )
43+ }
44+
45+ // Add Preload Comment
46+ if ( serviceOptions . preload ) {
47+ webpackComments . push ( `webpackPreload: true` )
48+ }
49+
50+ // Add strings surrounding the comment
51+ let webpackCommentsString = ''
52+ if ( webpackComments . length ) {
53+ webpackCommentsString = `/* ${ webpackComments . join ( ', ' ) } */`
54+ }
55+ return `await import(${ webpackCommentsString } 'firebase/${ serviceMapping [ service ] } ')`
56+ }
57+ % >
58+ < %= writeStaticImports ( ) % >
59+
60+ /** --------------------------------------------------------------------------------------------- **/
61+ /** -------------------------------------- END: Import Scripts ---------------------------------- **/
62+ /** --------------------------------------------------------------------------------------------- **/
63+
264
365export default async ( ctx , inject ) => {
466
@@ -10,26 +72,38 @@ export default async (ctx, inject) => {
1072 firebase . initializeApp ( firebaseConfig )
1173 }
1274
13- if ( options . services . auth ) {
14- await import ( 'firebase/auth' )
75+ /** --------------------------------------------------------------------------------------------- **/
76+ /** -------------------------------------- FIREBASE AUTH ---------------------------------------- **/
77+ /** --------------------------------------------------------------------------------------------- **/
78+
79+ < % if ( options . services . auth ) { % >
80+ < %= writeImportStatement ( 'auth' ) % >
1581
1682 const fireAuth = firebase . auth ( )
1783 const fireAuthObj = firebase . auth
1884 inject ( 'fireAuth' , fireAuth )
1985 inject ( 'fireAuthObj' , fireAuthObj )
20- }
86+ < % } % >
2187
22- if ( options . services . realtimeDb ) {
23- await import ( 'firebase/database' )
88+ /** --------------------------------------------------------------------------------------------- **/
89+ /** -------------------------------------- FIREBASE REALTIME DB --------------------------------- **/
90+ /** --------------------------------------------------------------------------------------------- **/
91+ < % if ( options . services . realtimeDb ) { % >
92+ < %= writeImportStatement ( 'realtimeDb' ) % >
2493
2594 const fireDb = firebase . database ( )
2695 const fireDbObj = firebase . database
2796 inject ( 'fireDb' , fireDb )
2897 inject ( 'fireDbObj' , fireDbObj )
29- }
3098
31- if ( options . services . firestore ) {
32- await import ( 'firebase/firestore' )
99+ < % } % >
100+
101+ /** --------------------------------------------------------------------------------------------- **/
102+ /** ---------------------------------------- FIREBASE FIRESTORE --------------------------------- **/
103+ /** --------------------------------------------------------------------------------------------- **/
104+
105+ < % if ( options . services . firestore ) { % >
106+ < %= writeImportStatement ( 'firestore' ) % >
33107
34108 const fireStore = firebase . firestore ( )
35109 const fireStoreObj = firebase . firestore
@@ -47,30 +121,46 @@ export default async (ctx, inject) => {
47121 }
48122 }
49123 }
50- }
51124
52- if ( options . services . storage ) {
53- await import ( 'firebase/storage' )
125+ < % } % >
126+
127+ /** --------------------------------------------------------------------------------------------- **/
128+ /** ------------------------------------------ FIREBASE STORAGE --------------------------------- **/
129+ /** --------------------------------------------------------------------------------------------- **/
130+
131+ < % if ( options . services . storage ) { % >
132+ < %= writeImportStatement ( 'storage' ) % >
54133
55134 const fireStorage = firebase . storage ( )
56135 const fireStorageObj = firebase . storage
57136 inject ( 'fireStorage' , fireStorage )
58137 inject ( 'fireStorageObj' , fireStorageObj )
59- }
60138
61- if ( options . services . functions ) {
62- await import ( 'firebase/functions' )
139+ < % } % >
140+
141+ /** --------------------------------------------------------------------------------------------- **/
142+ /** ---------------------------------------- FIREBASE FUNCTIONS --------------------------------- **/
143+ /** --------------------------------------------------------------------------------------------- **/
144+
145+ < % if ( options . services . functions ) { % >
146+ < %= writeImportStatement ( 'functions' ) % >
63147
64148 // If .location is undefined, default will be "us-central1"
65149 const fireFunc = firebase . app ( ) . functions ( options . services . functions . location )
66150 const fireFuncObj = firebase . functions
67151 inject ( 'fireFunc' , fireFunc )
68152 inject ( 'fireFuncObj' , fireFuncObj )
69- }
70153
154+ < % } % >
155+
156+ /** --------------------------------------------------------------------------------------------- **/
157+ /** ---------------------------------------- FIREBASE MESSAGING --------------------------------- **/
158+ /** --------------------------------------------------------------------------------------------- **/
159+
160+ < % if ( options . services . messaging ) { % >
71161 // Firebase Messaging can only be initiated on client side
72- if ( process . browser && options . services . messaging ) {
73- await import ( 'firebase/ messaging')
162+ if ( process . browser ) {
163+ < %= writeImportStatement ( ' messaging') % >
74164
75165 if ( firebase . messaging . isSupported ( ) ) {
76166 const fireMess = firebase . messaging ( )
@@ -85,46 +175,65 @@ export default async (ctx, inject) => {
85175 }
86176 }
87177
178+ < % } % >
179+
180+ /** --------------------------------------------------------------------------------------------- **/
181+ /** -------------------------------------- FIREBASE REALTIME DB --------------------------------- **/
182+ /** --------------------------------------------------------------------------------------------- **/
183+
88184 // Firebase Performance can only be initiated on client side
89- if ( process . browser && options . services . performance ) {
90- await import ( 'firebase/performance' )
185+ < % if ( options . services . performance ) { % >
186+ if ( process . browser ) {
187+ < %= writeImportStatement ( 'performance' ) % >
91188
92189 const firePerf = firebase . performance ( )
93190 const firePerfObj = firebase . performance
94191 inject ( 'firePerf' , firePerf )
95192 inject ( 'firePerfObj' , firePerfObj )
96193 }
194+ < % } % >
195+
196+ /** --------------------------------------------------------------------------------------------- **/
197+ /** ---------------------------------------- FIREBASE ANALYTICS --------------------------------- **/
198+ /** --------------------------------------------------------------------------------------------- **/
97199
98200 // Firebase Analytics can only be initiated on the client side
99- if ( process . browser && options . services . analytics ) {
100- await import ( 'firebase/analytics' )
201+ < % if ( options . services . analytics ) { % >
202+ if (process.browser) {
203+ < %= writeImportStatement ( 'analytics' ) % >
101204
102205 const fireAnalytics = firebase . analytics ( )
103206 const fireAnalyticsObj = firebase . analytics
104207 inject ( 'fireAnalytics' , fireAnalytics )
105208 inject ( 'fireAnalyticsObj' , fireAnalyticsObj )
209+
106210 }
211+ < % } % >
107212
213+ /** --------------------------------------------------------------------------------------------- **/
214+ /** --------------------------------- FIREBASE REMOTE CONFIG DB --------------------------------- **/
215+ /** --------------------------------------------------------------------------------------------- **/
216+ < % if ( options . services . remoteConfig ) { % >
108217 // Firebase Remote Config can only be initiated on the client side
109- if ( process . browser && options . services . remoteConfig ) {
110- await import ( 'firebase/remote-config' )
218+ if (process.browser) {
219+ < %= writeImportStatement ( 'remoteConfig' ) % >
111220
112221 const fireConfig = firebase . remoteConfig ( )
113222 const fireConfigObj = firebase . remoteConfig
114223
115- if ( options . services . remoteConfig ) {
116- const { settings : remoteSettings , defaultConfig : remoteDefaultConfig } = options . services . remoteConfig
117- if ( remoteSettings ) {
118- const { minimumFetchIntervalMillis, fetchTimeoutMillis } = remoteSettings
119- fireConfig . settings = {
120- fetchTimeoutMillis : fetchTimeoutMillis ? fetchTimeoutMillis : 60000 ,
121- minimumFetchIntervalMillis : minimumFetchIntervalMillis ? minimumFetchIntervalMillis : 43200000
122- }
224+ const { settings : remoteSettings , defaultConfig : remoteDefaultConfig } = options.services.remoteConfig
225+ if (remoteSettings) {
226+ const { minimumFetchIntervalMillis , fetchTimeoutMillis } = remoteSettings
227+ fireConfig.settings = {
228+ fetchTimeoutMillis : fetchTimeoutMillis ? fetchTimeoutMillis : 60000 ,
229+ minimumFetchIntervalMillis : minimumFetchIntervalMillis ? minimumFetchIntervalMillis : 43200000
123230 }
124- fireConfig . defaultConfig = ( remoteDefaultConfig )
125231 }
232+ fireConfig . defaultConfig = ( remoteDefaultConfig )
126233
127234 inject ( 'fireConfig ', fireConfig )
128235 inject ( 'fireConfigObj ', fireConfigObj )
236+
129237 }
238+ < % } % >
130239}
0 commit comments