Skip to content

Commit ce47ef8

Browse files
Restructure Rollup config
1 parent 2ac9dfc commit ce47ef8

File tree

2 files changed

+197
-163
lines changed

2 files changed

+197
-163
lines changed

packages/firestore/rollup.config.js

Lines changed: 112 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -25,169 +25,119 @@ import sourcemaps from 'rollup-plugin-sourcemaps';
2525
import typescript from 'typescript';
2626
import { terser } from 'rollup-plugin-terser';
2727

28-
import { renameInternals } from './scripts/rename-internals';
29-
import { extractPublicIdentifiers } from './scripts/extract-api';
3028
import pkg from './package.json';
3129
import memoryPkg from './memory/package.json';
32-
import { externs } from './externs.json';
3330

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(
3555
Object.assign({}, pkg.peerDependencies, pkg.dependencies)
3656
);
3757

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: /^__PRIVATE_/
62-
}
63-
}
64-
};
58+
function resolveBrowserExterns(id) {
59+
return browserDeps.some(dep => id === dep || id.startsWith(`${dep}/`));
60+
}
6561

66-
/**
67-
* ES5 Builds
68-
*/
6962
const es5BuildPlugins = [
7063
typescriptPlugin({
7164
typescript,
72-
transformers,
73-
cacheRoot: './.cache/es5.min/'
65+
transformers: appendPrivatePrefixTransformers,
66+
cacheRoot: `./.cache/es5.mangled/`
7467
}),
7568
json(),
76-
terser(terserOptions)
69+
terser(manglePrivatePropertiesOptions)
7770
];
7871

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
8482
}),
85-
replace({
86-
'process.env.FIRESTORE_PROTO_ROOT': JSON.stringify('src/protos')
87-
})
83+
json({ preferConst: true }),
84+
terser(manglePrivatePropertiesOptions)
8885
];
8986

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)
12489
{
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
13294
},
95+
// ES5 ESM Build (memory-only)
13396
{
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)
144105
},
145-
/**
146-
* Browser ESM Build
147-
*/
106+
// ES2017 ESM build (with persistence)
148107
{
149108
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
161116
},
117+
// ES2017 ESM build (memory-only)
162118
{
163119
input: 'index.memory.ts',
164120
output: {
165-
file: path.resolve('./memory', memoryPkg.module),
121+
file: path.resolve('./memory', memoryPkg.esm2017),
166122
format: 'es',
167123
sourcemap: true
168124
},
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)
179127
},
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.
186132
{
187133
input: pkg.module,
188134
output: { file: pkg.browser, format: 'cjs', sourcemap: true },
189135
plugins: [sourcemaps()]
190136
},
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.
191141
{
192142
input: path.resolve('./memory', memoryPkg.module),
193143
output: {
@@ -199,48 +149,47 @@ const es5Builds = [
199149
}
200150
];
201151

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']
215165
}),
216-
json({ preferConst: true }),
217-
terser(terserOptions)
166+
replace({
167+
'process.env.FIRESTORE_PROTO_ROOT': JSON.stringify('src/protos')
168+
})
218169
];
219170

220-
const es2017Builds = [
221-
/**
222-
* Browser Build
223-
*/
171+
const nodeBuilds = [
172+
// ES5 CJS build (with persistence)
224173
{
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
233178
},
179+
// ES5 CJS build (memory-only)
234180
{
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)
243192
}
244193
];
245194

246-
export default [...es5Builds, ...es2017Builds];
195+
export default [...browserBuilds, ...nodeBuilds];

0 commit comments

Comments
 (0)