diff --git a/handwritten/firestore/bundle-budget.json b/handwritten/firestore/bundle-budget.json new file mode 100644 index 000000000000..635ff53a6270 --- /dev/null +++ b/handwritten/firestore/bundle-budget.json @@ -0,0 +1,9 @@ +{ + "serverless_minimal_bundle": { + "entrypoint": "./build/src/index.js", + "target_minified_bytes": 2293760, + "max_minified_bytes_deduped": 2500000, + "max_minified_bytes_undeduped": 5000000, + "description": "2.19 MB minified target budget for standard serverless Cloud Function deployment" + } +} diff --git a/handwritten/firestore/package.json b/handwritten/firestore/package.json index 9007d1de6acb..9bb3b8ee7a79 100644 --- a/handwritten/firestore/package.json +++ b/handwritten/firestore/package.json @@ -52,6 +52,8 @@ "system-test:emulator": "concurrently -p \"[{name}]\" -n \"grpc,rest,enterprise-grpc,enterprise-rest\" -c \"cyan,magenta,blue,yellow\" \"npm:system-test:emulator:grpc\" \"npm:system-test:emulator:rest\" \"npm:system-test:enterprise:emulator:grpc\" \"npm:system-test:enterprise:emulator:rest\"", "presystem-test": "npm run compile", "conformance": "mocha build/conformance", + "precheck-bundle-size": "npm run compile", + "check-bundle-size": "node scripts/check-bundle-size.js", "test-only": "c8 mocha build/test", "pretest": "npm run compile", "test": "npm run test-only && npm run conformance", diff --git a/handwritten/firestore/scripts/check-bundle-size.js b/handwritten/firestore/scripts/check-bundle-size.js new file mode 100644 index 000000000000..3dd028a24bed --- /dev/null +++ b/handwritten/firestore/scripts/check-bundle-size.js @@ -0,0 +1,34 @@ +const fs = require('fs'); +const path = require('path'); +const os = require('os'); +const { execSync } = require('child_process'); +const budget = require('../bundle-budget.json'); + +const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'firestore-bundle-')); +const entrypoint = path.join(tmpDir, 'test_bundle_input.js'); +const outfile = path.join(tmpDir, 'test_bundle_out.min.js'); +const buildIndexPath = path.resolve(__dirname, '../build/src/index'); + +fs.writeFileSync( + entrypoint, + '\nconst { Firestore } = require(' + + JSON.stringify(buildIndexPath) + + ');\nconst f = new Firestore();\nconsole.log(f);\n', + 'utf8' +); + +try { + execSync('npx --yes esbuild --bundle --platform=node --target=node18 --minify "' + entrypoint + '" --outfile="' + outfile + '"', { + stdio: 'inherit', + }); + const stats = fs.statSync(outfile); + const size = stats.size; + console.log(`[test-bundle-size] Compiled serverless bundle size: ${size} bytes`); + if (size > budget.maxBundleSize) { + console.error(`[test-bundle-size] ERROR: Bundle size (${size} bytes) exceeds budget (${budget.maxBundleSize} bytes)`); + process.exit(1); + } + console.log('[test-bundle-size] SUCCESS: Bundle size is within budget.'); +} finally { + fs.rmSync(tmpDir, { recursive: true, force: true }); +}