Skip to content

Commit 8037b6b

Browse files
Add minified Firestore build
1 parent af162f9 commit 8037b6b

File tree

8 files changed

+326
-34
lines changed

8 files changed

+326
-34
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import '../../firestore/dist/index.esm.min';

packages/firebase/rollup.config.js

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,38 @@ import pkg from './package.json';
2828

2929
import appPkg from './app/package.json';
3030

31+
function createUmdOutputConfig(output) {
32+
return {
33+
file: output,
34+
format: 'umd',
35+
sourcemap: true,
36+
extend: true,
37+
name: GLOBAL_NAME,
38+
globals: {
39+
'@firebase/app': GLOBAL_NAME
40+
},
41+
42+
/**
43+
* use iife to avoid below error in the old Safari browser
44+
* SyntaxError: Functions cannot be declared in a nested block in strict mode
45+
* https://github.com/firebase/firebase-js-sdk/issues/1228
46+
*
47+
*/
48+
intro: `
49+
try {
50+
(function() {`,
51+
outro: `
52+
}).apply(this, arguments);
53+
} catch(err) {
54+
console.error(err);
55+
throw new Error(
56+
'Cannot instantiate ${output} - ' +
57+
'be sure to load firebase-app.js first.'
58+
);
59+
}`
60+
};
61+
}
62+
3163
const plugins = [
3264
sourcemaps(),
3365
resolveModule(),
@@ -101,43 +133,21 @@ const componentBuilds = pkg.components
101133
},
102134
{
103135
input: `${component}/index.ts`,
104-
output: {
105-
file: `firebase-${component}.js`,
106-
format: 'umd',
107-
sourcemap: true,
108-
extend: true,
109-
name: GLOBAL_NAME,
110-
globals: {
111-
'@firebase/app': GLOBAL_NAME
112-
},
113-
114-
/**
115-
* use iife to avoid below error in the old Safari browser
116-
* SyntaxError: Functions cannot be declared in a nested block in strict mode
117-
* https://github.com/firebase/firebase-js-sdk/issues/1228
118-
*
119-
*/
120-
121-
intro: `
122-
try {
123-
(function() {`,
124-
outro: `
125-
}).apply(this, arguments);
126-
} catch(err) {
127-
console.error(err);
128-
throw new Error(
129-
'Cannot instantiate firebase-${component} - ' +
130-
'be sure to load firebase-app.js first.'
131-
);
132-
}`
133-
},
136+
output: createUmdOutputConfig(`firebase-${component}.js`),
134137
plugins: [...plugins, uglify()],
135138
external: ['@firebase/app']
136139
}
137140
];
138141
})
139142
.reduce((a, b) => a.concat(b), []);
140143

144+
const firestoreMinifiedBuild = {
145+
input: `firestore/index.min.ts`,
146+
output: createUmdOutputConfig(`firebase-firestore-min.js`),
147+
plugins: [...plugins, uglify()],
148+
external: ['@firebase/app']
149+
};
150+
141151
/**
142152
* Complete Package Builds
143153
*/
@@ -239,4 +249,9 @@ const completeBuilds = [
239249
}
240250
];
241251

242-
export default [...appBuilds, ...componentBuilds, ...completeBuilds];
252+
export default [
253+
...appBuilds,
254+
...componentBuilds,
255+
firestoreMinifiedBuild,
256+
...completeBuilds
257+
];

packages/firestore/.npmignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
/src
33
test
44
.idea
5+
.cache
56

67
# Files not needed by end users
78
gulpfile.js
89
index.ts
910
karma.conf.js
10-
tsconfig.json
11+
tsconfig.json

