diff --git a/.changepacks/changepack_log_prepass_runtime_package.json b/.changepacks/changepack_log_prepass_runtime_package.json new file mode 100644 index 000000000..05f38d7c2 --- /dev/null +++ b/.changepacks/changepack_log_prepass_runtime_package.json @@ -0,0 +1,8 @@ +{ + "changes": { + "packages/plugin-utils/package.json": "Patch", + "packages/next-plugin/package.json": "Patch" + }, + "note": "Skip `@devup-ui/react` in the passes that run before the bundler: the shared class-slot reservation and the Next production prewarm no longer read, resolve or extract the runtime package's own modules, which hold no styles. Next's Turbopack setup drops from about 40 ms to 15 ms on the benchmark app and each webpack compiler does less work. Webpack per-file builds now number a project's files without the runtime package's modules in front of them, so per-file class prefixes and stylesheet names can be shorter (`devup-ui-0.css` and `a-` instead of `devup-ui-23.css` and `x-`); the styles are unchanged.", + "date": "2026-11-19T00:00:00.000Z" +} diff --git a/packages/next-plugin/src/__tests__/prewarm.test.ts b/packages/next-plugin/src/__tests__/prewarm.test.ts index 0d4d2b9cb..b1f43dfa5 100644 --- a/packages/next-plugin/src/__tests__/prewarm.test.ts +++ b/packages/next-plugin/src/__tests__/prewarm.test.ts @@ -151,6 +151,36 @@ describe('collectProductionPrewarmFiles', () => { ]) }) + it('neither reads nor lists the runtime package, from the app or from another Devup UI package', () => { + // Given the runtime package and a component package importing it. + writePackage('@devup-ui/react', './index.js', { + 'index.js': 'export { Box } from "./Box.js"', + 'Box.js': 'export const Box = 1', + 'stylex.js': 'export const props = 1', + }) + writePackage('@devup-ui/components', './index.js', { + 'index.js': + 'import { Box } from "@devup-ui/react"; import "@devup-ui/react/stylex.js"; export const Button = Box', + }) + // When collecting the extraction set of an app importing both. + const files = collectProductionPrewarmFiles({ + cwd, + graph: makeGraph( + [], + [ + '@devup-ui/react', + '@devup-ui/react/stylex.js', + '@devup-ui/components', + ], + ), + expectedBaseFiles: [], + libPackage: '@devup-ui/react', + include: [], + }) + // Then only the component package is extracted ahead of the bundler. + expect(files).toEqual(['node_modules/@devup-ui/components/index.js']) + }) + it('propagates a genuine accepted-package collection error', () => { // Given a malformed accepted package manifest. writePackage('design-system', './index.js', { 'index.js': '' }) diff --git a/packages/next-plugin/src/prewarm.ts b/packages/next-plugin/src/prewarm.ts index 8564ed492..c78877de1 100644 --- a/packages/next-plugin/src/prewarm.ts +++ b/packages/next-plugin/src/prewarm.ts @@ -3,6 +3,7 @@ import { extname, isAbsolute, relative, resolve } from 'node:path' import { createModuleResolver, + isRuntimePackage, parseImports, SOURCE_FILE_REGEX, type StaticImportGraph, @@ -88,6 +89,7 @@ export function collectProductionPrewarmFiles({ [...specifiers].sort().map((specifier) => ({ importer, specifier })), ) for (const { importer, specifier } of queue) { + if (isRuntimePackage(specifier)) continue const local = specifier.startsWith('.') || isAbsolute(specifier) if (!local) { const packageName = packageNameFromSpecifier(specifier) diff --git a/packages/plugin-utils/src/__tests__/reservation-entries.test.ts b/packages/plugin-utils/src/__tests__/reservation-entries.test.ts index 5e8e4a943..7353693cc 100644 --- a/packages/plugin-utils/src/__tests__/reservation-entries.test.ts +++ b/packages/plugin-utils/src/__tests__/reservation-entries.test.ts @@ -167,3 +167,35 @@ it("stops at a dependency the loaders leave to the bundler, but crawls Devup UI' ]) expect(dependencies).toHaveLength(6) }) + +it('never reads the runtime package, from the app or from another Devup UI package', () => { + // Given + using fs = fixture() + fs.write( + 'src/A.tsx', + `${STYLE}\nimport '@devup-ui/components'; import '@devup-ui/react/stylex'`, + ) + fs.write('node_modules/@devup-ui/react/package.json', '{"main":"index.js"}') + fs.write('node_modules/@devup-ui/react/index.js', STYLE) + fs.write('node_modules/@devup-ui/react/stylex.js', STYLE) + fs.write( + 'node_modules/@devup-ui/components/package.json', + '{"main":"index.js"}', + ) + fs.write('node_modules/@devup-ui/components/index.js', STYLE) + // When + const { inputs, dependencies, packageManifests } = collectReservationInputs({ + roots: ['src'], + cwd: fs.root, + package: '@devup-ui/react', + importAliases: {}, + toId: (path: string) => relative(fs.root, path), + }) + // Then + expect(inputs.map(({ filename }) => filename.replaceAll('\\', '/'))).toEqual([ + 'node_modules/@devup-ui/components/index.js', + 'src/A.tsx', + ]) + expect(dependencies).toHaveLength(2) + expect(packageManifests).toHaveLength(1) +}) diff --git a/packages/plugin-utils/src/__tests__/runtime-package.test.ts b/packages/plugin-utils/src/__tests__/runtime-package.test.ts new file mode 100644 index 000000000..5eb24cab5 --- /dev/null +++ b/packages/plugin-utils/src/__tests__/runtime-package.test.ts @@ -0,0 +1,52 @@ +import { readdirSync, readFileSync } from 'node:fs' +import { join, relative } from 'node:path' + +import { expect, it } from 'bun:test' + +import { RUNTIME_PACKAGE } from '../runtime-package' +import { mergeImportAliases } from '../types' + +const wasm = await import('../../../../bindings/devup-ui-wasm/pkg/index.js') + +const ROOT = join(import.meta.dir, '../../../..') + +function modules(directory: string): string[] { + return readdirSync(directory, { withFileTypes: true }).flatMap((entry) => { + const path = join(directory, entry.name) + if (entry.isDirectory()) + return entry.name === '__tests__' ? [] : modules(path) + return /\.tsx?$/.test(entry.name) && + !/\.(?:test(?:-d)?\.tsx?|d\.ts)$/.test(entry.name) + ? [path] + : [] + }) +} + +it('gets no styles from the modules of the runtime package, which the prepasses skip', () => { + // The prepasses that complete the sheet before the bundler runs neither read + // nor extract the runtime package: that is only right while it has no styles + wasm.resetBuildState() + const files = modules(join(ROOT, 'packages/react/src')) + expect(files.length).toBeGreaterThan(10) + const styled = files.filter((path) => { + const output = wasm.codeExtractWithoutSourceMap( + relative(ROOT, path).replaceAll('\\', '/'), + readFileSync(path, 'utf-8'), + RUNTIME_PACKAGE, + './df', + // One sheet: every style any module adds shows in it + true, + false, + true, + mergeImportAliases(undefined, undefined), + ) + try { + return output.updatedBaseStyle + } finally { + output.free() + } + }) + expect(styled).toEqual([]) + expect(wasm.getCss(null, false)).toBe('') + wasm.resetBuildState() +}) diff --git a/packages/plugin-utils/src/index.ts b/packages/plugin-utils/src/index.ts index 469daa733..d3b110798 100644 --- a/packages/plugin-utils/src/index.ts +++ b/packages/plugin-utils/src/index.ts @@ -76,6 +76,7 @@ export { type ReservationInventory, } from './reservation-discovery' export { reservationGraph } from './reservation-graph' +export { isRuntimePackage, RUNTIME_PACKAGE } from './runtime-package' export { createNodeModulesExcludeRegex, createThemeInterfaceArgs, diff --git a/packages/plugin-utils/src/reservation-discovery.ts b/packages/plugin-utils/src/reservation-discovery.ts index 670aa29c9..07623be44 100644 --- a/packages/plugin-utils/src/reservation-discovery.ts +++ b/packages/plugin-utils/src/reservation-discovery.ts @@ -13,6 +13,7 @@ import { isReservationReadFailure, reservationSource, } from './reservation-source' +import { isRuntimePackage } from './runtime-package' import { createNodeModulesExcludeRegex } from './shared' import type { WasmImportAliases } from './types' @@ -177,6 +178,7 @@ export function collectReservationInputs( overlay?.filename ?? physicalPath, code, )) { + if (isRuntimePackage(specifier)) continue const allowed = allowedPackage(specifier, include) const module = reservationSource( () => resolver(specifier, physicalPath), diff --git a/packages/plugin-utils/src/runtime-package.ts b/packages/plugin-utils/src/runtime-package.ts new file mode 100644 index 000000000..c4e04dcac --- /dev/null +++ b/packages/plugin-utils/src/runtime-package.ts @@ -0,0 +1,13 @@ +/** + * The package whose API the extraction compiles away. Its own modules hold no + * styles (extracting any of them changes nothing), so the passes that complete + * the sheet before the bundler runs neither read nor extract them. + */ +export const RUNTIME_PACKAGE = '@devup-ui/react' + +/** Whether an import specifier names the runtime package or one of its subpaths */ +export function isRuntimePackage(specifier: string): boolean { + return ( + specifier === RUNTIME_PACKAGE || specifier.startsWith(`${RUNTIME_PACKAGE}/`) + ) +}