: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
chore(frontend): stop shipping stylesheets no page links (#105794)
Paul D'Ambraad042f7486f1
diff --git a/frontend/bin/unlinkedStylesheets.mjs b/frontend/bin/unlinkedStylesheets.mjsnew file mode 100644index 000000000000..40d3d216fa57--- /dev/null+++ b/frontend/bin/unlinkedStylesheets.mjs@@ -0,0 +1,47 @@+import fs from 'node:fs'+import path from 'node:path'++const buildState = new Map()+let cleanupDone = false++export function resetModuleState() {+ buildState.clear()+ cleanupDone = false+}++export function removeUnlinkedStylesheets(absWorkingDir, outputs, entryPoint) {+ const entry = Object.entries(outputs).find(+ ([file, output]) => output.entryPoint === entryPoint && file.endsWith('.js')+ )+ const linked = entry?.[1].cssBundle+ if (!linked) {+ throw new Error(`No linked stylesheet found for ${entryPoint}, so no stylesheet was removed.`)+ }++ const buildKey = `${absWorkingDir}:${entryPoint}`+ const linkedPath = path.resolve(absWorkingDir, linked)+ buildState.set(buildKey, { outputs, linkedPath })++ if (buildState.size < 2 || cleanupDone) {+ return 0+ }++ const protectedFiles = new Set(buildState.values().map((state) => state.linkedPath))+ let removedBytes = 0+ for (const { outputs: build } of buildState.values()) {+ for (const [file, output] of Object.entries(build)) {+ if (!file.endsWith('.css')) {+ continue+ }+ const absolute = path.resolve(absWorkingDir, file)+ if (protectedFiles.has(absolute)) {+ continue+ }+ fs.rmSync(absolute, { force: true })+ fs.rmSync(path.resolve(absWorkingDir, `${file}.map`), { force: true })+ removedBytes += output.bytes+ }+ }+ cleanupDone = true+ return removedBytes+}diff --git a/frontend/bin/unlinkedStylesheets.test.ts b/frontend/bin/unlinkedStylesheets.test.tsnew file mode 100644index 000000000000..e03d4c8b9822--- /dev/null+++ b/frontend/bin/unlinkedStylesheets.test.ts@@ -0,0 +1,48 @@+import fs from 'node:fs'+import os from 'node:os'+import path from 'node:path'++import { removeUnlinkedStylesheets, resetModuleState } from './unlinkedStylesheets.mjs'++describe('removeUnlinkedStylesheets', () => {+ let dir: string+ const write = (file: string): void => fs.writeFileSync(path.join(dir, file), 'x')+ const exists = (file: string): boolean => fs.existsSync(path.join(dir, file))++ beforeEach(() => {+ resetModuleState()+ dir = fs.mkdtempSync(path.join(os.tmpdir(), 'unlinked-stylesheets-'))+ fs.mkdirSync(path.join(dir, 'dist'))+ })+ afterEach(() => fs.rmSync(dir, { recursive: true, force: true }))++ it('keeps linked stylesheets when builds complete in either order', () => {+ for (const file of ['index-A.css', 'index-A.css.map', 'Scene-B.css', 'Scene-B.css.map', 'exporter-C.css']) {+ write(`dist/${file}`)+ }+ const app = {+ 'dist/index-A.js': { entryPoint: 'src/index.tsx', cssBundle: 'dist/index-A.css' },+ 'dist/index-A.css': { bytes: 10 },+ 'dist/Scene-B.css': { bytes: 5 },+ }+ const exporter = {+ 'dist/exporter-C.js': { entryPoint: 'src/exporter/index.tsx', cssBundle: 'dist/exporter-C.css' },+ 'dist/exporter-C.css': { bytes: 1 },+ 'dist/index-A.css': { bytes: 10 },+ }++ removeUnlinkedStylesheets(dir, exporter, 'src/exporter/index.tsx')+ removeUnlinkedStylesheets(dir, app, 'src/index.tsx')++ expect(['index-A.css', 'index-A.css.map', 'exporter-C.css'].every((f) => exists(`dist/${f}`))).toBe(true)+ expect(exists('dist/Scene-B.css') || exists('dist/Scene-B.css.map')).toBe(false)+ })++ it('removes nothing when the entry has no linked stylesheet', () => {+ write('dist/Scene-B.css')+ expect(() => removeUnlinkedStylesheets(dir, { 'dist/Scene-B.css': { bytes: 5 } }, 'src/index.tsx')).toThrow(+ 'No linked stylesheet found for src/index.tsx'+ )+ expect(exists('dist/Scene-B.css')).toBe(true)+ })+})diff --git a/frontend/build.mjs b/frontend/build.mjsindex 4b7b676a8521..0be5dd40d2e4 100755--- a/frontend/build.mjs+++ b/frontend/build.mjs@@ -19,6 +19,7 @@ import {import { writeStableChunks } from './bin/stableChunkNames.mjs'import { buildCssGroups } from './bin/stableCss.mjs'import { cssPrelude, CSS_SPECIFIER_PREFIX, planCssGroups } from './bin/stableCssPlan.mjs'+import { removeUnlinkedStylesheets } from './bin/unlinkedStylesheets.mjs'import { finalizeToolbarBuild, getToolbarAppBuildConfig } from './toolbar-config.mjs'import { WORKER_ENTRIES } from './workers.config.mjs'@@ -137,6 +138,7 @@ await buildInParallel(),eagerCss: cssPlan.eager.map((group) => cssFiles.get(group)),})+ removeUnlinkedStylesheets(__dirname, buildResponse.outputs, 'src/index.tsx')}writeIndexHtml(chunks, entrypoints, stable)}@@ -144,6 +146,7 @@ await buildInParallel(if (config.name === 'Exporter') {if (!isDev) {reportTopChunks(buildResponse.outputs, { label: 'Exporter chunks' })+ removeUnlinkedStylesheets(__dirname, buildResponse.outputs, 'src/exporter/index.tsx')}writeExporterHtml(chunks, entrypoints)}