packages/firestore/externs.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"externs" : [
3+
"node_modules/typescript/lib/lib.es5.d.ts",
4+
"node_modules/typescript/lib/lib.dom.d.ts",
5+
"node_modules/typescript/lib/lib.es2015.promise.d.ts",
6+
"node_modules/typescript/lib/lib.es2015.symbol.d.ts",
7+
"node_modules/typescript/lib/lib.es2015.iterable.d.ts",
8+
"node_modules/typescript/lib/lib.es2015.collection.d.ts",
9+
"node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts",
10+
"node_modules/typescript/lib/lib.es2015.core.d.ts",
11+
"node_modules/typescript/lib/lib.es2017.object.d.ts",
12+
"packages/app-types/index.d.ts",
13+
"packages/app-types/private.d.ts",
14+
"packages/auth-interop-types/index.d.ts",
15+
"packages/firestore-types/index.d.ts",
16+
"packages/firebase/index.d.ts",
17+
"packages/component/dist/src/component.d.ts",
18+
"packages/component/dist/src/provider.d.ts",
19+
"packages/component/dist/src/component_container.d.ts",
20+
"packages/logger/dist/src/logger.d.ts",
21+
"packages/webchannel-wrapper/src/index.d.ts",
22+
"packages/util/dist/src/environment.d.ts",
23+
"packages/firestore/src/protos/firestore_proto_api.d.ts",
24+
"packages/firestore/dist/lib/src/local/indexeddb_schema.d.ts",
25+
"packages/firestore/dist/lib/src/local/shared_client_state_schema.d.ts"
26+
]
27+
}

packages/firestore/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "",
55
"author": "Firebase <[email protected]> (https://firebase.google.com/)",
66
"scripts": {
7+
"prebuild": "tsc -d --declarationDir dist/lib --emitDeclarationOnly && tsc -m es2015 --moduleResolution node scripts/*.ts ",
78
"build": "rollup -c",
89
"build:console": "node tools/console.build.js",
910
"dev": "rollup -c -w",
@@ -24,8 +25,11 @@
2425
},
2526
"main": "dist/index.node.cjs.js",
2627
"browser": "dist/index.cjs.js",
28+
"browserMinified": "dist/index.cjs.min.js",
2729
"module": "dist/index.esm.js",
30+
"moduleMinified": "dist/index.esm.min.js",
2831
"esm2017": "dist/index.esm2017.js",
32+
"esm2017Minified": "dist/index.esm2017.min.js",
2933
"license": "Apache-2.0",
3034
"files": [
3135
"dist"
@@ -48,8 +52,10 @@
4852
"protobufjs": "6.8.8",
4953
"rollup": "1.27.9",
5054
"rollup-plugin-copy-assets": "1.1.0",
55+
"rollup-plugin-json": "5.1.3",
5156
"rollup-plugin-node-resolve": "5.2.0",
5257
"rollup-plugin-replace": "2.2.0",
58+
"rollup-plugin-terser": "5.1.2",
5359
"rollup-plugin-typescript2": "0.25.3",
5460
"typescript": "3.7.3"
5561
},

packages/firestore/rollup.config.js

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,73 @@
1515
* limitations under the License.
1616
*/
1717

18+
import * as path from 'path';
19+
1820
import json from 'rollup-plugin-json';
1921
import typescriptPlugin from 'rollup-plugin-typescript2';
2022
import replace from 'rollup-plugin-replace';
2123
import copy from 'rollup-plugin-copy-assets';
2224
import typescript from 'typescript';
25+
import { terser } from 'rollup-plugin-terser';
26+
27+
import { renameInternals } from './scripts/rename-internals';
28+
import { extractPublicIdentifiers } from './scripts/extract-api';
2329
import pkg from './package.json';
30+
import { externs } from './externs.json';
2431

2532
const deps = Object.keys(
2633
Object.assign({}, pkg.peerDependencies, pkg.dependencies)
2734
);
2835

36+
// Extract all identifiers used in external APIs (to be used as a blacklist by
37+
// the SDK minifier).
38+
const externsPaths = externs.map(p => path.resolve(__dirname, '../../', p));
39+
const publicIdentifiers = extractPublicIdentifiers(externsPaths);
40+
const transformers = [
41+
service => ({
42+
before: [
43+
renameInternals(service.getProgram(), {
44+
publicIdentifiers,
45+
prefix: '__PRIVATE_'
46+
})
47+
],
48+
after: []
49+
})
50+
];
51+
52+
const terserOptions = {
53+
output: {
54+
comments: false
55+
},
56+
mangle: {
57+
properties: {
58+
regex: /^__PRIVATE_/
59+
}
60+
}
61+
};
62+
2963
/**
3064
* ES5 Builds
3165
*/
3266
const es5BuildPlugins = [
3367
typescriptPlugin({
34-
typescript
68+
typescript,
69+
cacheRoot: './.cache/es5/'
3570
}),
3671
json()
3772
];
3873

