diff --git a/packages/router-utils/src/compiler/README.md b/packages/router-utils/src/compiler/README.md index d161113e1e..abcf219bff 100644 --- a/packages/router-utils/src/compiler/README.md +++ b/packages/router-utils/src/compiler/README.md @@ -240,7 +240,7 @@ text compiles waits for it and emits its own output from the result. take: a file of the same name with another extension, or of a directory a resolution went into; a resolution that found an unlinked file is stale without asking) and returns `{ evicted, dependents }`, which the host hard-invalidates in the bundler. A summary change invalidates an importer - only when what it read changed: the digest of an export it looked up (`ModuleSummary.exportKeys`; an absent name + only when what it read changed: the digest of an export it looked up (`exportKeyOf`; an absent name appearing counts), the `export *` list a lookup walked, or the whole export-facts digest for a listing. An export added to a widely imported module re-plans none of its importers. A published plan keeps one `exports` record per module it looked names up in: the names and their keys, or none when the module exports none of them (a barrel's diff --git a/packages/router-utils/src/compiler/cache.ts b/packages/router-utils/src/compiler/cache.ts index ac25594c3f..6b6e4f8146 100644 --- a/packages/router-utils/src/compiler/cache.ts +++ b/packages/router-utils/src/compiler/cache.ts @@ -4,7 +4,7 @@ * validation, and the dependents index. Only plain data is retained. */ import { pathOfId, planKeyString } from './identity' -import { ownEntry } from './strings' +import { exportKeyOf, summaryDigest } from './summary' import { approxBytes, cacheDisabled } from './stats' import type { OutputId } from './rewrite' import type { PlanKey } from './identity' @@ -18,7 +18,7 @@ export type DependencyFingerprint = | { kind: 'summary'; canonicalId: string; digest: string | null } /** * The exports a plan read of one module, one record however many names it - * read: per name, its `ModuleSummary.exportKeys` entry (null: the module + * read: per name, its `exportKeyOf` answer (null: the module * has no such export); `keys` is null when the module has none of them (a * star target a lookup passed through). `stars`: the module's `export *` * list, when a lookup of a name it does not export itself went through it. @@ -118,17 +118,17 @@ export async function fingerprintCurrent( src: FingerprintSource, ): Promise { switch (fp.kind) { - case 'summary': - return ((await src.summary(fp.canonicalId))?.digest ?? null) === fp.digest + case 'summary': { + const summary = await src.summary(fp.canonicalId) + return (summary ? summaryDigest(summary) : null) === fp.digest + } case 'exports': { const summary = await src.summary(fp.canonicalId) const stars = fp.stars return ( summary !== null && fp.names.every( - (n, i) => - (ownEntry(summary.exportKeys, n) ?? null) === - (fp.keys?.[i] ?? null), + (n, i) => exportKeyOf(summary, n) === (fp.keys?.[i] ?? null), ) && (stars === undefined || (summary.stars.length === stars.length && @@ -308,7 +308,7 @@ class DependentsIndex { export class DependencyLedger { readonly entries: Array = [] private readonly byId = new Map() - /** What lookups read of a module: name → `exportKeys` entry (null: no such export), and its `export *` list if they walked it. */ + /** What lookups read of a module: name → `exportKeyOf` answer (null: no such export), and its `export *` list if they walked it. */ private readonly exportReads = new Map() /** The dependents-index buckets of `entries` and `exportReads`. */ private readonly indexKeys = new Set() @@ -329,7 +329,7 @@ export class DependencyLedger { this.indexPath(fingerprintIndexKey(fp)) } - /** A lookup read export `name` of a module (`key`: its `exportKeys` entry, null when it has none). */ + /** A lookup read export `name` of a module (`key`: its `exportKeyOf` answer, null when it has none). */ recordExport(canonicalId: string, name: string, key: string | null): void { const { keys } = this.readsOf(canonicalId) if (!keys.has(name)) { diff --git a/packages/router-utils/src/compiler/facts.ts b/packages/router-utils/src/compiler/facts.ts index 3444ebe2de..af0cf889f8 100644 --- a/packages/router-utils/src/compiler/facts.ts +++ b/packages/router-utils/src/compiler/facts.ts @@ -476,8 +476,9 @@ export class RawFactsCache { summary: { exports: exportFacts, stars: [], - exportKeys: exportKeysOf(exportFacts), - digest, + marker: null, + exportKeyMemo: exportKeysOf(exportFacts), + digestMemo: digest, }, targetFacts: { ...tf, digest: digestOf(tf) }, } diff --git a/packages/router-utils/src/compiler/pipeline.ts b/packages/router-utils/src/compiler/pipeline.ts index 2937085c02..96bb2b7937 100644 --- a/packages/router-utils/src/compiler/pipeline.ts +++ b/packages/router-utils/src/compiler/pipeline.ts @@ -30,7 +30,7 @@ import { } from './host' import { isVirtualId } from './facts' import { registerCompilerStats } from './stats' -import { ownEntry } from './strings' +import { exportKeyOf, summaryDigest } from './summary' import { SELF_CHECKS, invariantWarning } from './self-checks' import { verifyPlan } from './verify' import type { CacheEntry, Settled } from './cache' @@ -499,7 +499,7 @@ async function runAttempt( ledger.record({ kind: 'summary', canonicalId, - digest: facts.summary?.digest ?? null, + digest: facts.summary ? summaryDigest(facts.summary) : null, }) return facts.summary } @@ -509,11 +509,7 @@ async function runAttempt( if (summary === null) { ledger.record({ kind: 'summary', canonicalId, digest: null }) } else { - ledger.recordExport( - canonicalId, - name, - ownEntry(summary.exportKeys, name) ?? null, - ) + ledger.recordExport(canonicalId, name, exportKeyOf(summary, name)) } return summary } diff --git a/packages/router-utils/src/compiler/summary.ts b/packages/router-utils/src/compiler/summary.ts index 443ffb2ada..ed8986b89c 100644 --- a/packages/router-utils/src/compiler/summary.ts +++ b/packages/router-utils/src/compiler/summary.ts @@ -4,6 +4,7 @@ * what importers can observe. */ import { digestOf } from './identity' +import { ownEntry } from './strings' import { ValueAnalysis, stripSites } from './values' import type { AbstractValue, RecognizerTable } from './values' import type { ModuleAnalysis } from './analysis' @@ -16,25 +17,64 @@ type ExportFact = export interface ModuleSummary { exports: Record stars: Array + marker: ModuleAnalysis['markers'] /** * Per export, the digest of what an importer reading it observes (the * fact, sites stripped): an importer depends on the exports it reads - * only, so another export changing leaves its plan current. + * only, so another export changing leaves its plan current. Filled as + * the exports are read (`exportKeyOf`): a module's importers read few of + * its exports. Read through `exportKeyOf` only: an export not read yet + * has no entry. */ - exportKeys: Record - /** EXPORT-FACTS DIGEST over {exportKeys, stars, marker}: a reader of the whole summary (a listing of its exports). */ - digest: string + readonly exportKeyMemo: Record + /** `summaryDigest`'s answer, once asked (set at build for a known package's synthetic summary). */ + digestMemo?: string } -/** The per-export digests of a summary's exports (`ModuleSummary.exportKeys`). */ +function exportKey(f: ExportFact): string { + return digestOf( + f.kind === 'local' ? { kind: 'local', value: stripSites(f.value) } : f, + ) +} + +/** The key of export `name` (`ModuleSummary.exportKeyMemo`); null when the module has no such export. */ +export function exportKeyOf( + summary: ModuleSummary, + name: string, +): string | null { + const known = ownEntry(summary.exportKeyMemo, name) + if (known !== undefined) { + return known + } + const f = ownEntry(summary.exports, name) + if (f === undefined) { + return null + } + return (summary.exportKeyMemo[name] = exportKey(f)) +} + +/** EXPORT-FACTS DIGEST over {exportKeys, stars, marker}: what a reader of the whole summary (a listing of its exports) depends on. */ +export function summaryDigest(summary: ModuleSummary): string { + if (summary.digestMemo === undefined) { + for (const name of Object.keys(summary.exports)) { + exportKeyOf(summary, name) + } + summary.digestMemo = digestOf({ + exports: summary.exportKeyMemo, + stars: summary.stars, + marker: summary.marker, + }) + } + return summary.digestMemo +} + +/** Every export's key, computed now (a known package's synthetic summary). */ export function exportKeysOf( exports: ModuleSummary['exports'], -): ModuleSummary['exportKeys'] { +): ModuleSummary['exportKeyMemo'] { const keys: Record = Object.create(null) for (const [name, f] of Object.entries(exports)) { - keys[name] = digestOf( - f.kind === 'local' ? { kind: 'local', value: stripSites(f.value) } : f, - ) + keys[name] = exportKey(f) } return keys } @@ -95,16 +135,10 @@ export function buildSummary( break } } - const exportKeys = exportKeysOf(exportFacts) - const digest = digestOf({ - exports: exportKeys, - stars, - marker: analysis.markers, - }) return { exports: exportFacts, stars, - exportKeys, - digest, + marker: analysis.markers, + exportKeyMemo: Object.create(null), } } diff --git a/packages/router-utils/tests/facts.test.ts b/packages/router-utils/tests/facts.test.ts index 6153c650e6..933300faa2 100644 --- a/packages/router-utils/tests/facts.test.ts +++ b/packages/router-utils/tests/facts.test.ts @@ -1,5 +1,6 @@ import { describe, expect, it } from 'vitest' import { RawFactsCache } from '../src/compiler' +import { summaryDigest } from '../src/compiler/summary' import type { RecognizerTable } from '../src/compiler' import type { RawFacts } from '../src/compiler/facts' @@ -592,7 +593,7 @@ describe('facts of a file that stops parsing', () => { cache.markStale('/app/a.ts') const broken = await cache.get('/app/a.ts', 'client') expect(broken.targetFacts.digest).toBe(parsed.targetFacts.digest) - expect(broken.summary?.digest).toBe(parsed.summary?.digest) + expect(summaryDigest(broken.summary!)).toBe(summaryDigest(parsed.summary!)) expect(cache.stats(false)).toMatchObject({ rawFactsStale: 0 }) // parsing again with other exports: the facts follow the file files['/app/a.ts'] = 'export const b = 1\n' diff --git a/packages/router-utils/tests/summary.test.ts b/packages/router-utils/tests/summary.test.ts index 3bffbb1acc..968e6496a9 100644 --- a/packages/router-utils/tests/summary.test.ts +++ b/packages/router-utils/tests/summary.test.ts @@ -1,5 +1,6 @@ import { describe, expect, it } from 'vitest' import { ModuleAnalysis, buildSummary, parseModule } from '../src/compiler' +import { exportKeyOf, summaryDigest } from '../src/compiler/summary' import type { RecognizerTable } from '../src/compiler' const TABLE: RecognizerTable = { @@ -60,13 +61,30 @@ describe('export facts', () => { export const fn = () => 2 export const options = { a: 1 } `) - expect(moved.digest).toBe(base.digest) + expect(summaryDigest(moved)).toBe(summaryDigest(base)) const star = summarise(` export const options = { a: 1 } export const fn = () => 1 export * from './more' `) - expect(star.digest).not.toBe(base.digest) + expect(summaryDigest(star)).not.toBe(summaryDigest(base)) + }) + + it('keys an export when it is read; the digest keys them all', () => { + const code = ` + export const options = { a: 1 } + export const fn = () => 1 + ` + const s = summarise(code) + expect(Object.keys(s.exportKeyMemo)).toEqual([]) + const fn = exportKeyOf(s, 'fn') + expect(Object.keys(s.exportKeyMemo)).toEqual(['fn']) + expect(exportKeyOf(s, 'missing')).toBeNull() + expect(fn).toBe(exportKeyOf(summarise(`// moved\n${code}`), 'fn')) + expect(exportKeyOf(s, 'options')).not.toBe(fn) + const digest = summaryDigest(s) + expect(Object.keys(s.exportKeyMemo).sort()).toEqual(['fn', 'options']) + expect(summaryDigest(summarise(code))).toBe(digest) }) })