diff --git a/packages/start-plugin-core/tests/hydrate-split-modules.test.ts b/packages/start-plugin-core/tests/hydrate-split-modules.test.ts index 58671a853a..cb62a7d1bd 100644 --- a/packages/start-plugin-core/tests/hydrate-split-modules.test.ts +++ b/packages/start-plugin-core/tests/hydrate-split-modules.test.ts @@ -4,14 +4,15 @@ import { compileCode, compileFirstChunk, compileHydrate, + evaluateHydrateParent, evaluateModule, getBoundaryIds, + getChunkComponent, getChunkIds, hydrateParentStubs, importSources, loadChunk, parentImportSources, - parentModuleStubs, renderChunk, } from './regression-helpers' import { declarationOf, getModuleErrors } from './validate-module' @@ -393,7 +394,7 @@ const __jsx = (type, props) => JSON.stringify(props) propsRuntime, ) const module = await evaluateModule(chunks[0]!, {}, propsRuntime) - expect(module.H0({})).toBe(expected.H0()) + expect(getChunkComponent(module)({})).toBe(expected.H0()) }) // Source: Qwik optimizer example_default_export @@ -438,9 +439,9 @@ export function Page() { return

{++renders}

}`, ) - const module = await evaluateModule(chunks[0]!) - expect(module.H0({})).toBe('

1

') - expect(module.H0({})).toBe('

2

') + const component = getChunkComponent(await evaluateModule(chunks[0]!)) + expect(component({})).toBe('

1

') + expect(component({})).toBe('

2

') }) // Source: Qwik optimizer should_keep_non_migrated_binding_from_shared_destructuring_declarator @@ -452,7 +453,7 @@ export function Page() { ])( 'client: an $name destructuring shared by the parent and the children keeps both bindings', async ({ declaration }) => { - const { parent, chunks } = await compileHydrate( + const { parent, chunks, plugin } = await compileHydrate( 'client', `${head}${declaration} export const fromRoot = () => JSON.stringify(b) @@ -460,14 +461,13 @@ export function Page() { return

{a}

}`, ) - const module = await evaluateModule(parent, hydrateParentStubs) + const { module, parentModuleStubs } = await evaluateHydrateParent( + plugin, + parent, + ) expect(module.fromRoot()).toContain('B') expect( - await renderChunk( - chunks[0]!, - {}, - await parentModuleStubs(chunks[0]!, module), - ), + await renderChunk(chunks[0]!, {}, await parentModuleStubs(chunks[0]!)), ).toBe('

A

') }, ) @@ -476,7 +476,7 @@ export function Page() { // example_segment_variable_migration, // variable_migration_transitive_dep_used_by_other_segment test('client: helpers shared by two boundaries and an export stay available to each', async () => { - const { parent, chunks } = await compileHydrate( + const { parent, chunks, plugin } = await compileHydrate( 'client', `${head}const scrollState = (el) => ({ x: el.x, y: el.y }) const saveScroll = (s) => s.x + s.y @@ -487,10 +487,13 @@ export function App() { }`, ) expect(chunks).toHaveLength(2) - const module = await evaluateModule(parent, hydrateParentStubs) + const { module, parentModuleStubs } = await evaluateHydrateParent( + plugin, + parent, + ) expect(module.direct).toBe(2) const render = async (chunk: string) => - renderChunk(chunk, {}, await parentModuleStubs(chunk, module)) + renderChunk(chunk, {}, await parentModuleStubs(chunk)) expect(await render(chunks[0]!)).toBe('

3

') expect(await render(chunks[1]!)).toBe('

4

') }) diff --git a/packages/start-plugin-core/tests/regression-helpers.ts b/packages/start-plugin-core/tests/regression-helpers.ts index ded1cb3e71..dcdc80d79a 100644 --- a/packages/start-plugin-core/tests/regression-helpers.ts +++ b/packages/start-plugin-core/tests/regression-helpers.ts @@ -346,24 +346,48 @@ export async function compileFirstChunk(plugin: HydratePlugin, parent: string) { return { code, serverFns } } -/** The names a split chunk component receives as props, sorted. */ +/** Exported function declarations: name and parameter list. */ +const exportedFunction = + /export\s+(?:default\s+)?function\s*([\w$]*)\s*\(([^)]*)\)/g + +/** + * The names the component of a split chunk receives as props, sorted. The + * component is the generated `H` export, else the only exported + * function. + */ export function getChunkParams(chunk: string) { - const params = chunk.match(/export function H\d+\(([^)]*)\)/)?.[1] ?? '' + const components = [...chunk.matchAll(exportedFunction)] + const component = + components.find(([, name]) => /^H\d+$/.test(name!)) ?? + (components.length === 1 ? components[0] : undefined) + const params = component?.[2] ?? '' return [...params.matchAll(/[\w$]+/g)].map(([name]) => name).sort() } +/** + * The component an evaluated `` chunk exports: the generated + * `H` export, else the only exported function. + */ +export function getChunkComponent(module: Record) { + const functions = Object.keys(module).filter( + (key) => typeof module[key] === 'function', + ) + const name = + functions.find((key) => /^H\d+$/.test(key)) ?? + (functions.length === 1 ? functions[0] : undefined) + if (!name) { + throw new Error('expected the chunk to export a component') + } + return module[name] as (props: Record) => any +} + /** Renders the component a `` chunk exports with the given props. */ export async function renderChunk( chunk: string, props: Record = {}, stubs: Record = {}, ) { - const module = await evaluateModule(chunk, stubs) - const name = Object.keys(module).find((key) => /^H\d+$/.test(key)) - if (!name) { - throw new Error('expected the chunk to export a component') - } - return module[name]!(props) + return getChunkComponent(await evaluateModule(chunk, stubs))(props) } /** Parent-module stubs: `` marks where its children render, lazy chunks render their props. */ @@ -386,31 +410,62 @@ export const hydrateParentStubs = { } /** - * Import sources of a `` chunk that name its parent module: a chunk - * may import the module bindings it shares with the parent instead of - * declaring its own copy. + * Import sources of a `` module that name the parent module, with any + * query or hash: a chunk may import the module bindings it shares with the + * parent instead of declaring its own copy. */ -export function parentImportSources(chunk: string) { - return importSources(chunk).filter((source) => - [moduleId, './module.tsx', './module'].includes(source), +export function parentImportSources(code: string) { + return importSources(code).filter((source) => + [moduleId, './module.tsx', './module'].includes( + source.replace(/[?#].*$/, ''), + ), ) } /** - * Stubs that link the parent imports of `chunk` (if any) to the parent module, - * evaluated with `hydrateParentStubs` unless an evaluated module is given. + * Evaluates a client `` parent and links its modules like a bundler. + * An import of the parent module (`parentImportSources`) resolves to the + * virtual module the Hydrate plugin loads for that id, evaluated once and + * shared by the parent and its chunks, or else to the evaluated parent. + * Returns the parent module and `parentModuleStubs(chunk)`, the stubs that + * link a chunk's parent imports. */ -export async function parentModuleStubs( - chunk: string, - parent: string | Record, -): Promise> { - const sources = parentImportSources(chunk) - if (sources.length === 0) { - return {} +export async function evaluateHydrateParent( + plugin: HydratePlugin, + parent: string, + stubs: Record = hydrateParentStubs, + runtime?: string, +) { + const virtualModules = new Map>>() + const link = async (code: string, parentModule?: Record) => { + const linked: Record = {} + for (const source of parentImportSources(code)) { + const id = `${moduleId}${source.match(/[?#].*$/)?.[0] ?? ''}` + const virtualModule = loadChunk(plugin, 'client', id) + if (virtualModule !== null) { + if (!virtualModules.has(id)) { + const linkedModule = link(virtualModule, parentModule).then( + (imports) => + evaluateModule(virtualModule, { ...stubs, ...imports }, runtime), + ) + virtualModules.set(id, linkedModule) + } + linked[source] = await virtualModules.get(id)! + } else if (parentModule) { + linked[source] = parentModule + } else { + throw new Error(`Cannot link ${source} before the parent is evaluated`) + } + } + return linked + } + const module: Record = await evaluateModule( + parent, + { ...stubs, ...(await link(parent)) }, + runtime, + ) + return { + module, + parentModuleStubs: (chunk: string) => link(chunk, module), } - const module = - typeof parent === 'string' - ? await evaluateModule(parent, hydrateParentStubs) - : parent - return Object.fromEntries(sources.map((source) => [source, module])) }