74+
const es5MinifiedBuildPlugins = [
75+
typescriptPlugin({
76+
typescript,
77+
transformers,
78+
cacheRoot: './.cache/es5.min/'
79+
}),
80+
json(),
81+
terser(terserOptions),
82+
replace({ delimiters: ['', ''], '__PRIVATE_': ''})
83+
];
84+
3985
const es5Builds = [
4086
/**
4187
* Node.js Build
@@ -69,6 +115,15 @@ const es5Builds = [
69115
],
70116
plugins: es5BuildPlugins,
71117
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`))
118+
},
119+
{
120+
input: 'index.ts',
121+
output: [
122+
{ file: pkg.browserMinified, format: 'cjs', sourcemap: true },
123+
{ file: pkg.moduleMinified, format: 'es', sourcemap: true }
124+
],
125+
plugins: es5MinifiedBuildPlugins,
126+
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`))
72127
}
73128
];
74129

@@ -82,11 +137,28 @@ const es2017BuildPlugins = [
82137
compilerOptions: {
83138
target: 'es2017'
84139
}
85-
}
140+
},
141+
cacheRoot: './.cache/es2017/'
86142
}),
87143
json({ preferConst: true })
88144
];
89145

146+
const es2017MinifiedBuildPlugins = [
147+
typescriptPlugin({
148+
typescript,
149+
tsconfigOverride: {
150+
compilerOptions: {
151+
target: 'es2017'
152+
}
153+
},
154+
cacheRoot: './.cache/es2017.min/',
155+
transformers
156+
}),
157+
json({ preferConst: true }),
158+
terser(terserOptions),
159+
replace({ delimiters: ['', ''], '__PRIVATE_': ''})
160+
];
161+
90162
const es2017Builds = [
91163
/**
92164
* Browser Build
@@ -100,6 +172,16 @@ const es2017Builds = [
100172
},
101173
plugins: es2017BuildPlugins,
102174
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`))
175+
},
176+
{
177+
input: 'index.ts',
178+
output: {
179+
file: pkg.esm2017Minified,
180+
format: 'es',
181+
sourcemap: true
182+
},
183+
plugins: es2017MinifiedBuildPlugins,
184+
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`))
103185
}
104186
];
105187

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import * as ts from 'typescript';
19+
import * as fs from 'fs';
20+
21+
function extractIdentifiersFromNodeAndChildren(
22+
node: ts.Node,
23+
symbols: Set<string>
24+
): void {
25+
if (ts.isIdentifier(node)) {
26+
symbols.add(node.escapedText.toString());
27+
}
28+
29+
ts.forEachChild(node, (childNode: ts.Node) =>
30+
extractIdentifiersFromNodeAndChildren(childNode, symbols)
31+
);
32+
}
33+
34+
/**
35+
* Traverses TypeScript type definition files and returns the list of referenced
36+
* identifiers.
37+
*/
38+
export function extractPublicIdentifiers(filePaths: string[]): Set<string> {
39+
const publicIdentifiers = new Set<string>();
40+
41+
for (const filePath of filePaths) {
42+
const contents = fs.readFileSync(filePath, { encoding: 'UTF-8' });
43+
const sourceFile = ts.createSourceFile(
44+
filePath,
45+
contents,
46+
ts.ScriptTarget.ES2015
47+
);
48+
49+
const identifiers = new Set<string>();
50+
ts.forEachChild(sourceFile, (childNode: ts.Node) =>
51+
extractIdentifiersFromNodeAndChildren(childNode, identifiers)
52+
);
53+
54+
identifiers.forEach(api => {
55+
publicIdentifiers.add(api);
56+
});
57+
}
58+
59+
return publicIdentifiers;
60+
}

0 commit comments

Comments
 (0)