Skip to content

Memory-only Firestore build #2608

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 23 commits into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions integration/firestore/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ function clean() {
return del(['temp/**/*', 'dist/**/*']);
}

function isPersistenceEnabled() {
return process.env.DISABLE_FIRESTORE_PERSISTENCE !== 'true';
}

function copyTests() {
/**
* NOTE: We intentionally don't copy src/ files (to make sure we are only
Expand All @@ -37,7 +41,9 @@ function copyTests() {
const firebaseAppSdk = 'firebase/app/dist/index.esm.js';
const firebaseFirestoreSdk = resolve(
__dirname,
'../../packages/firestore/dist/index.esm.js'
isPersistenceEnabled()
? '../../packages/firestore/dist/index.esm.js'
: '../../packages/firestore/dist/index.memory.esm.js'
);
return gulp
.src(
Expand All @@ -63,7 +69,14 @@ function copyTests() {
*/
/import\s+firebase\s+from\s+('|")[^\1]+firebase_export\1;?/,
`import firebase from '${firebaseAppSdk}';
import '${firebaseFirestoreSdk}';`
import '${firebaseFirestoreSdk}';

if (typeof process === 'undefined') {
process = { env: { DISABLE_FIRESTORE_PERSISTENCE: '${!isPersistenceEnabled()}' } } as any;
} else {
process.env.DISABLE_FIRESTORE_PERSISTENCE = '${!isPersistenceEnabled()}';
}
`
)
)
.pipe(
Expand Down
13 changes: 10 additions & 3 deletions integration/firestore/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@
"version": "1.0.1",
"private": true,
"scripts": {
"build": "(cd ../../ ; yarn build) ; gulp compile-tests",
"build:dependencies": "(cd ../../ ; yarn build)",
"build": "yarn build:dependencies; gulp compile-tests",
"build:memory": "yarn build:dependencies; DISABLE_FIRESTORE_PERSISTENCE=true gulp compile-tests",
"pretest": "yarn build:dependencies",
"pretest:manual": "yarn build",
"pretest:manual:memory": "yarn build:memory",
"pretest:debug": "yarn build",
"test": "echo 'Automated tests temporarily disabled, run `yarn test:manual` to execute these tests'",
"pretest:debug:memort": "yarn build:memory",
"test": "gulp compile-tests; karma start --single-run; DISABLE_FIRESTORE_PERSISTENCE=true gulp compile-tests; karma start --single-run;",
"test:manual": "karma start --single-run",
"test:debug": "karma start --auto-watch --browsers Chrome"
"test:manual:memory": "karma start --single-run",
"test:debug": "karma start --auto-watch --browsers Chrome",
"test:debug:memory": "karma start --single-run"
},
"devDependencies": {
"@types/mocha": "7.0.1",
Expand Down
18 changes: 18 additions & 0 deletions packages/firebase/firestore/index.memory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @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 '../../firestore/dist/index.memory.esm';
14 changes: 13 additions & 1 deletion packages/firebase/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@ const componentBuilds = pkg.components
})
.reduce((a, b) => a.concat(b), []);

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

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

export default [...appBuilds, ...componentBuilds, ...completeBuilds];
export default [
...appBuilds,
...componentBuilds,
firestoreMemoryBuild,
...completeBuilds
];
33 changes: 33 additions & 0 deletions packages/firestore/index.memory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @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 firebase from '@firebase/app';
import { FirebaseNamespace } from '@firebase/app-types';

import { configureForFirebase } from './src/platform/config';
import './register-module';
import './src/platform_browser/browser_init';

import { name, version } from './package.json';
import { Firestore, PublicMemoryFirestore } from './src/api/database';

export function registerFirestore(instance: FirebaseNamespace): void {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done (here and elsewhere)

configureForFirebase(instance, PublicMemoryFirestore, Firestore);
instance.registerVersion(name, version);
}

registerFirestore(firebase);
33 changes: 33 additions & 0 deletions packages/firestore/index.node.memory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @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 firebase from '@firebase/app';
import { FirebaseNamespace } from '@firebase/app-types';

import { configureForFirebase } from './src/platform/config';
import './register-module';
import './src/platform_node/node_init';

import { name, version } from './package.json';
import { Firestore, PublicMemoryFirestore } from './src/api/database';

export function registerFirestore(instance: FirebaseNamespace): void {
configureForFirebase(instance, PublicMemoryFirestore, Firestore);
instance.registerVersion(name, version);
}

registerFirestore(firebase);
10 changes: 9 additions & 1 deletion packages/firestore/index.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,17 @@ import './src/platform_node/node_init';
import { FirebaseNamespace } from '@firebase/app-types';

import { name, version } from './package.json';
import {
PersistenceFirestore,
PublicPersistenceFirestore
} from './src/api/persistence';

export function registerFirestore(instance: FirebaseNamespace): void {
configureForFirebase(instance);
configureForFirebase(
instance,
PublicPersistenceFirestore,
PersistenceFirestore
);
instance.registerVersion(name, version, 'node');
}

Expand Down
10 changes: 9 additions & 1 deletion packages/firestore/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,17 @@ import './src/platform_browser/browser_init';
import { FirebaseNamespace } from '@firebase/app-types';

import { name, version } from './package.json';
import {
PersistenceFirestore,
PublicPersistenceFirestore
} from './src/api/persistence';

export function registerFirestore(instance: FirebaseNamespace): void {
configureForFirebase(instance);
configureForFirebase(
instance,
PublicPersistenceFirestore,
PersistenceFirestore
);
instance.registerVersion(name, version);
}

Expand Down
6 changes: 5 additions & 1 deletion packages/firestore/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@
"test:node:persistence": "FIRESTORE_EMULATOR_PORT=8080 FIRESTORE_EMULATOR_PROJECT_ID=test-emulator USE_MOCK_PERSISTENCE=YES TS_NODE_CACHE=NO TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' nyc --reporter lcovonly -- mocha 'test/{,!(browser)/**/}*.test.ts' --require ts-node/register --require index.node.ts --require test/util/node_persistence.ts --opts ../../config/mocha.node.opts",
"test:node:persistence:prod": "USE_MOCK_PERSISTENCE=YES TS_NODE_CACHE=NO TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' nyc --reporter lcovonly -- mocha 'test/{,!(browser)/**/}*.test.ts' --require ts-node/register --require index.node.ts --require test/util/node_persistence.ts --opts ../../config/mocha.node.opts",
"test:travis": "ts-node --compiler-options='{\"module\":\"commonjs\"}' ../../scripts/emulator-testing/firestore-test-runner.ts",
"test:minified": "(cd ../../integration/firestore ; yarn test:manual)",
"test:minified": "(cd ../../integration/firestore ; yarn test)",
"prepare": "yarn build"
},
"main": "dist/index.node.cjs.js",
"mainMemoryPersistence": "dist/index.node.memory.cjs.js",
"browser": "dist/index.cjs.js",
"browserMemoryPersistence": "dist/index.memory.cjs.js",
"module": "dist/index.esm.js",
"moduleMemoryPersistence": "dist/index.memory.esm.js",
"esm2017": "dist/index.esm2017.js",
"esm2017MemoryPersistence": "dist/index.memory.esm2017.js",
"license": "Apache-2.0",
"files": [
"dist"
Expand Down
118 changes: 100 additions & 18 deletions packages/firestore/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,32 +62,81 @@ const terserOptions = {
}
};

/**
* ES5 Builds
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This configuration is now quite complex. For someone who isn't well versed in all this it's essentially inscrutable. This comment doesn't really help anyone who doesn't already know exactly what they're doing already.

What's an ES5 build? Where's it used (in the browser? node?)? Is this shipped to end users or an input into some later stage in the build?

This might be cumbersome to document inline so another way to approach this might be to put together an overview somewhere--either in comments in this file or in a doc--that described what all these pieces are and how they fit together.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expanded on these comments a bit, but I think it is a bit too much to ask to explain these concepts in comments to someone that is new to the JavaScript build process. So even with the new comments, there will be room for lots of questions. I spent a lot of time digging through the Rollup documentation, and I don't think it will be possible for someone to ramp up on this without doing the same.

@Feiyang1 Do you think we could provide a high-level document that describes some of the paradigms that we use in our SDKs?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I can add a page in go/jscore.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

const nodeBuildPlugins = [
...es5BuildPlugins,
// Needed as we also use the *.proto files
copy({
assets: ['./src/protos']
}),
replace({
'process.env.FIRESTORE_PROTO_ROOT': JSON.stringify('src/protos')
})
];

/**
* Returns the externs locations for the Memory-based Firestore implementation.
* Verifies that no persistence sources are used by Firestore's memory-only
* implementation.
*/
function resolveMemoryExterns(id, referencedBy) {
const externalRef = path
.resolve(path.dirname(referencedBy), id)
.replace('.ts', '');

const persistenceRef = [
'local/indexeddb_persistence.ts',
'local/indexeddb_index_manager.ts',
'local/indexeddb_mutation_queue.ts',
'local/indexeddb_remote_document_cache.ts',
'local/indexeddb_schema.ts',
'local/indexeddb_target_cache.ts',
'local/local_serializer.ts',
'local/simple_db.ts',
'api/persistence.ts'
].map(p => path.resolve(__dirname, 'src', p));

if (persistenceRef.indexOf(externalRef) !== -1) {
throw new Error('Unexpected reference in Memory-only client on ' + id);
}
return [...deps, 'util', 'path'].some(
dep => id === dep || id.startsWith(`${dep}/`)
);
}

const es5Builds = [
/**
* Node.js Build
*/
{
input: 'index.node.ts',
output: [{ file: pkg.main, format: 'cjs', sourcemap: true }],
plugins: [
typescriptPlugin({
typescript,
cacheRoot: './.cache/node.cjs/'
}),
json(),
// Needed as we also use the *.proto files
copy({
assets: ['./src/protos']
}),
replace({
'process.env.FIRESTORE_PROTO_ROOT': JSON.stringify('src/protos')
})
],
plugins: nodeBuildPlugins,
external: id =>
[...deps, 'util', 'path'].some(
dep => id === dep || id.startsWith(`${dep}/`)
)
},
{
input: 'index.node.memory.ts',
output: [
{ file: pkg.mainMemoryPersistence, format: 'cjs', sourcemap: true }
],
plugins: nodeBuildPlugins,
external: resolveMemoryExterns
},
/**
* Browser ESM Build
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comment to above: what's an ESM build? Why do we need this? How does it relate to the other things in here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two import styles in JavaScript. ESM uses import ... from ... and CJS uses require. I believe this was omitted from "JavaScript - the good parts". I added comments to name the exact configuration - this build rule builds ES5 JavaScript with ESM imports.

*/
Expand All @@ -105,17 +154,40 @@ const es5Builds = [
],
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`))
},
{
input: 'index.memory.ts',
output: { file: pkg.moduleMemoryPersistence, format: 'es', sourcemap: true },
plugins: [
typescriptPlugin({
typescript,
transformers,
cacheRoot: './.cache/esm/'
}),
json(),
terser(terserOptions)
],
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use resolveMemoryExterns instead ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

},
/**
* Browser CJS Build
*
* This build is based on the mangling in the ESM build above, since
* Browser CJS Build
*
* These builds are based on the mangling in the ESM build above, since
* Terser's Property name mangling doesn't work well with CJS's syntax.
*/
{
input: pkg.module,
output: { file: pkg.browser, format: 'cjs', sourcemap: true },
plugins: [ sourcemaps() ]
plugins: [sourcemaps()]
},
{
input: pkg.moduleMemoryPersistence,
output: {
file: pkg.browserMemoryPersistence,
format: 'cjs',
sourcemap: true
},
plugins: [sourcemaps()]
}
];

/**
Expand Down Expand Up @@ -149,6 +221,16 @@ const es2017Builds = [
},
plugins: es2017BuildPlugins,
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`))
},
{
input: 'index.memory.ts',
output: {
file: pkg.esm2017MemoryPersistence,
format: 'es',
sourcemap: true
},
plugins: es2017BuildPlugins,
external: resolveMemoryExterns
}
];

Expand Down
Loading