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.

perf(frontend): minify each app chunk on its own so unchanged chunks stay cached (#122919)

Paul D'Ambra31da92b4d555
diff --git a/.gitignore b/.gitignore
index 3059cecb5957..7cc77d6078c7 100644
--- a/.gitignore
+++ b/.gitignore
@@ -128,6 +128,9 @@ playwright/__snapshots__/
frontend/.cache/
frontend/@posthog/apps-common/dist/
frontend/dist/
+# Temporary folders of the app build's per-chunk minify step
+frontend/.unminified-chunks/
+frontend/.stable-chunks-rewritten/
frontend/dist-report/
frontend/eager-graph-report*.json
frontend/pnpm-error.log
diff --git a/common/esbuilder/minify.mjs b/common/esbuilder/minify.mjs
new file mode 100644
index 000000000000..4701ade2d029
--- /dev/null
+++ b/common/esbuilder/minify.mjs
@@ -0,0 +1,47 @@
+import { build } from 'esbuild'
+import fs from 'node:fs'
+import path from 'node:path'
+
+/**
+ * Minifies JS files one file at a time.
+ *
+ * When esbuild minifies while it bundles, a chunk's short names depend on the names it imports from
+ * other chunks. A change in one chunk then renames the variables of chunks whose code did not change.
+ * Minified one at a time, a file's short names depend only on its own code.
+ *
+ * `files` are absolute paths under `inputDir`. A file that names its source map in a
+ * `//# sourceMappingURL` comment gets a map that chains back to the original sources, with paths
+ * relative to `outdir`. Returns, per input file, the minified code (with no sourceMappingURL comment)
+ * and its map as JSON.
+ */
+export async function minifySeparately(files, { inputDir, outdir, target }) {
+ if (files.length === 0) {
+ return new Map()
+ }
+ // esbuild reads inputs at their real path, so a symlinked outdir would put the map's source paths
+ // relative to the wrong directory.
+ const realInputDir = fs.realpathSync(inputDir)
+ const realOutdir = fs.realpathSync(outdir)
+ const outputPathOf = (file) => path.join(realOutdir, path.relative(realInputDir, fs.realpathSync(file)))
+ const { outputFiles } = await build({
+ entryPoints: files.map((file) => fs.realpathSync(file)),
+ outbase: realInputDir,
+ outdir: realOutdir,
+ bundle: false,
+ format: 'esm',
+ minify: true,
+ target,
+ sourcemap: 'external',
+ write: false,
+ // The bundling build already warned about the source. Here every file is compiled output, so a
+ // warning would point at third-party code that the bundling build treats as a dependency.
+ logLevel: 'error',
+ })
+ const textByPath = new Map(outputFiles.map((file) => [file.path, file.text]))
+ return new Map(
+ files.map((file) => {
+ const outputPath = outputPathOf(file)
+ return [file, { code: textByPath.get(outputPath), map: textByPath.get(`${outputPath}.map`) }]
+ })
+ )
+}
diff --git a/common/esbuilder/utils.mjs b/common/esbuilder/utils.mjs
index 3ddc45799147..03fcdd5ede3c 100644
--- a/common/esbuilder/utils.mjs
+++ b/common/esbuilder/utils.mjs
@@ -536,8 +536,10 @@ let buildsInProgress = 0
// disposes its context before releasing the lock (see runBuild), so the next heavy build reuses
// the freed memory instead of stacking on top of it. Dev is unaffected: it keeps contexts alive
// for incremental watch rebuilds and isn't memory-constrained.
+// onBuildComplete runs after the lock is released, so heavy post-build work there must take the
+// lock too. The lock is not reentrant: fn must never call runExclusiveHeavy, or it waits forever.
let heavyBuildChain = Promise.resolve()
-function runExclusiveHeavy(fn) {
+export function runExclusiveHeavy(fn) {
const result = heavyBuildChain.then(fn)
heavyBuildChain = result.then(
() => {},
diff --git a/frontend/bin/stableChunkNames.mjs b/frontend/bin/stableChunkNames.mjs
index 2554fb78ff12..19a74d1255c5 100644
--- a/frontend/bin/stableChunkNames.mjs
+++ b/frontend/bin/stableChunkNames.mjs
@@ -17,7 +17,10 @@ import { moduleKey } from './loggedOutScenes.mjs'
* identity specifier (`@c/<identity>`) instead of a hashed path. An import map, rendered by the
* backend, resolves each identity to its current file. A chunk's identity comes from the source
* modules it contains, so it does not change when their code changes. A chunk's file name is a
- * hash of its own rewritten bytes, so it changes only when its own code changes.
+ * hash of its own rewritten and minified bytes, so it changes only when its own code changes.
+ *
+ * The app build does not minify identifiers. This step minifies each copy on its own, for the reason
+ * in minifySeparately.
*
* The copies sit next to esbuild's files in `dist/`, because some chunks resolve workers and wasm
* files relative to `import.meta.url`. Their names (`<prefix>-S<10 hex>.js`) cannot collide with
@@ -77,6 +80,15 @@ export function alphanumericStem(name) {
return name.replace(/[^A-Za-z0-9]+(.?)/g, (_, next) => next.toUpperCase())
}
+/**
+ * Shifts a source map's mappings down by the one line a prelude occupies. A `;` with no VLQ
+ * segments before it marks that first generated line as empty, so everything the original map
+ * described moves down to line two.
+ */
+export function shiftMappingsForPrelude(mappings) {
+ return `;${mappings}`
+}
+
export function stableFileName(originalFile, rewrittenSource) {
const prefix = originalFile.replace(/-[^-./]+\.js$/, '')
// Only holds because chunkNames/entryNames in common/esbuilder/utils.mjs are `[name]-[hash]` for non-dev builds.
@@ -93,8 +105,7 @@ export function stableFileName(originalFile, rewrittenSource) {
*
* `preludes` maps an output path to a function of its specifier that returns the chunk's first line.
*
- * Returns, per JS output, its identity, stable file name and rewritten source, plus the import
- * map.
+ * Returns, per JS output, its identity and rewritten source.
*/
export function planStableChunks(outputs, readSource, distPrefix = 'dist/', preludes = new Map()) {
const jsOutputs = Object.entries(outputs).filter(([outputPath]) => outputPath.endsWith('.js'))
@@ -118,7 +129,6 @@ export function planStableChunks(outputs, readSource, distPrefix = 'dist/', prel
const cssChunkFiles = new Set([...preludes.keys()].map(fileOf))
const plan = new Map()
- const imports = {}
for (const [outputPath] of jsOutputs) {
const file = fileOf(outputPath)
const rawSource = readSource(outputPath)
@@ -130,11 +140,9 @@ export function planStableChunks(outputs, readSource, distPrefix = 'dist/', prel
const identity = identityByFile.get(file)
const prelude = preludes.get(outputPath)?.(`${SPECIFIER_PREFIX}${identity}`) ?? null
const finalSource = prelude ? `${prelude}\n${source}` : source
- const stableFile = stableFileName(file, finalSource)
- plan.set(outputPath, { identity, file, stableFile, source: finalSource, mapValid, prelude })
- imports[`${SPECIFIER_PREFIX}${identity}`] = `static/${stableFile}`
+ plan.set(outputPath, { identity, file, source: finalSource, mapValid, prelude })
}
- return { plan, imports }
+ return { plan }
}
/**
@@ -194,9 +202,12 @@ export function loggedOutSpecifiers(
* manifest the backend reads. `chunks` is the chunk map (scene name -> esbuild chunk hashes) and
* `entrypoints` the absolute paths of the entry files, as `buildOrWatch` returns them. `loggedOut`
* holds the options for loggedOutSpecifiers. Without it, every page gets the full import map.
+ * `sourceDir` holds the build's unminified chunks and their maps, when they are no longer in `dist/`.
*/
-export function writeStableChunks({
+export async function writeStableChunks({
absWorkingDir,
+ sourceDir = path.resolve(absWorkingDir, 'dist'),
+ target,
outputs,
chunks,
entrypoints,
@@ -207,52 +218,90 @@ export function writeStableChunks({
loggedOut = null,
}) {
const distDir = path.resolve(absWorkingDir, 'dist')
- const { plan, imports } = planStableChunks(
+ const sourceOf = (file) => path.resolve(sourceDir, file)
+ const { plan } = planStableChunks(
outputs,
- (outputPath) => fs.readFileSync(path.resolve(absWorkingDir, outputPath), 'utf8'),
+ (outputPath) => fs.readFileSync(sourceOf(outputPath.slice('dist/'.length)), 'utf8'),
'dist/',
preludes
)
- Object.assign(imports, extraImports)
- const stableByFile = new Map([...plan.values()].map((entry) => [entry.file, entry.stableFile]))
- for (const { file, stableFile, source, mapValid, prelude } of plan.values()) {
- const mapFile = path.resolve(distDir, `${file}.map`)
- // A rewrite that changed the source's length invalidates the map's byte offsets, so serve
- // the source with no map rather than one that points at the wrong columns.
- const hasMap = fs.existsSync(mapFile) && mapValid
- fs.writeFileSync(
- path.resolve(distDir, stableFile),
- hasMap ? `${source}\n//# sourceMappingURL=${stableFile}.map\n` : source
+ // A sibling of dist/, so the relative source paths in the maps resolve the same way.
+ const rewrittenDir = path.resolve(absWorkingDir, '.stable-chunks-rewritten')
+ fs.rmSync(rewrittenDir, { recursive: true, force: true })
+ fs.mkdirSync(rewrittenDir)
+ try {
+ const hasMapByFile = new Map()
+ for (const { file, source, mapValid, prelude } of plan.values()) {
+ const mapFile = sourceOf(`${file}.map`)
+ // A rewrite that changed the source's length invalidates the map's byte offsets, so serve
+ // the source with no map rather than one that points at the wrong columns.
+ const hasMap = fs.existsSync(mapFile) && mapValid
+ hasMapByFile.set(file, hasMap)
+ fs.writeFileSync(
+ path.resolve(rewrittenDir, file),
+ hasMap ? `${source}\n//# sourceMappingURL=${file}.map\n` : source
+ )
+ if (hasMap) {
+ const map = JSON.parse(fs.readFileSync(mapFile, 'utf8'))
+ if (prelude) {
+ map.mappings = shiftMappingsForPrelude(map.mappings)
+ }
+ fs.writeFileSync(path.resolve(rewrittenDir, `${file}.map`), JSON.stringify(map))
+ }
+ }
+
+ // Imported here, not at the top: esbuild refuses to load under jsdom, where the planning tests run.
+ const { minifySeparately } = await import('@posthog/esbuilder/minify.mjs')
+ const minified = await minifySeparately(
+ [...plan.values()].map(({ file }) => path.resolve(rewrittenDir, file)),
+ { inputDir: rewrittenDir, outdir: distDir, target }
)
- if (hasMap && prelude) {
- const map = JSON.parse(fs.readFileSync(mapFile, 'utf8'))
- map.mappings = `;${map.mappings}`
- fs.writeFileSync(path.resolve(distDir, `${stableFile}.map`), JSON.stringify(map))
- } else if (hasMap) {
- fs.copyFileSync(mapFile, path.resolve(distDir, `${stableFile}.map`))
+ const imports = {}
+ const stableByFile = new Map()
+ for (const { identity, file } of plan.values()) {
+ const { code, map } = minified.get(path.resolve(rewrittenDir, file))
+ const stableFile = stableFileName(file, code)
+ const hasMap = hasMapByFile.get(file)
+ fs.writeFileSync(
+ path.resolve(distDir, stableFile),
+ hasMap ? `${code}//# sourceMappingURL=${stableFile}.map\n` : code
+ )
+ if (hasMap) {
+ fs.writeFileSync(path.resolve(distDir, `${stableFile}.map`), map)
+ }
+ imports[`${SPECIFIER_PREFIX}${identity}`] = `static/${stableFile}`
+ stableByFile.set(file, stableFile)
}
- }
+ Object.assign(imports, extraImports)
- const toStable = (file) => stableByFile.get(file) ?? file
- const chunkId = (file) => file.replace(/^chunk-/, '').replace(/\.js$/, '')
- const stableChunks = Object.fromEntries(
- Object.entries(chunks).map(([name, list]) => [name, list.map((hash) => chunkId(toStable(`chunk-${hash}.js`)))])
- )
- const stableEntrypoints = entrypoints.map((entrypoint) =>
- entrypoint.endsWith('.js') ? path.resolve(distDir, toStable(path.relative(distDir, entrypoint))) : entrypoint
- )
- const toStableUrl = (url) => `static/${toStable(url.replace(/^static\//, ''))}`
- const manifest = {
- imports,
- eagerCss: eagerCss.map((file) => `static/${file}`),
- // The backend renders only these import map entries for a logged-out page.
- loggedOutImports: loggedOut ? loggedOutSpecifiers(outputs, plan, loggedOut) : null,
- preload: {
- js: (preloadManifest?.js || []).map(toStableUrl),
- authenticatedJs: (preloadManifest?.authenticatedJs || []).map(toStableUrl),
- },
+ const toStable = (file) => stableByFile.get(file) ?? file
+ const chunkId = (file) => file.replace(/^chunk-/, '').replace(/\.js$/, '')
+ const stableChunks = Object.fromEntries(
+ Object.entries(chunks).map(([name, list]) => [
+ name,
+ list.map((hash) => chunkId(toStable(`chunk-${hash}.js`))),
+ ])
+ )
+ const stableEntrypoints = entrypoints.map((entrypoint) =>
+ entrypoint.endsWith('.js')
+ ? path.resolve(distDir, toStable(path.relative(distDir, entrypoint)))
+ : entrypoint
+ )
+ const toStableUrl = (url) => `static/${toStable(url.replace(/^static\//, ''))}`
+ const manifest = {
+ imports,
+ eagerCss: eagerCss.map((file) => `static/${file}`),
+ // The backend renders only these import map entries for a logged-out page.
+ loggedOutImports: loggedOut ? loggedOutSpecifiers(outputs, plan, loggedOut) : null,
+ preload: {
+ js: (preloadManifest?.js || []).map(toStableUrl),
+ authenticatedJs: (preloadManifest?.authenticatedJs || []).map(toStableUrl),
+ },
+ }
+ fs.writeFileSync(path.resolve(distDir, 'stable-chunks-manifest.json'), JSON.stringify(manifest))
+ return { chunks: stableChunks, entrypoints: stableEntrypoints, eagerCss, manifest }
+ } finally {
+ fs.rmSync(rewrittenDir, { recursive: true, force: true })
}
- fs.writeFileSync(path.resolve(distDir, 'stable-chunks-manifest.json'), JSON.stringify(manifest))
- return { chunks: stableChunks, entrypoints: stableEntrypoints, eagerCss, manifest }
}
diff --git a/frontend/bin/stableChunkNames.test.ts b/frontend/bin/stableChunkNames.test.ts
index b7b29ef24ab1..1071f9ee5f1d 100644
--- a/frontend/bin/stableChunkNames.test.ts
+++ b/frontend/bin/stableChunkNames.test.ts
@@ -1,8 +1,9 @@
+import { spawnSync } from 'node:child_process'
import { mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import path from 'node:path'
-import { loggedOutSpecifiers, planStableChunks, stableFileName, writeStableChunks } from './stableChunkNames.mjs'
+import { loggedOutSpecifiers, planStableChunks, shiftMappingsForPrelude, stableFileName } from './stableChunkNames.mjs'
// Two chunks: an entry that imports a shared chunk, the way esbuild writes them with publicPath /static.
const OUTPUTS = {
@@ -10,7 +11,7 @@ const OUTPUTS = {
'dist/chunk-BBBB2222.js': { inputs: { 'src/lib/shared.ts': {}, 'src/lib/other.ts': {} } },
}
-function plan(sources: Record<string, string>): Map<string, { stableFile: string; source: string; mapValid: boolean }> {
+function plan(sources: Record<string, string>): Map<string, { source: string; mapValid: boolean }> {
return planStableChunks(OUTPUTS, (outputPath: string) => sources[outputPath]).plan
}
@@ -35,11 +36,11 @@ describe('planStableChunks', () => {
entry('export const a=1', 'renderAgain()'),
false,
],
- ])('%s', (_name, before, after, importerKeepsName) => {
+ ])('%s', (_name, before, after, importerKeepsSource) => {
const [first, second] = [plan(before), plan(after)]
- expect(
- first.get('dist/Scene-AAAA1111.js')!.stableFile === second.get('dist/Scene-AAAA1111.js')!.stableFile
- ).toBe(importerKeepsName)
+ expect(first.get('dist/Scene-AAAA1111.js')!.source === second.get('dist/Scene-AAAA1111.js')!.source).toBe(
+ importerKeepsSource
+ )
})
it('rewrites chunk imports to identity specifiers and leaves URLs alone', () => {
@@ -108,8 +109,15 @@ describe('planStableChunks', () => {
})
})
+ // Pins the one-line shift so a regression here doesn't only show up as misattributed stack traces.
+ describe('shiftMappingsForPrelude', () => {
+ it('adds one leading empty generated line to the mappings', () => {
+ expect(shiftMappingsForPrelude('AAAA')).toBe(';AAAA')
+ })
+ })
+
// The copied source map is shifted down exactly one line, so the prelude must be a line of its own.
- it('puts a prelude on its own first line, keyed by the chunk specifier, and names the chunk by it', () => {
+ it('puts a prelude on its own first line, keyed by the chunk specifier', () => {
const sources = entry('export const a=1')
const withPrelude = planStableChunks(
OUTPUTS,
@@ -119,7 +127,6 @@ describe('planStableChunks', () => {
).plan.get('dist/Scene-AAAA1111.js')!
expect(withPrelude.source.split('\n')[0]).toMatch(/^register\("@c\/e[0-9A-F]{10}"\);$/)
- expect(withPrelude.stableFile).not.toBe(plan(sources).get('dist/Scene-AAAA1111.js')!.stableFile)
})
it('imports a chunk with a prelude dynamically through the stylesheet-aware loader, and statically as is', () => {
@@ -144,45 +151,130 @@ describe('planStableChunks', () => {
})
describe('writeStableChunks', () => {
- const ENTRY_OUTPUTS = {
- 'dist/entry-AAAA1111.js': { entryPoint: 'src/entry.tsx', inputs: { 'src/entry.tsx': {} } },
+ interface Written {
+ files: Record<string, string>
+ stableFileOf: (outputPath: string) => string
}
- function writeAndReadMap(prelude: string | null): unknown {
- const absWorkingDir = mkdtempSync(path.join(tmpdir(), 'stable-chunks-'))
- try {
- const distDir = path.join(absWorkingDir, 'dist')
- mkdirSync(distDir, { recursive: true })
- writeFileSync(path.join(distDir, 'entry-AAAA1111.js'), 'console.log(1)')
- writeFileSync(
- path.join(distDir, 'entry-AAAA1111.js.map'),
- JSON.stringify({ version: 3, mappings: 'AAAA' })
- )
-
- writeStableChunks({
+ // esbuild refuses to load under jsdom, so the writer runs in a node process of its own.
+ function runWriter(
+ absWorkingDir: string,
+ outputs: Record<string, object>,
+ preludes: Record<string, string>
+ ): any {
+ const script = `
+ const { writeStableChunks } = await import(${JSON.stringify(path.join(__dirname, 'stableChunkNames.mjs'))})
+ const [absWorkingDir, outputs, preludes] = JSON.parse(process.argv[1])
+ const { manifest } = await writeStableChunks({
absWorkingDir,
- outputs: ENTRY_OUTPUTS,
+ outputs,
chunks: {},
entrypoints: [],
preloadManifest: undefined,
- preludes: prelude ? new Map([['dist/entry-AAAA1111.js', () => prelude]]) : new Map(),
+ preludes: new Map(Object.entries(preludes).map(([file, prelude]) => [file, () => prelude])),
+ target: 'es2020',
})
+ process.stdout.write(JSON.stringify(manifest))
+ `
+ const result = spawnSync(
+ 'node',
+ ['--input-type=module', '-e', script, JSON.stringify([absWorkingDir, outputs, preludes])],
+ { encoding: 'utf8' }
+ )
+ if (result.status !== 0) {
+ throw new Error(result.stderr)
+ }
+ return JSON.parse(result.stdout)
+ }
+
+ function write(
+ outputs: Record<string, object>,
+ sources: Record<string, string>,
+ preludes: Record<string, string> = {}
+ ): Written {
+ const absWorkingDir = mkdtempSync(path.join(tmpdir(), 'stable-chunks-'))
+ try {
+ const distDir = path.join(absWorkingDir, 'dist')
+ mkdirSync(distDir, { recursive: true })
+ for (const [outputPath, source] of Object.entries(sources)) {
+ writeFileSync(path.join(absWorkingDir, outputPath), source)
+ }
- const stableMapFile = readdirSync(distDir).find(
- (file) => file.startsWith('entry-S') && file.endsWith('.map')
- )!
- return JSON.parse(readFileSync(path.join(distDir, stableMapFile), 'utf8')).mappings
+ const manifest = runWriter(absWorkingDir, outputs, preludes)
+
+ const files = Object.fromEntries(
+ readdirSync(distDir).map((file) => [file, readFileSync(path.join(distDir, file), 'utf8')])
+ )
+ const { plan: written } = planStableChunks(outputs, (outputPath: string) => sources[outputPath])
+ const stableFileOf = (outputPath: string): string =>
+ manifest.imports[`@c/${written.get(outputPath)!.identity}`].replace(/^static\//, '')
+ return { files, stableFileOf }
} finally {
rmSync(absWorkingDir, { recursive: true, force: true })
}
}
- // Pins the map shift so a regression here doesn't only show up as misattributed stack traces.
+ // Minified while bundling, an unchanged library chunk got new bytes, and a new name, on most deploys.
+ it('keeps a chunk stable name when only a chunk it imports changes', () => {
+ const library = 'const someLongLibraryName=function(){return 1};export{someLongLibraryName as a};'
+ const sources = (render: string): Record<string, string> => ({
+ 'dist/Scene-AAAA1111.js': `import{a as libraryValue}from"/static/chunk-BBBB2222.js";${render}`,
+ 'dist/chunk-BBBB2222.js': library,
+ })
+
+ const before = write(OUTPUTS, sources('console.log(libraryValue())'))
+ const after = write(OUTPUTS, sources('console.log(libraryValue(),2)'))
+
+ const libraryFile = before.stableFileOf('dist/chunk-BBBB2222.js')
+ expect(after.stableFileOf('dist/chunk-BBBB2222.js')).toBe(libraryFile)
+ expect(after.stableFileOf('dist/Scene-AAAA1111.js')).not.toBe(before.stableFileOf('dist/Scene-AAAA1111.js'))
+ expect(before.files[libraryFile]).not.toContain('someLongLibraryName')
+ })
+
+ // The prelude becomes part of the bytes a chunk's stable name hashes, so adding one must
+ // give the chunk a new name, the same as any other change to its own code.
+ it('gives a chunk a new stable name when a prelude is added', () => {
+ const outputs = {
+ 'dist/Lazy-DDDD4444.js': { entryPoint: 'src/scenes/Lazy.tsx', inputs: { 'src/scenes/Lazy.tsx': {} } },
+ }
+ const sources = { 'dist/Lazy-DDDD4444.js': 'console.log(1)' }
+
+ const noPrelude = write(outputs, sources)
+ const withPrelude = write(outputs, sources, { 'dist/Lazy-DDDD4444.js': 'register();' })
+
+ expect(withPrelude.stableFileOf('dist/Lazy-DDDD4444.js')).not.toBe(
+ noPrelude.stableFileOf('dist/Lazy-DDDD4444.js')
+ )
+ })
+
+ // A wrong map only shows up later, as stack traces that point at the wrong code.
it.each([
- ['a prelude chunk', 'register();', ';AAAA'],
- ['a chunk with no prelude', null, 'AAAA'],
- ])('shifts the written source map by one line for %s', (_name, prelude, expectedMappings) => {
- expect(writeAndReadMap(prelude)).toBe(expectedMappings)
+ ['a prelude chunk', { 'dist/entry-AAAA1111.js': 'register();' }],
+ ['a chunk with no prelude', {}],
+ ])('points the source map of %s back at the original source', (_name, preludes) => {
+ const outputs = {
+ 'dist/entry-AAAA1111.js': { entryPoint: 'src/entry.tsx', inputs: { 'src/entry.tsx': {} } },
+ }
+ const { files, stableFileOf } = write(
+ outputs,
+ {
+ 'dist/entry-AAAA1111.js': 'console.log(1)\n//# sourceMappingURL=entry-AAAA1111.js.map\n',
+ 'dist/entry-AAAA1111.js.map': JSON.stringify({
+ version: 3,
+ sources: ['../src/entry.tsx'],
+ sourcesContent: ['console.log(1)'],
+ names: [],
+ mappings: 'AAAA',
+ }),
+ },
+ preludes
+ )
+
+ const stableFile = stableFileOf('dist/entry-AAAA1111.js')
+ expect(files[stableFile]).toContain(`//# sourceMappingURL=${stableFile}.map`)
+ const map = JSON.parse(files[`${stableFile}.map`])
+ expect(map.sources).toEqual(['../src/entry.tsx'])
+ expect(map.sourcesContent).toEqual(['console.log(1)'])
})
})
diff --git a/frontend/build.mjs b/frontend/build.mjs
index c60f4b3c383a..4cb98c0de6fa 100755
--- a/frontend/build.mjs
+++ b/frontend/build.mjs
@@ -13,8 +13,10 @@ import {
createHashlessEntrypoints,
isDev,
reportTopChunks,
+ runExclusiveHeavy,
startDevServer,
} from '@posthog/esbuilder'
+import { minifySeparately } from '@posthog/esbuilder/minify.mjs'
import { AUTHENTICATED_SHELL, ENTRY } from './bin/bootEntries.mjs'
import { LOGGED_OUT_SCENES, moduleKey, sceneModules } from './bin/loggedOutScenes.mjs'
@@ -69,6 +71,8 @@ await buildInParallel(
outdir: path.resolve(__dirname, 'dist'),
heavy: true,
...appBundle,
+ // minifyAppChunks minifies the identifiers of each chunk on its own, after the build.
+ ...(isDev ? {} : { minify: false, minifyWhitespace: true, minifySyntax: true }),
},
...WORKER_ENTRIES.map(({ name, entryPoint, outfileName, define }) => ({
name,
@@ -127,43 +131,8 @@ await buildInParallel(
console.error('Could not get entrypoint for bundle "PostHog App."')
throw new Error('Could not get entrypoint for bundle "PostHog App."')
}
- let stable = null
- if (!isDev) {
- reportTopChunks(buildResponse.outputs, { label: 'PostHog App chunks' })
- const preloadManifest = writePreloadManifest(buildResponse.outputs)
- // A throw here must fail the build: it reaches buildInParallel's catch, which
- // exits non-zero for non-dev builds. Keep it in this awaited call chain.
- // The stable build also splits the app's CSS: see bin/stableCss.mjs.
- const cssPlan = planCssGroups(buildResponse)
- const cssFiles = await buildCssGroups(__dirname, cssPlan.groups)
- stable = writeStableChunks({
- absWorkingDir: __dirname,
- outputs: buildResponse.outputs,
- chunks,
- entrypoints,
- preloadManifest,
- preludes: new Map(
- [...cssPlan.lazyGroupsByEntry].map(([file, groups]) => [
- file,
- (specifier) => cssPrelude(specifier, groups, cssPlan.rankOfGroup),
- ])
- ),
- extraImports: Object.fromEntries(
- [...cssFiles].map(([group, file]) => [`${CSS_SPECIFIER_PREFIX}${group}`, `static/${file}`])
- ),
- eagerCss: cssPlan.eager.map((group) => cssFiles.get(group)),
- loggedOut: {
- ...loggedOutImportMapScope(),
- cssSpecifiersByOutput: new Map(
- [...cssPlan.lazyGroupsByEntry].map(([file, groups]) => [
- file,
- groups.map((group) => `${CSS_SPECIFIER_PREFIX}${group}`),
- ])
- ),
- },
- })
- removeUnlinkedStylesheets(__dirname, buildResponse.outputs, 'src/index.tsx')
- }
+ // onBuildComplete runs outside the heavy-build lock, and this work runs esbuild over every chunk.
+ const stable = isDev ? null : await runExclusiveHeavy(() => finishAppBuild(buildResponse))
writeIndexHtml(chunks, entrypoints, stable)
}
@@ -192,6 +161,101 @@ await buildInParallel(
}
)
+async function finishAppBuild(buildResponse) {
+ const { chunks, entrypoints } = buildResponse
+ const unminifiedDir = await minifyAppChunks(buildResponse)
+ reportTopChunks(buildResponse.outputs, { label: 'PostHog App chunks' })
+ const preloadManifest = writePreloadManifest(buildResponse.outputs)
+ // A throw here must fail the build: it reaches buildInParallel's catch, which
+ // exits non-zero for non-dev builds. Keep it in this awaited call chain.
+ // The stable build also splits the app's CSS: see bin/stableCss.mjs.
+ const cssPlan = planCssGroups(buildResponse)
+ const cssFiles = await buildCssGroups(__dirname, cssPlan.groups)
+ const stable = await writeStableChunks({
+ absWorkingDir: __dirname,
+ sourceDir: unminifiedDir,
+ target: commonConfig.target,
+ outputs: buildResponse.outputs,
+ chunks,
+ entrypoints,
+ preloadManifest,
+ preludes: new Map(
+ [...cssPlan.lazyGroupsByEntry].map(([file, groups]) => [
+ file,
+ (specifier) => cssPrelude(specifier, groups, cssPlan.rankOfGroup),
+ ])
+ ),
+ extraImports: Object.fromEntries(
+ [...cssFiles].map(([group, file]) => [`${CSS_SPECIFIER_PREFIX}${group}`, `static/${file}`])
+ ),
+ eagerCss: cssPlan.eager.map((group) => cssFiles.get(group)),
+ loggedOut: {
+ ...loggedOutImportMapScope(),
+ cssSpecifiersByOutput: new Map(
+ [...cssPlan.lazyGroupsByEntry].map(([file, groups]) => [
+ file,
+ groups.map((group) => `${CSS_SPECIFIER_PREFIX}${group}`),
+ ])
+ ),
+ },
+ })
+ fs.rmSync(unminifiedDir, { recursive: true, force: true })
+ removeUnlinkedStylesheets(__dirname, buildResponse.outputs, 'src/index.tsx')
+ return stable
+}
+
+/**
+ * Minifies every app chunk on its own, in place (see minifySeparately). Keeps the unminified chunks and
+ * their maps in the returned directory for the stable copies, and corrects the output sizes that the
+ * preload manifest and the eager graph check read.
+ *
+ * This rewrites each chunk's bytes after esbuild names it, so the default (non-stable) chunk file
+ * name is no longer a hash of the bytes it serves.
+ */
+async function minifyAppChunks(buildResponse) {
+ const distDir = path.resolve(__dirname, 'dist')
+ const unminifiedDir = path.resolve(__dirname, '.unminified-chunks')
+ fs.rmSync(unminifiedDir, { recursive: true, force: true })
+ fs.mkdirSync(unminifiedDir)
+ const files = Object.keys(buildResponse.outputs)
+ .filter((outputPath) => outputPath.endsWith('.js'))
+ .map((outputPath) => path.relative(distDir, path.resolve(__dirname, outputPath)))
+ const hasMap = (file) => fs.existsSync(path.resolve(distDir, `${file}.map`))
+ for (const file of files) {
+ fs.mkdirSync(path.dirname(path.resolve(unminifiedDir, file)), { recursive: true })
+ fs.renameSync(path.resolve(distDir, file), path.resolve(unminifiedDir, file))
+ if (hasMap(file)) {
+ fs.copyFileSync(path.resolve(distDir, `${file}.map`), path.resolve(unminifiedDir, `${file}.map`))
+ }
+ }
+
+ const minified = await minifySeparately(
+ files.map((file) => path.resolve(unminifiedDir, file)),
+ { inputDir: unminifiedDir, outdir: distDir, target: commonConfig.target }
+ )
+ for (const file of files) {
+ const { code, map } = minified.get(path.resolve(unminifiedDir, file))
+ const withMap = hasMap(file)
+ const contents = withMap ? `${code}//# sourceMappingURL=${path.basename(file)}.map\n` : code
+ fs.writeFileSync(path.resolve(distDir, file), contents)
+ if (withMap) {
+ fs.writeFileSync(path.resolve(distDir, `${file}.map`), map)
+ }
+ // esbuild only knows the unminified sizes. Scale each module's share to the minified chunk.
+ const output = buildResponse.outputs[`dist/${file}`]
+ const bytes = Buffer.byteLength(contents)
+ for (const input of Object.values(output.inputs || {})) {
+ input.bytesInOutput = Math.round((input.bytesInOutput * bytes) / output.bytes)
+ }
+ output.bytes = bytes
+ }
+ fs.writeFileSync(
+ path.resolve(__dirname, 'posthog-app-esbuild-meta.json'),
+ JSON.stringify({ inputs: buildResponse.inputs, outputs: buildResponse.outputs })
+ )
+ return unminifiedDir
+}
+
/** Which modules a logged-out page loads, for the logged-out import map. See loggedOutSpecifiers. */
function loggedOutImportMapScope() {
const scenes = sceneModules(__dirname)