Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

chore(ci): stop bundle-size report colliding chunk basenames across bundles (#59626)

Paul D'Ambra4f14dcec66ee
diff --git a/.gitignore b/.gitignore
index 9f15e454d979..54067d95d565 100644
--- a/.gitignore
+++ b/.gitignore
@@ -129,6 +129,7 @@ playwright/__snapshots__/
frontend/.cache/
frontend/@posthog/apps-common/dist/
frontend/dist/
+frontend/dist-report/
frontend/pnpm-error.log
frontend/public/Matter*.woff*
frontend/tmp
diff --git a/common/esbuilder/utils.mjs b/common/esbuilder/utils.mjs
index cdd2785cb946..3c956b3b12c7 100644
--- a/common/esbuilder/utils.mjs
+++ b/common/esbuilder/utils.mjs
@@ -510,7 +510,7 @@ export async function buildOrWatch(config) {
if (writeMetaFile) {
await fs.writeFile(
- `${config.name.toLowerCase().replace(' ', '-')}-esbuild-meta.json`,
+ `${config.name.toLowerCase().replace(/ /g, '-')}-esbuild-meta.json`,
JSON.stringify(buildResult.metafile)
)
}
diff --git a/frontend/bin/build-bundle-report.mjs b/frontend/bin/build-bundle-report.mjs
new file mode 100755
index 000000000000..4be41dc9b611
--- /dev/null
+++ b/frontend/bin/build-bundle-report.mjs
@@ -0,0 +1,92 @@
+#!/usr/bin/env node
+import fse from 'fs-extra'
+import path from 'node:path'
+import { fileURLToPath } from 'node:url'
+
+const __dirname = path.dirname(fileURLToPath(import.meta.url))
+const frontendDir = path.resolve(__dirname, '..')
+const reportDir = path.join(frontendDir, 'dist-report')
+
+const META_SUFFIX = '-esbuild-meta.json'
+
+// entryPoint can be `../products/...` for workspace imports — flatten `..` so the
+// destination stays under the per-bundle subdir of dist-report/.
+// The CI bundle-size action's `**/_chunks/chunk*.js` exclude (in
+// .github/workflows/ci-frontend.yml) depends on the `_chunks` literal below;
+// keep them in sync if either changes.
+function sanitizeSourcePath(p) {
+ return p
+ .split(path.sep)
+ .map((s) => (s === '..' ? '_parent' : s))
+ .join(path.sep)
+}
+
+function discoverMetafiles() {
+ return fse
+ .readdirSync(frontendDir)
+ .filter((name) => name.endsWith(META_SUFFIX))
+ .map((name) => ({
+ name: name.slice(0, -META_SUFFIX.length),
+ metaPath: path.join(frontendDir, name),
+ }))
+ .sort((a, b) => a.name.localeCompare(b.name))
+}
+
+fse.removeSync(reportDir)
+fse.mkdirSync(reportDir, { recursive: true })
+
+const metafiles = discoverMetafiles()
+if (metafiles.length === 0) {
+ console.error(
+ `Bundle report: no ${META_SUFFIX} files found in ${frontendDir}. ` +
+ `Each production esbuild config must have writeMetaFile enabled — see frontend/build.mjs. ` +
+ `Did the esbuild step run?`
+ )
+ process.exit(1)
+}
+
+let totalCopied = 0
+for (const { name, metaPath } of metafiles) {
+ let meta
+ try {
+ meta = JSON.parse(fse.readFileSync(metaPath, 'utf-8'))
+ } catch (err) {
+ console.error(`Bundle report: failed to parse ${metaPath}: ${err.message}`)
+ process.exit(1)
+ }
+
+ let copiedForBundle = 0
+ for (const [outPath, outInfo] of Object.entries(meta.outputs)) {
+ if (!outPath.endsWith('.js')) {
+ continue
+ }
+ const src = path.resolve(frontendDir, outPath)
+ if (!fse.existsSync(src)) {
+ console.warn(`Bundle report: ${name} metafile lists ${outPath} but file is missing on disk; skipping`)
+ continue
+ }
+
+ const baseFile = path.basename(outPath)
+ const subPath =
+ outInfo.entryPoint && outInfo.entryPoint !== '.'
+ ? path.join(sanitizeSourcePath(path.dirname(outInfo.entryPoint)), baseFile)
+ : path.join('_chunks', baseFile)
+
+ const dest = path.join(reportDir, name, subPath)
+ fse.mkdirSync(path.dirname(dest), { recursive: true })
+ fse.copyFileSync(src, dest)
+ copiedForBundle++
+ totalCopied++
+ }
+
+ if (copiedForBundle === 0) {
+ console.error(`Bundle report: bundle "${name}" contributed zero .js outputs; metafile may be stale`)
+ process.exit(1)
+ }
+}
+
+console.info(
+ `Bundle report: wrote ${totalCopied} files from ${metafiles.length} bundle(s) (${metafiles
+ .map((m) => m.name)
+ .join(', ')}) to ${reportDir}`
+)
diff --git a/frontend/build.mjs b/frontend/build.mjs
index 0791c6fe47cb..861b986993e9 100755
--- a/frontend/build.mjs
+++ b/frontend/build.mjs
@@ -36,6 +36,7 @@ await import('./build-products.mjs')
const common = {
absWorkingDir: __dirname,
bundle: true,
+ writeMetaFile: !isDev,
}
await buildInParallel(
diff --git a/frontend/package.json b/frontend/package.json
index e1398247ff0e..12306c5d66e7 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -19,6 +19,7 @@
"copy-scripts": "mkdir -p dist/ && ./bin/copy-posthog-js",
"clean": "rm -rf dist && mkdir dist",
"build": "pnpm copy-scripts && pnpm build:esbuild",
+ "build:with-report": "pnpm build && node ./bin/build-bundle-report.mjs",
"build:esbuild": "DEBUG=0 node ./build.mjs",
"build:products": "DEBUG=0 node build-products.mjs",
"start": "pnpm start-http",