Skip to content

Add minified ("mangled") Firestore build #2495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jan 13, 2020
Merged
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
node_modules
dist
.awcache
.cache
/config/project.json
scripts/docgen/html

Expand Down
18 changes: 18 additions & 0 deletions packages/firebase/firestore/index.min.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @license
* Copyright 2020 Google Inc.
*
* 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 '../../firestore/dist/index.esm.min';
77 changes: 46 additions & 31 deletions packages/firebase/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,38 @@ import pkg from './package.json';

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

function createUmdOutputConfig(output) {
return {
file: output,
format: 'umd',
sourcemap: true,
extend: true,
name: GLOBAL_NAME,
globals: {
'@firebase/app': GLOBAL_NAME
},

/**
* use iife to avoid below error in the old Safari browser
* SyntaxError: Functions cannot be declared in a nested block in strict mode
* https://github.com/firebase/firebase-js-sdk/issues/1228
*
*/
intro: `
try {
(function() {`,
outro: `
}).apply(this, arguments);
} catch(err) {
console.error(err);
throw new Error(
'Cannot instantiate ${output} - ' +
'be sure to load firebase-app.js first.'
);
}`
};
}

const plugins = [
sourcemaps(),
resolveModule(),
Expand Down Expand Up @@ -107,43 +139,21 @@ const componentBuilds = pkg.components
},
{
input: `${component}/index.ts`,
output: {
file: `firebase-${component}.js`,
format: 'umd',
sourcemap: true,
extend: true,
name: GLOBAL_NAME,
globals: {
'@firebase/app': GLOBAL_NAME
},

/**
* use iife to avoid below error in the old Safari browser
* SyntaxError: Functions cannot be declared in a nested block in strict mode
* https://github.com/firebase/firebase-js-sdk/issues/1228
*
*/

intro: `
try {
(function() {`,
outro: `
}).apply(this, arguments);
} catch(err) {
console.error(err);
throw new Error(
'Cannot instantiate firebase-${component} - ' +
'be sure to load firebase-app.js first.'
);
}`
},
output: createUmdOutputConfig(`firebase-${component}.js`),
plugins: [...plugins, uglify()],
external: ['@firebase/app']
}
];
})
.reduce((a, b) => a.concat(b), []);

const firestoreMinifiedBuild = {
input: `firestore/index.min.ts`,
output: createUmdOutputConfig(`firebase-firestore.min.js`),
plugins: [...plugins, uglify()],
external: ['@firebase/app']
};

/**
* Complete Package Builds
*/
Expand Down Expand Up @@ -257,4 +267,9 @@ const completeBuilds = [
}
];

export default [...appBuilds, ...componentBuilds, ...completeBuilds];
export default [
...appBuilds,
...componentBuilds,
firestoreMinifiedBuild,
...completeBuilds
];
3 changes: 2 additions & 1 deletion packages/firestore/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
/src
test
.idea
.cache

# Files not needed by end users
gulpfile.js
index.ts
karma.conf.js
tsconfig.json
tsconfig.json
27 changes: 27 additions & 0 deletions packages/firestore/externs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"externs" : [
"node_modules/typescript/lib/lib.es5.d.ts",
"node_modules/typescript/lib/lib.dom.d.ts",
"node_modules/typescript/lib/lib.es2015.promise.d.ts",
"node_modules/typescript/lib/lib.es2015.symbol.d.ts",
"node_modules/typescript/lib/lib.es2015.iterable.d.ts",
"node_modules/typescript/lib/lib.es2015.collection.d.ts",
"node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts",
"node_modules/typescript/lib/lib.es2015.core.d.ts",
"node_modules/typescript/lib/lib.es2017.object.d.ts",
"packages/app-types/index.d.ts",
"packages/app-types/private.d.ts",
"packages/auth-interop-types/index.d.ts",
"packages/firestore-types/index.d.ts",
"packages/firebase/index.d.ts",
"packages/component/dist/src/component.d.ts",
"packages/component/dist/src/provider.d.ts",
"packages/component/dist/src/component_container.d.ts",
"packages/logger/dist/src/logger.d.ts",
"packages/webchannel-wrapper/src/index.d.ts",
"packages/util/dist/src/environment.d.ts",
"packages/firestore/src/protos/firestore_proto_api.d.ts",
"packages/firestore/dist/lib/src/local/indexeddb_schema.d.ts",
"packages/firestore/dist/lib/src/local/shared_client_state_schema.d.ts"
]
}
6 changes: 6 additions & 0 deletions packages/firestore/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "",
"author": "Firebase <[email protected]> (https://firebase.google.com/)",
"scripts": {
"prebuild": "tsc -d --downlevelIteration --declarationDir dist/lib --emitDeclarationOnly && tsc -m es2015 --moduleResolution node scripts/*.ts ",
"build": "rollup -c",
"build:console": "node tools/console.build.js",
"dev": "rollup -c -w",
Expand All @@ -24,8 +25,11 @@
},
"main": "dist/index.node.cjs.js",
"browser": "dist/index.cjs.js",
"browserMinified": "dist/index.cjs.min.js",
"module": "dist/index.esm.js",
"moduleMinified": "dist/index.esm.min.js",
"esm2017": "dist/index.esm2017.js",
"esm2017Minified": "dist/index.esm2017.min.js",
"license": "Apache-2.0",
"files": [
"dist"
Expand All @@ -48,8 +52,10 @@
"protobufjs": "6.8.8",
"rollup": "1.28.0",
"rollup-plugin-copy-assets": "1.1.0",
"rollup-plugin-json": "4.0.0",
"rollup-plugin-node-resolve": "5.2.0",
"rollup-plugin-replace": "2.2.0",
"rollup-plugin-terser": "5.1.3",
"rollup-plugin-typescript2": "0.25.3",
"typescript": "3.7.3"
},
Expand Down
84 changes: 82 additions & 2 deletions packages/firestore/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,72 @@
* limitations under the License.
*/

