Skip to content
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

Firestore: Add tests to verify bundles produced by rollup #7972

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions packages/firestore/externs.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"packages/firestore/src/local/indexeddb_schema.ts",
"packages/firestore/src/local/indexeddb_schema_legacy.ts",
"packages/firestore/src/local/shared_client_state_schema.ts",
"packages/firestore/src/util/rollup_info.ts",
"packages/firestore/src/util/testing_hooks.ts"
]
}
2 changes: 2 additions & 0 deletions packages/firestore/lite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,5 @@ export { GeoPoint } from '../src/lite-api/geo_point';
export { Timestamp } from '../src/lite-api/timestamp';

export { FirestoreErrorCode, FirestoreError } from '../src/util/error';

export { ROLLUP_BUNDLE_ID as _ROLLUP_BUNDLE_ID } from '../src/util/rollup_info';
30 changes: 24 additions & 6 deletions packages/firestore/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ const allBuilds = [
},
plugins: [
...util.es2017ToEs5Plugins(/* mangled= */ false),
replace(generateBuildTargetReplaceConfig('cjs', 2017))
replace(generateBuildTargetReplaceConfig('cjs', 2017)),
replace({
'__FIRESTORE_ROLLUP_BUNDLE_ID__': 'fstrbid_node_cjs'
})
],
external: util.resolveNodeExterns,
treeshake: {
Expand All @@ -115,7 +118,10 @@ const allBuilds = [
},
plugins: [
sourcemaps(),
replace(generateBuildTargetReplaceConfig('esm', 2017))
replace(generateBuildTargetReplaceConfig('esm', 2017)),
replace({
'__FIRESTORE_ROLLUP_BUNDLE_ID__': 'fstrbid_node_esm'
})
],
external: util.resolveNodeExterns,
treeshake: {
Expand Down Expand Up @@ -150,7 +156,10 @@ const allBuilds = [
],
plugins: [
...util.es2017ToEs5Plugins(/* mangled= */ true),
replace(generateBuildTargetReplaceConfig('esm', 5))
replace(generateBuildTargetReplaceConfig('esm', 5)),
replace({
'__FIRESTORE_ROLLUP_BUNDLE_ID__': 'fstrbid_browser_esm5'
})
],
external: util.resolveBrowserExterns,
treeshake: {
Expand All @@ -169,7 +178,10 @@ const allBuilds = [
],
plugins: [
sourcemaps(),
replace(generateBuildTargetReplaceConfig('cjs', 2017))
replace(generateBuildTargetReplaceConfig('cjs', 2017)),
replace({
'__FIRESTORE_ROLLUP_BUNDLE_ID__': 'fstrbid_browser_cjs'
})
],
external: util.resolveBrowserExterns,
treeshake: {
Expand All @@ -188,7 +200,10 @@ const allBuilds = [
],
plugins: [
sourcemaps(),
replace(generateBuildTargetReplaceConfig('esm', 2017))
replace(generateBuildTargetReplaceConfig('esm', 2017)),
replace({
'__FIRESTORE_ROLLUP_BUNDLE_ID__': 'fstrbid_browser_esm'
})
],
external: util.resolveBrowserExterns,
treeshake: {
Expand All @@ -206,7 +221,10 @@ const allBuilds = [
plugins: [
alias(util.generateAliasConfig('rn')),
...browserPlugins(),
replace(generateBuildTargetReplaceConfig('esm', 2017))
replace(generateBuildTargetReplaceConfig('esm', 2017)),
replace({
'__FIRESTORE_ROLLUP_BUNDLE_ID__': 'fstrbid_react_native'
})
],
external: util.resolveBrowserExterns,
treeshake: {
Expand Down
20 changes: 13 additions & 7 deletions packages/firestore/rollup.config.lite.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ const allBuilds = [
}),
json(),
sourcemaps(),
replace(generateBuildTargetReplaceConfig('cjs', 5))
replace(generateBuildTargetReplaceConfig('cjs', 5)),
replace({ '__FIRESTORE_ROLLUP_BUNDLE_ID__': 'lite.node.cjs' })
],
external: util.resolveNodeExterns,
treeshake: {
Expand All @@ -125,7 +126,8 @@ const allBuilds = [
},
plugins: [
sourcemaps(),
replace(generateBuildTargetReplaceConfig('esm', 2017))
replace(generateBuildTargetReplaceConfig('esm', 2017)),
replace({ '__FIRESTORE_ROLLUP_BUNDLE_ID__': 'lite.node.mjs' })
],
external: util.resolveNodeExterns,
treeshake: {
Expand Down Expand Up @@ -167,7 +169,8 @@ const allBuilds = [
],
plugins: [
...util.es2017ToEs5Plugins(/* mangled= */ true),
replace(generateBuildTargetReplaceConfig('esm', 5))
replace(generateBuildTargetReplaceConfig('esm', 5)),
replace({ '__FIRESTORE_ROLLUP_BUNDLE_ID__': 'lite.browser.esm5' })
],
external: util.resolveBrowserExterns,
treeshake: {
Expand All @@ -186,7 +189,8 @@ const allBuilds = [
],
plugins: [
sourcemaps(),
replace(generateBuildTargetReplaceConfig('cjs', 2017))
replace(generateBuildTargetReplaceConfig('cjs', 2017)),
replace({ '__FIRESTORE_ROLLUP_BUNDLE_ID__': 'lite.browser.cjs' })
],
external: util.resolveBrowserExterns,
treeshake: {
Expand All @@ -205,7 +209,8 @@ const allBuilds = [
],
plugins: [
sourcemaps(),
replace(generateBuildTargetReplaceConfig('esm', 2017))
replace(generateBuildTargetReplaceConfig('esm', 2017)),
replace({ '__FIRESTORE_ROLLUP_BUNDLE_ID__': 'lite.browser.mjs' })
],
external: util.resolveBrowserExterns,
treeshake: {
Expand All @@ -225,8 +230,9 @@ const allBuilds = [
...browserPlugins(),
replace({
...generateBuildTargetReplaceConfig('esm', 2017),
'__RUNTIME_ENV__': 'rn'
})
'__RUNTIME_ENV__': 'rn',
'__FIRESTORE_ROLLUP_BUNDLE_ID__': 'lite.rn'
}),
],
external: util.resolveBrowserExterns,
treeshake: {
Expand Down
1 change: 1 addition & 0 deletions packages/firestore/rollup.shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const typescriptPlugin = require('rollup-plugin-typescript2');
const typescript = require('typescript');
const { terser } = require('rollup-plugin-terser');
const path = require('path');
const replace = require('rollup-plugin-replace');
const sourcemaps = require('rollup-plugin-sourcemaps');

const { renameInternals } = require('./scripts/rename-internals');
Expand Down
1 change: 1 addition & 0 deletions packages/firestore/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,4 @@ export {
TestingHooks as _TestingHooks
} from './util/testing_hooks';
export { ExistenceFilterMismatchInfo as _TestingHooksExistenceFilterMismatchInfo } from './util/testing_hooks_spi';
export { ROLLUP_BUNDLE_ID as _ROLLUP_BUNDLE_ID } from './util/rollup_info';
32 changes: 32 additions & 0 deletions packages/firestore/src/util/rollup_info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @license
* Copyright 2024 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.
*/

/**
* The unique ID for this bundled version of the Firestore SDK as assigned by
* rollup.js at bundling time, or some undefined value if the SDK was not
* bundled with rollup.
*
* The ID can be used to determine which rollup.js plugins were used to bundle
* the SDK. See `../../rollup.shared.js` for details about how this value gets
* set by rollup at bundling time.
*
* The initial use case for this value is in tests that verify the bundled
* output of rollup.js.
*
* @internal
*/
export const ROLLUP_BUNDLE_ID = '__FIRESTORE_ROLLUP_BUNDLE_ID__';

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/firestore/test/rollup/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extension": "ts",
"require": "ts-node/register",
"spec": "test/**/*.test.ts",
"timeout": 60000
}
3 changes: 3 additions & 0 deletions packages/firestore/test/rollup/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This directory contains tests for the rollup bundling process.
It does not test the Firestore SDK itself, but rather the SDK bundles
that get produced by rollup when commands like `yarn build` are run.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions packages/firestore/test/rollup/bundle_id/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
This test verifies that the @firebase/firestore bundle that node.js chooses
is the one that is expected.

# Usage

Run both of these commands:

```
node test.cjs
```

to test that the commonjs export from @firebase/firestore is used and

```
node test.mjs
```

to test that the ES modules export from @firebase/firestore is used.
21 changes: 21 additions & 0 deletions packages/firestore/test/rollup/bundle_id/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @license
* Copyright 2024 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.
*/

const { _ROLLUP_BUNDLE_ID } = require('@firebase/firestore');
const { stdout } = require('node:process');

stdout.write(_ROLLUP_BUNDLE_ID);
21 changes: 21 additions & 0 deletions packages/firestore/test/rollup/bundle_id/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @license
* Copyright 2024 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 { _ROLLUP_BUNDLE_ID } from '@firebase/firestore';
import { stdout } from 'node:process';

stdout.write(_ROLLUP_BUNDLE_ID);
17 changes: 17 additions & 0 deletions packages/firestore/test/rollup/bundle_id/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"scripts": {
"rollup": "rollup --config rollup.config.ts --configPlugin typescript2='{\"tsconfig\":\"tsconfig.rollup.json\",\"verbosity\":2}'"
},
"dependencies": {
"@firebase/firestore": "file:../../.."
},
"devDependencies": {
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^15.2.3",
"@tsconfig/node16": "^16.1.1",
"rollup": "^4.9.5",
"rollup-plugin-typescript2": "^0.36.0",
"typescript": "^5.3.3"
}
}
80 changes: 80 additions & 0 deletions packages/firestore/test/rollup/bundle_id/rollup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import type { RollupOptions } from 'rollup';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';

function browserCommonJsRollupOptions(): RollupOptions {
return {
input: 'index.cjs',
output: {
file: 'dist/index.browser.cjs',
format: 'cjs'
},
plugins: [commonjs(), nodeResolve({ browser: true })]
};
}

function browserESModuleRollupOptions(): RollupOptions {
return {
input: 'index.mjs',
output: {
file: 'dist/index.browser.mjs',
format: 'es'
},
plugins: [commonjs(), nodeResolve({ browser: true })]
};
}

function browserEsm5RollupOptions(): RollupOptions {
return {
input: 'index.mjs',
output: {
file: 'dist/index.browser.esm5.mjs',
format: 'es'
},
plugins: [
commonjs(),
nodeResolve({ browser: true, exportConditions: ['esm5'] })
]
};
}

function nodeCommonJsRollupOptions(): RollupOptions {
return {
input: 'index.cjs',
output: {
file: 'dist/index.node.cjs',
format: 'cjs'
},
plugins: [
json(),
commonjs(),
nodeResolve({ browser: false, exportConditions: ['node'] })
]
};
}

function nodeESModuleRollupOptions(): RollupOptions {
return {
input: 'index.mjs',
output: {
file: 'dist/index.node.mjs',
format: 'es'
},
plugins: [
json(),
commonjs(),
nodeResolve({ browser: false, exportConditions: ['node'] })
]
};
}

function* allRollupOptions(): Generator<RollupOptions> {
yield browserCommonJsRollupOptions();
yield browserESModuleRollupOptions();
yield browserEsm5RollupOptions();
yield nodeCommonJsRollupOptions();
yield nodeESModuleRollupOptions();
}

export default [...allRollupOptions()] satisfies RollupOptions[];
Loading
Loading