@@ -25,169 +25,119 @@ import sourcemaps from 'rollup-plugin-sourcemaps';
25
25
import typescript from 'typescript' ;
26
26
import { terser } from 'rollup-plugin-terser' ;
27
27
28
- import { renameInternals } from './scripts/rename-internals' ;
29
- import { extractPublicIdentifiers } from './scripts/extract-api' ;
30
28
import pkg from './package.json' ;
31
29
import memoryPkg from './memory/package.json' ;
32
- import { externs } from './externs.json' ;
33
30
34
- const deps = Object . keys (
31
+ import {
32
+ appendPrivatePrefixTransformers ,
33
+ manglePrivatePropertiesOptions ,
34
+ resolveMemoryExterns
35
+ } from './terser.config' ;
36
+
37
+ // This Firestore Rollup configuration provides a number of different builds:
38
+ // - Browser builds that support persistence in ES5 CJS and ES5 ESM formats and
39
+ // ES2017 in ESM format.
40
+ // - In-memory Browser builds that support persistence in ES5 CJS and ES5 ESM
41
+ // formats and ES2017 in ESM format.
42
+ // - A NodeJS build that supports persistence (to be used with an IndexedDb
43
+ // shim)
44
+ // - A in-memory only NodeJS build
45
+ //
46
+ // The in-memory builds are roughly 130 KB smaller, but throw an exception
47
+ // for calls to `enablePersistence()` or `clearPersistence()`.
48
+ //
49
+ // All browser builds rely on Terser's property name mangling to reduce code
50
+ // size.
51
+
52
+ // MARK: Browser builds
53
+
54
+ const browserDeps = Object . keys (
35
55
Object . assign ( { } , pkg . peerDependencies , pkg . dependencies )
36
56
) ;
37
57
38
- // Extract all identifiers used in external APIs (to be used as a blacklist by
39
- // the SDK minifier).
40
- const externsPaths = externs . map ( p => path . resolve ( __dirname , '../../' , p ) ) ;
41
- const publicIdentifiers = extractPublicIdentifiers ( externsPaths ) ;
42
- const transformers = [
43
- service => ( {
44
- before : [
45
- renameInternals ( service . getProgram ( ) , {
46
- publicIdentifiers,
47
- prefix : '__PRIVATE_'
48
- } )
49
- ] ,
50
- after : [ ]
51
- } )
52
- ] ;
53
-
54
- const terserOptions = {
55
- output : {
56
- comments : 'all' ,
57
- beautify : true
58
- } ,
59
- mangle : {
60
- properties : {
61
- regex : / ^ _ _ P R I V A T E _ /
62
- }
63
- }
64
- } ;
58
+ function resolveBrowserExterns ( id ) {
59
+ return browserDeps . some ( dep => id === dep || id . startsWith ( `${ dep } /` ) ) ;
60
+ }
65
61
66
- /**
67
- * ES5 Builds
68
- */
69
62
const es5BuildPlugins = [
70
63
typescriptPlugin ( {
71
64
typescript,
72
- transformers,
73
- cacheRoot : ' ./.cache/es5.min/'
65
+ transformers : appendPrivatePrefixTransformers ,
66
+ cacheRoot : ` ./.cache/es5.mangled/`
74
67
} ) ,
75
68
json ( ) ,
76
- terser ( terserOptions )
69
+ terser ( manglePrivatePropertiesOptions )
77
70
] ;
78
71
79
- const nodeBuildPlugins = [
80
- ...es5BuildPlugins ,
81
- // Needed as we also use the *.proto files
82
- copy ( {
83
- assets : [ './src/protos' ]
72
+ const es2017BuildPlugins = [
73
+ typescriptPlugin ( {
74
+ typescript,
75
+ tsconfigOverride : {
76
+ compilerOptions : {
77
+ target : 'es2017'
78
+ }
79
+ } ,
80
+ cacheRoot : './.cache/es2017.mangled/' ,
81
+ transformers : appendPrivatePrefixTransformers
84
82
} ) ,
85
- replace ( {
86
- 'process.env.FIRESTORE_PROTO_ROOT' : JSON . stringify ( 'src/protos' )
87
- } )
83
+ json ( { preferConst : true } ) ,
84
+ terser ( manglePrivatePropertiesOptions )
88
85
] ;
89
86
90
- /**
91
- * Returns the externs locations for the Memory-based Firestore implementation.
92
- * Verifies that no persistence sources are used by Firestore's memory-only
93
- * implementation.
94
- */
95
- function resolveMemoryExterns ( id , referencedBy ) {
96
- const externalRef = path
97
- . resolve ( path . dirname ( referencedBy ) , id )
98
- . replace ( '.ts' , '' ) ;
99
-
100
- const persistenceRef = [
101
- 'local/indexeddb_persistence.ts' ,
102
- 'local/indexeddb_index_manager.ts' ,
103
- 'local/indexeddb_mutation_queue.ts' ,
104
- 'local/indexeddb_remote_document_cache.ts' ,
105
- 'local/indexeddb_schema.ts' ,
106
- 'local/indexeddb_target_cache.ts' ,
107
- 'local/local_serializer.ts' ,
108
- 'local/simple_db.ts' ,
109
- 'api/persistence.ts'
110
- ] . map ( p => path . resolve ( __dirname , 'src' , p ) ) ;
111
-
112
- if ( persistenceRef . indexOf ( externalRef ) !== - 1 ) {
113
- throw new Error ( 'Unexpected reference in Memory-only client on ' + id ) ;
114
- }
115
- return [ ...deps , 'util' , 'path' ] . some (
116
- dep => id === dep || id . startsWith ( `${ dep } /` )
117
- ) ;
118
- }
119
-
120
- const es5Builds = [
121
- /**
122
- * Node.js Build
123
- */
87
+ const browserBuilds = [
88
+ // ES5 ESM Build (with persistence)
124
89
{
125
- input : 'index.node.ts' ,
126
- output : [ { file : pkg . main , format : 'cjs' , sourcemap : true } ] ,
127
- plugins : nodeBuildPlugins ,
128
- external : id =>
129
- [ ...deps , 'util' , 'path' ] . some (
130
- dep => id === dep || id . startsWith ( `${ dep } /` )
131
- )
90
+ input : 'index.ts' ,
91
+ output : { file : pkg . module , format : 'es' , sourcemap : true } ,
92
+ plugins : es5BuildPlugins ,
93
+ external : resolveBrowserExterns
132
94
} ,
95
+ // ES5 ESM Build (memory-only)
133
96
{
134
- input : 'index.node.memory.ts' ,
135
- output : [
136
- {
137
- file : path . resolve ( './memory' , memoryPkg . main ) ,
138
- format : 'cjs' ,
139
- sourcemap : true
140
- }
141
- ] ,
142
- plugins : nodeBuildPlugins ,
143
- external : resolveMemoryExterns
97
+ input : 'index.memory.ts' ,
98
+ output : {
99
+ file : path . resolve ( './memory' , memoryPkg . module ) ,
100
+ format : 'es' ,
101
+ sourcemap : true
102
+ } ,
103
+ plugins : es5BuildPlugins ,
104
+ external : ( id , referencedBy ) => resolveMemoryExterns ( browserDeps , id , referencedBy )
144
105
} ,
145
- /**
146
- * Browser ESM Build
147
- */
106
+ // ES2017 ESM build (with persistence)
148
107
{
149
108
input : 'index.ts' ,
150
- output : { file : pkg . module , format : 'es' , sourcemap : true } ,
151
- plugins : [
152
- typescriptPlugin ( {
153
- typescript,
154
- transformers,
155
- cacheRoot : './.cache/esm/'
156
- } ) ,
157
- json ( ) ,
158
- terser ( terserOptions )
159
- ] ,
160
- external : id => deps . some ( dep => id === dep || id . startsWith ( `${ dep } /` ) )
109
+ output : {
110
+ file : pkg . esm2017 ,
111
+ format : 'es' ,
112
+ sourcemap : true
113
+ } ,
114
+ plugins : es5BuildPlugins ,
115
+ external : resolveBrowserExterns
161
116
} ,
117
+ // ES2017 ESM build (memory-only)
162
118
{
163
119
input : 'index.memory.ts' ,
164
120
output : {
165
- file : path . resolve ( './memory' , memoryPkg . module ) ,
121
+ file : path . resolve ( './memory' , memoryPkg . esm2017 ) ,
166
122
format : 'es' ,
167
123
sourcemap : true
168
124
} ,
169
- plugins : [
170
- typescriptPlugin ( {
171
- typescript,
172
- transformers,
173
- cacheRoot : './.cache/esm/'
174
- } ) ,
175
- json ( ) ,
176
- terser ( terserOptions )
177
- ] ,
178
- external : resolveMemoryExterns
125
+ plugins : es2017BuildPlugins ,
126
+ external : ( id , referencedBy ) => resolveMemoryExterns ( browserDeps , id , referencedBy )
179
127
} ,
180
- /**
181
- * Browser CJS Build
182
- *
183
- * These builds are based on the mangling in the ESM build above, since
184
- * Terser's Property name mangling doesn't work well with CJS's syntax.
185
- */
128
+ // ES5 CJS Build (with persistence)
129
+ //
130
+ // This build is based on the mangling in the ESM build above, since
131
+ // Terser's Property name mangling doesn't work well with CJS's syntax.
186
132
{
187
133
input : pkg . module ,
188
134
output : { file : pkg . browser , format : 'cjs' , sourcemap : true } ,
189
135
plugins : [ sourcemaps ( ) ]
190
136
} ,
137
+ // ES5 CJS Build (memory-only)
138
+ //
139
+ // This build is based on the mangling in the ESM build above, since
140
+ // Terser's Property name mangling doesn't work well with CJS's syntax.
191
141
{
192
142
input : path . resolve ( './memory' , memoryPkg . module ) ,
193
143
output : {
@@ -199,48 +149,47 @@ const es5Builds = [
199
149
}
200
150
] ;
201
151
202
- /**
203
- * ES2017 Builds
204
- */
205
- const es2017BuildPlugins = [
206
- typescriptPlugin ( {
207
- typescript ,
208
- tsconfigOverride : {
209
- compilerOptions : {
210
- target : 'es2017'
211
- }
212
- } ,
213
- cacheRoot : './.cache/esm2017/' ,
214
- transformers
152
+ // MARK: Node builds
153
+
154
+ const nodeDeps = [ ... browserDeps , 'util' , 'path' ] ;
155
+
156
+ function resolveNodeExterns ( id ) {
157
+ return nodeDeps . some ( dep => id === dep || id . startsWith ( ` ${ dep } /` ) ) ;
158
+ }
159
+
160
+ const nodeBuildPlugins = [
161
+ ... es5BuildPlugins ,
162
+ // Needed as we also use the *.proto files
163
+ copy ( {
164
+ assets : [ './src/protos' ]
215
165
} ) ,
216
- json ( { preferConst : true } ) ,
217
- terser ( terserOptions )
166
+ replace ( {
167
+ 'process.env.FIRESTORE_PROTO_ROOT' : JSON . stringify ( 'src/protos' )
168
+ } )
218
169
] ;
219
170
220
- const es2017Builds = [
221
- /**
222
- * Browser Build
223
- */
171
+ const nodeBuilds = [
172
+ // ES5 CJS build (with persistence)
224
173
{
225
- input : 'index.ts' ,
226
- output : {
227
- file : pkg . esm2017 ,
228
- format : 'es' ,
229
- sourcemap : true
230
- } ,
231
- plugins : es2017BuildPlugins ,
232
- external : id => deps . some ( dep => id === dep || id . startsWith ( `${ dep } /` ) )
174
+ input : 'index.node.ts' ,
175
+ output : [ { file : pkg . main , format : 'cjs' , sourcemap : true } ] ,
176
+ plugins : nodeBuildPlugins ,
177
+ external : resolveNodeExterns
233
178
} ,
179
+ // ES5 CJS build (memory-only)
234
180
{
235
- input : 'index.memory.ts' ,
236
- output : {
237
- file : path . resolve ( './memory' , memoryPkg . esm2017 ) ,
238
- format : 'es' ,
239
- sourcemap : true
240
- } ,
241
- plugins : es2017BuildPlugins ,
242
- external : resolveMemoryExterns
181
+ input : 'index.node.memory.ts' ,
182
+ output : [
183
+ {
184
+ file : path . resolve ( './memory' , memoryPkg . main ) ,
185
+ format : 'cjs' ,
186
+ sourcemap : true
187
+ }
188
+ ] ,
189
+ plugins : nodeBuildPlugins ,
190
+ external : ( id , referencedBy ) =>
191
+ resolveMemoryExterns ( nodeDeps , id , referencedBy )
243
192
}
244
193
] ;
245
194
246
- export default [ ...es5Builds , ...es2017Builds ] ;
195
+ export default [ ...browserBuilds , ...nodeBuilds ] ;
0 commit comments