import * as path from 'path';

import json from 'rollup-plugin-json';
import typescriptPlugin from 'rollup-plugin-typescript2';
import replace from 'rollup-plugin-replace';
import copy from 'rollup-plugin-copy-assets';
import typescript from 'typescript';
import { terser } from 'rollup-plugin-terser';

import { renameInternals } from './scripts/rename-internals';
import { extractPublicIdentifiers } from './scripts/extract-api';
import pkg from './package.json';
import { externs } from './externs.json';

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

// Extract all identifiers used in external APIs (to be used as a blacklist by
// the SDK minifier).
const externsPaths = externs.map(p => path.resolve(__dirname, '../../', p));
const publicIdentifiers = extractPublicIdentifiers(externsPaths);
const transformers = [
service => ({
before: [
renameInternals(service.getProgram(), {
publicIdentifiers,
prefix: '__PRIVATE_'
})
],
after: []
})
];

const terserOptions = {
output: {
comments: false
},
mangle: {
properties: {
regex: /^__PRIVATE_/
}
}
};

/**
* ES5 Builds
*/
const es5BuildPlugins = [
typescriptPlugin({
typescript
typescript,
cacheRoot: './.cache/es5/'
}),
json()
];

const es5MinifiedBuildPlugins = [
typescriptPlugin({
typescript,
transformers,
cacheRoot: './.cache/es5.min/'
}),
json(),
terser(terserOptions)
];

const es5Builds = [
/**
* Node.js Build
Expand Down Expand Up @@ -69,6 +114,15 @@ const es5Builds = [
],
plugins: es5BuildPlugins,
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`))
},
{
input: 'index.ts',
output: [
{ file: pkg.browserMinified, format: 'cjs', sourcemap: true },
{ file: pkg.moduleMinified, format: 'es', sourcemap: true }
],
plugins: es5MinifiedBuildPlugins,
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`))
}
];

Expand All @@ -82,11 +136,27 @@ const es2017BuildPlugins = [
compilerOptions: {
target: 'es2017'
}
}
},
cacheRoot: './.cache/es2017/'
}),
json({ preferConst: true })
];

const es2017MinifiedBuildPlugins = [
typescriptPlugin({
typescript,
tsconfigOverride: {
compilerOptions: {
target: 'es2017'
}
},
cacheRoot: './.cache/es2017.min/',
transformers
}),
json({ preferConst: true }),
terser(terserOptions)
];

const es2017Builds = [
/**
* Browser Build
Expand All @@ -100,6 +170,16 @@ const es2017Builds = [
},
plugins: es2017BuildPlugins,
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`))
},
{
input: 'index.ts',
output: {
file: pkg.esm2017Minified,
format: 'es',
sourcemap: true
},
plugins: es2017MinifiedBuildPlugins,
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`))
}
];

Expand Down
61 changes: 61 additions & 0 deletions packages/firestore/scripts/extract-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* @license
* Copyright 2020 Google Inc.
*
* 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.
*/

// eslint-disable-next-line import/no-extraneous-dependencies
import * as ts from 'typescript';
import * as fs from 'fs';

function extractIdentifiersFromNodeAndChildren(
node: ts.Node,
symbols: Set<string>
): void {
if (ts.isIdentifier(node)) {
symbols.add(node.escapedText.toString());
}

ts.forEachChild(node, (childNode: ts.Node) =>
extractIdentifiersFromNodeAndChildren(childNode, symbols)
);
}

/**
* Traverses TypeScript type definition files and returns the list of referenced
* identifiers.
*/
export function extractPublicIdentifiers(filePaths: string[]): Set<string> {
const publicIdentifiers = new Set<string>();

for (const filePath of filePaths) {
const contents = fs.readFileSync(filePath, { encoding: 'UTF-8' });
const sourceFile = ts.createSourceFile(
filePath,
contents,
ts.ScriptTarget.ES2015
);

const identifiers = new Set<string>();
ts.forEachChild(sourceFile, (childNode: ts.Node) =>
extractIdentifiersFromNodeAndChildren(childNode, identifiers)
);

identifiers.forEach(api => {
publicIdentifiers.add(api);
});
}

return publicIdentifiers;
}
Loading