/
rollup.config.js
238 lines (229 loc) · 6.65 KB
/
rollup.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { version as grpcVersion } from '@grpc/grpc-js/package.json';
import alias from '@rollup/plugin-alias';
import json from '@rollup/plugin-json';
import copy from 'rollup-plugin-copy';
import replace from 'rollup-plugin-replace';
import { terser } from 'rollup-plugin-terser';
import typescriptPlugin from 'rollup-plugin-typescript2';
import tmp from 'tmp';
import { basename } from 'path';
import typescript from 'typescript';
import { generateBuildTargetReplaceConfig } from '../../scripts/build/rollup_replace_build_target';
import pkg from './package.json';
const util = require('./rollup.shared');
// Customize how import.meta.url is polyfilled in cjs nodejs build. We use it to
// be able to use require() in esm. It only generates the nodejs version of the
// polyfill, as opposed to the default polyfill which supports both browser and
// nodejs. The browser support doesn't work well with Jest.
// See https://github.com/firebase/firebase-js-sdk/issues/5687
// Although this is a cjs Node build and shouldn't require the browser option,
// Vercel apps using this break on deployment, but work in local development.
// See https://github.com/firebase/firebase-js-sdk/issues/5823
function importMetaUrlPolyfillPlugin(filename) {
return {
name: 'import-meta-url-current-module',
resolveImportMeta(property, { moduleId }) {
if (property === 'url') {
// Added a check for Jest (see issue 5687 linked above)
// See https://jestjs.io/docs/environment-variables - apparently
// these are not always both set.
const JEST_CHECK =
`typeof process !== 'undefined' && process.env !== undefined` +
` && (process.env.JEST_WORKER_ID !== undefined || ` +
`process.env.NODE_ENV === 'test')`;
// Copied from rollup output
return (
`((typeof document === 'undefined' || (${JEST_CHECK})) ?` +
` new (require('url').URL)` +
`('file:' + __filename).href : (document.currentScript && ` +
`document.currentScript.src || new URL('${filename}', ` +
`document.baseURI).href))`
);
}
return null;
}
};
}
const nodePlugins = function () {
return [
typescriptPlugin({
typescript,
tsconfigOverride: {
compilerOptions: {
target: 'es2017'
}
},
cacheDir: tmp.dirSync(),
abortOnError: false,
transformers: [util.removeAssertTransformer]
}),
json({ preferConst: true }),
// Needed as we also use the *.proto files
copy({
targets: [
{
src: 'src/protos',
dest: 'dist/src'
}
]
}),
replace({
'process.env.FIRESTORE_PROTO_ROOT': JSON.stringify('src/protos'),
'__GRPC_VERSION__': grpcVersion
})
];
};
const browserPlugins = function () {
return [
typescriptPlugin({
typescript,
tsconfigOverride: {
compilerOptions: {
target: 'es2017'
}
},
cacheDir: tmp.dirSync(),
abortOnError: false,
transformers: [util.removeAssertAndPrefixInternalTransformer]
}),
json({ preferConst: true }),
terser(util.manglePrivatePropertiesOptions)
];
};
const allBuilds = [
// Intermidiate Node ESM build without build target reporting
// this is an intermidiate build used to generate the actual esm and cjs builds
// which add build target reporting
{
input: './src/index.node.ts',
output: {
file: pkg['main-esm'],
format: 'es',
sourcemap: true
},
plugins: [alias(util.generateAliasConfig('node')), ...nodePlugins()],
external: util.resolveNodeExterns,
treeshake: {
moduleSideEffects: false
},
onwarn: util.onwarn
},
// Node CJS build
{
input: pkg['main-esm'],
output: {
file: pkg.main,
format: 'cjs',
sourcemap: true
},
plugins: [
...util.es2017ToEs5Plugins(/* mangled= */ false),
replace(generateBuildTargetReplaceConfig('cjs', 2017)),
importMetaUrlPolyfillPlugin(basename(pkg.main))
],
external: util.resolveNodeExterns,
treeshake: {
moduleSideEffects: false
}
},
// Node ESM build with build target reporting
{
input: pkg['main-esm'],
output: {
file: pkg['main-esm'],
format: 'es',
sourcemap: true
},
plugins: [replace(generateBuildTargetReplaceConfig('esm', 2017))],
external: util.resolveNodeExterns,
treeshake: {
moduleSideEffects: false
}
},
// Intermidiate browser build without build target reporting
// this is an intermidiate build used to generate the actual esm and cjs builds
// which add build target reporting
{
input: './src/index.ts',
output: {
file: pkg.browser,
format: 'es',
sourcemap: true
},
plugins: [alias(util.generateAliasConfig('browser')), ...browserPlugins()],
external: util.resolveBrowserExterns,
treeshake: {
moduleSideEffects: false
}
},
// Convert es2017 build to ES5
{
input: pkg['browser'],
output: [
{
file: pkg['esm5'],
format: 'es',
sourcemap: true
}
],
plugins: [
...util.es2017ToEs5Plugins(/* mangled= */ true),
replace(generateBuildTargetReplaceConfig('esm', 5))
],
external: util.resolveBrowserExterns,
treeshake: {
moduleSideEffects: false
}
},
// es2017 build with build target reporting
{
input: pkg['browser'],
output: [
{
file: pkg['browser'],
format: 'es',
sourcemap: true
}
],
plugins: [replace(generateBuildTargetReplaceConfig('esm', 2017))],
external: util.resolveBrowserExterns,
treeshake: {
moduleSideEffects: false
}
},
// RN build
{
input: './src/index.rn.ts',
output: {
file: pkg['react-native'],
format: 'es',
sourcemap: true
},
plugins: [
alias(util.generateAliasConfig('rn')),
...browserPlugins(),
replace(generateBuildTargetReplaceConfig('esm', 2017))
],
external: util.resolveBrowserExterns,
treeshake: {
moduleSideEffects: false
}
}
];
export default allBuilds;