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 5 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';
15 changes: 14 additions & 1 deletion packages/firebase/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ 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 +268,9 @@ const completeBuilds = [
}
];

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

6 changes: 5 additions & 1 deletion packages/firestore/index.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ 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
6 changes: 5 additions & 1 deletion packages/firestore/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ 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
87 changes: 77 additions & 10 deletions packages/firestore/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,28 +73,68 @@ const es5BuildPlugins = [
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: [
...es5BuildPlugins,
// 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 Builds
*/
Expand All @@ -106,6 +146,23 @@ const es5Builds = [
],
plugins: es5BuildPlugins,
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`))
},
{
input: 'index.memory.ts',
output: [
{
file: pkg.browserMemoryPersistence,
format: 'cjs',
sourcemap: true
},
{
file: pkg.moduleMemoryPersistence,
format: 'es',
sourcemap: true
}
],
plugins: es5BuildPlugins,
external: resolveMemoryExterns
}
];

Expand Down Expand Up @@ -140,6 +197,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