From b19debd0022de6bae60b17f257068cbe977f33d8 Mon Sep 17 00:00:00 2001 From: daishi Date: Fri, 30 Aug 2024 14:29:26 +0900 Subject: [PATCH 1/5] introduce unstable_getPlatformObject --- packages/waku/src/lib/builder/build.ts | 5 +++++ packages/waku/src/server.ts | 25 ++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/packages/waku/src/lib/builder/build.ts b/packages/waku/src/lib/builder/build.ts index 8f83b5198..a29648f2d 100644 --- a/packages/waku/src/lib/builder/build.ts +++ b/packages/waku/src/lib/builder/build.ts @@ -6,6 +6,7 @@ import viteReact from '@vitejs/plugin-react'; import type { LoggingFunction, RollupLog } from 'rollup'; import type { Config } from '../../config.js'; +import { unstable_getPlatformObject } from '../../server.js'; import type { BuildConfig, EntriesPrd } from '../../server.js'; import type { ResolvedConfig } from '../config.js'; import { resolveConfig, EXTENSIONS } from '../config.js'; @@ -695,6 +696,10 @@ export async function build(options: { options.deploy !== 'partykit' && options.deploy !== 'deno'; + const platformObject = unstable_getPlatformObject(); + platformObject.buildOptions ||= {}; + platformObject.buildOptions.deploy = options.deploy; + const { clientEntryFiles, serverEntryFiles, serverModuleFiles } = await analyzeEntries(rootDir, config); const serverBuildOutput = await buildServerBundle( diff --git a/packages/waku/src/server.ts b/packages/waku/src/server.ts index 243e9357f..176393a69 100644 --- a/packages/waku/src/server.ts +++ b/packages/waku/src/server.ts @@ -64,7 +64,7 @@ export type EntriesPrd = EntriesDev & { publicIndexHtml: string; }; -let serverEnv: Record = {}; +let serverEnv: Readonly> = {}; /** * This is an internal function and not for public use. @@ -149,3 +149,26 @@ export function unstable_getHeaders(): Record { string >; } + +type PlatformObject = { + buildData?: unknown; // must be JSON serializable + buildOptions?: { + deploy?: + | 'vercel-static' + | 'vercel-serverless' + | 'netlify-static' + | 'netlify-functions' + | 'cloudflare' + | 'partykit' + | 'deno' + | 'aws-lambda' + | undefined; + }; +} & Record; + +const platformObject: PlatformObject = {}; + +// TODO tentative name +export function unstable_getPlatformObject(): PlatformObject { + return platformObject; +} From 947419017da495000d36112857530a2c681b13e5 Mon Sep 17 00:00:00 2001 From: daishi Date: Fri, 30 Aug 2024 15:33:04 +0900 Subject: [PATCH 2/5] wip: new build custom data in router --- packages/waku/src/lib/builder/build.ts | 8 +- .../waku/src/lib/renderers/rsc-renderer.ts | 27 ++-- packages/waku/src/router/create-pages.ts | 35 +---- packages/waku/src/router/define-router.ts | 51 +++---- packages/waku/src/router/fs-router.ts | 137 +++++++++--------- packages/waku/src/server.ts | 7 +- 6 files changed, 116 insertions(+), 149 deletions(-) diff --git a/packages/waku/src/lib/builder/build.ts b/packages/waku/src/lib/builder/build.ts index a29648f2d..561fbc8ba 100644 --- a/packages/waku/src/lib/builder/build.ts +++ b/packages/waku/src/lib/builder/build.ts @@ -745,10 +745,6 @@ export async function build(options: { { env, config }, { entries: distEntries }, ); - await appendFile( - distEntriesFile, - `export const buildConfig = ${JSON.stringify(buildConfig)};`, - ); const { getClientModules } = await emitRscFiles( rootDir, env, @@ -766,6 +762,10 @@ export async function build(options: { getClientModules, clientBuildOutput, ); + await appendFile( + distEntriesFile, + `export const buildData = ${JSON.stringify(platformObject.buildData)};`, + ); if (options.deploy?.startsWith('vercel-')) { await emitVercelOutput( diff --git a/packages/waku/src/lib/renderers/rsc-renderer.ts b/packages/waku/src/lib/renderers/rsc-renderer.ts index d5ffea1a6..d31a9cae8 100644 --- a/packages/waku/src/lib/renderers/rsc-renderer.ts +++ b/packages/waku/src/lib/renderers/rsc-renderer.ts @@ -1,6 +1,7 @@ import type { ReactNode } from 'react'; import type { default as RSDWServerType } from 'react-server-dom-webpack/server.edge'; +import { unstable_getPlatformObject } from '../../server.js'; import type { EntriesDev, EntriesPrd, @@ -70,9 +71,9 @@ export async function renderRsc( const { default: { renderEntries }, loadModule, - buildConfig, + buildData, } = entries as - | (EntriesDev & { loadModule: never; buildConfig: never }) + | (EntriesDev & { loadModule: never; buildData: never }) | EntriesPrd; const loadServerModule = (key: keyof typeof SERVER_MODULE_MAP) => @@ -94,6 +95,9 @@ export async function renderRsc( ]); setAllEnvInternal(env); + if (buildData) { + unstable_getPlatformObject().buildData = buildData; + } const clientBundlerConfig = new Proxy( {}, @@ -132,10 +136,7 @@ export async function renderRsc( }, }; return runWithRenderStoreInternal(renderStore, async () => { - const elements = await renderEntries(input, { - params, - buildConfig, - }); + const elements = await renderEntries(input, { params }); if (elements === null) { const err = new Error('No function component found'); (err as any).statusCode = 404; // HACK our convention for NotFound @@ -167,7 +168,7 @@ export async function renderRsc( } elementsPromise = Promise.all([ elementsPromise, - renderEntries(input, { params, buildConfig }), + renderEntries(input, { params }), ]).then(([oldElements, newElements]) => ({ ...oldElements, // FIXME we should actually check if newElements is null and send an error @@ -327,9 +328,9 @@ export async function getSsrConfig( const { default: { getSsrConfig }, loadModule, - buildConfig, + buildData, } = entries as - | (EntriesDev & { loadModule: never; buildConfig: never }) + | (EntriesDev & { loadModule: never; buildData: never }) | EntriesPrd; const loadServerModule = (key: keyof typeof SERVER_MODULE_MAP) => @@ -351,11 +352,11 @@ export async function getSsrConfig( ]); setAllEnvInternal(env); + if (buildData) { + unstable_getPlatformObject().buildData = buildData; + } - const ssrConfig = await getSsrConfig?.(pathname, { - searchParams, - buildConfig, - }); + const ssrConfig = await getSsrConfig?.(pathname, { searchParams }); if (!ssrConfig) { return null; } diff --git a/packages/waku/src/router/create-pages.ts b/packages/waku/src/router/create-pages.ts index 9b7732bfb..848310975 100644 --- a/packages/waku/src/router/create-pages.ts +++ b/packages/waku/src/router/create-pages.ts @@ -9,7 +9,6 @@ import { getPathMapping, path2regexp, } from '../lib/utils/path.js'; -import type { BuildConfig } from '../server.js'; import type { PathSpec } from '../lib/utils/path.js'; const hasPathSpecPrefix = (prefix: PathSpec, path: PathSpec) => { @@ -182,16 +181,10 @@ export type CreateLayout = (layout: { }) => void; export function createPages( - fn: ( - fns: { - createPage: CreatePage; - createLayout: CreateLayout; - unstable_setBuildData: (path: string, data: unknown) => void; - }, - opts: { - unstable_buildConfig: BuildConfig | undefined; - }, - ) => Promise, + fn: (fns: { + createPage: CreatePage; + createLayout: CreateLayout; + }) => Promise, ) { let configured = false; @@ -211,7 +204,6 @@ export function createPages( >(); const staticComponentMap = new Map>(); const noSsrSet = new WeakSet(); - const buildDataMap = new Map(); const registerStaticComponent = ( id: string, @@ -325,17 +317,10 @@ export function createPages( } }; - const unstable_setBuildData = (path: string, data: unknown) => { - buildDataMap.set(path, data); - }; - let ready: Promise | undefined; - const configure = async (buildConfig?: BuildConfig) => { + const configure = async () => { if (!configured && !ready) { - ready = fn( - { createPage, createLayout, unstable_setBuildData }, - { unstable_buildConfig: buildConfig }, - ); + ready = fn({ createPage, createLayout }); await ready; configured = true; } @@ -350,7 +335,6 @@ export function createPages( path: PathSpec; isStatic: boolean; noSsr: boolean; - data: unknown; }[] = []; for (const [path, pathSpec] of staticPathSet) { const noSsr = noSsrSet.has(pathSpec); @@ -368,7 +352,6 @@ export function createPages( path: pathSpec, isStatic, noSsr, - data: buildDataMap.get(path), }); } for (const [path, [pathSpec]] of dynamicPagePathMap) { @@ -378,7 +361,6 @@ export function createPages( path: pathSpec, isStatic: false, noSsr, - data: buildDataMap.get(path), }); } for (const [path, [pathSpec]] of wildcardPagePathMap) { @@ -388,13 +370,12 @@ export function createPages( path: pathSpec, isStatic: false, noSsr, - data: buildDataMap.get(path), }); } return paths; }, - async (id, { unstable_setShouldSkip, unstable_buildConfig }) => { - await configure(unstable_buildConfig); + async (id, { unstable_setShouldSkip }) => { + await configure(); const staticComponent = staticComponentMap.get(id); if (staticComponent) { unstable_setShouldSkip([]); diff --git a/packages/waku/src/router/define-router.ts b/packages/waku/src/router/define-router.ts index e0263f40d..3fe5150d1 100644 --- a/packages/waku/src/router/define-router.ts +++ b/packages/waku/src/router/define-router.ts @@ -1,7 +1,11 @@ import { createElement } from 'react'; import type { ComponentProps, FunctionComponent, ReactNode } from 'react'; -import { defineEntries, rerender } from '../server.js'; +import { + defineEntries, + rerender, + unstable_getPlatformObject, +} from '../server.js'; import type { BuildConfig, RenderEntries, @@ -48,7 +52,6 @@ export function unstable_defineRouter( path: PathSpec; isStatic?: boolean; noSsr?: boolean; - data?: unknown; // For build: put in customData }> >, getComponent: ( @@ -56,7 +59,6 @@ export function unstable_defineRouter( options: { // TODO setShouldSkip API is too hard to understand unstable_setShouldSkip: (val?: ShouldSkipValue) => void; - unstable_buildConfig: BuildConfig | undefined; }, ) => Promise< | FunctionComponent @@ -64,18 +66,18 @@ export function unstable_defineRouter( | null >, ): ReturnType { + const platformObject = unstable_getPlatformObject(); type MyPathConfig = { pattern: string; pathname: PathSpec; isStatic?: boolean | undefined; - customData: { noSsr?: boolean; is404: boolean; data: unknown }; + specs: { noSsr?: boolean; is404: boolean }; }[]; let cachedPathConfig: MyPathConfig | undefined; - const getMyPathConfig = async ( - buildConfig?: BuildConfig, - ): Promise => { - if (buildConfig) { - return buildConfig as MyPathConfig; + const getMyPathConfig = async (): Promise => { + const pathConfig = platformObject.buildData?.defineRouterPathConfigs; + if (pathConfig) { + return pathConfig as MyPathConfig; } if (!cachedPathConfig) { cachedPathConfig = Array.from(await getPathConfig()).map((item) => { @@ -87,7 +89,7 @@ export function unstable_defineRouter( pattern: item.pattern, pathname: item.path, isStatic: item.isStatic, - customData: { is404, noSsr: !!item.noSsr, data: item.data }, + specs: { is404, noSsr: !!item.noSsr }, }; }); } @@ -95,26 +97,22 @@ export function unstable_defineRouter( }; const existsPath = async ( pathname: string, - buildConfig: BuildConfig | undefined, ): Promise<['FOUND', 'NO_SSR'?] | ['NOT_FOUND', 'HAS_404'?]> => { - const pathConfig = await getMyPathConfig(buildConfig); + const pathConfig = await getMyPathConfig(); const found = pathConfig.find(({ pathname: pathSpec }) => getPathMapping(pathSpec, pathname), ); return found - ? found.customData.noSsr + ? found.specs.noSsr ? ['FOUND', 'NO_SSR'] : ['FOUND'] - : pathConfig.some(({ customData: { is404 } }) => is404) // FIXMEs should avoid re-computation + : pathConfig.some(({ specs: { is404 } }) => is404) // FIXMEs should avoid re-computation ? ['NOT_FOUND', 'HAS_404'] : ['NOT_FOUND']; }; - const renderEntries: RenderEntries = async ( - input, - { params, buildConfig }, - ) => { + const renderEntries: RenderEntries = async (input, { params }) => { const pathname = parseInputString(input); - if ((await existsPath(pathname, buildConfig))[0] === 'NOT_FOUND') { + if ((await existsPath(pathname))[0] === 'NOT_FOUND') { return null; } const shouldSkipObj: { @@ -144,7 +142,6 @@ export function unstable_defineRouter( }; const component = await getComponent(id, { unstable_setShouldSkip: setShoudSkip, - unstable_buildConfig: buildConfig, }); if (!component) { return []; @@ -196,7 +193,7 @@ globalThis.__WAKU_ROUTER_PREFETCH__ = (path) => { } };`; const buildConfig: BuildConfig = []; - for (const { pathname: pathSpec, isStatic, customData } of pathConfig) { + for (const { pathname: pathSpec, isStatic, specs } of pathConfig) { const entries: BuildConfig[number]['entries'] = []; if (pathSpec.every(({ type }) => type === 'literal')) { const pathname = '/' + pathSpec.map(({ name }) => name).join('/'); @@ -209,18 +206,16 @@ globalThis.__WAKU_ROUTER_PREFETCH__ = (path) => { entries, customCode: customCode + - (customData.is404 ? 'globalThis.__WAKU_ROUTER_404__ = true;' : ''), - customData, + (specs.is404 ? 'globalThis.__WAKU_ROUTER_404__ = true;' : ''), }); } + platformObject.buildData ||= {}; + platformObject.buildData.defineRouterPathConfigs = buildConfig; return buildConfig; }; - const getSsrConfig: GetSsrConfig = async ( - pathname, - { searchParams, buildConfig }, - ) => { - const pathStatus = await existsPath(pathname, buildConfig); + const getSsrConfig: GetSsrConfig = async (pathname, { searchParams }) => { + const pathStatus = await existsPath(pathname); if (pathStatus[1] === 'NO_SSR') { return null; } diff --git a/packages/waku/src/router/fs-router.ts b/packages/waku/src/router/fs-router.ts index c6498009b..28025f00c 100644 --- a/packages/waku/src/router/fs-router.ts +++ b/packages/waku/src/router/fs-router.ts @@ -1,3 +1,4 @@ +import { unstable_getPlatformObject } from '../server.js'; import { createPages } from './create-pages.js'; import { EXTENSIONS } from '../lib/config.js'; @@ -9,83 +10,75 @@ export function fsRouter( loadPage: (file: string) => Promise | undefined, pages = 'pages', ) { - return createPages( - async ( - { createPage, createLayout, unstable_setBuildData }, - { unstable_buildConfig }, - ) => { - let files: string[]; - if (unstable_buildConfig) { - // TODO FIXME this is toooooooo naive - files = (unstable_buildConfig[0]!.customData as any).data; - } else { - // dev and build only - const [ - { readdir }, - { join, dirname, extname, sep }, - { fileURLToPath }, - ] = await Promise.all([ + const platformObject = unstable_getPlatformObject(); + return createPages(async ({ createPage, createLayout }) => { + let files: string[] | undefined = platformObject.buildData + ?.fsRouterFiles as string[] | undefined; + if (!files) { + // dev and build only + const [{ readdir }, { join, dirname, extname, sep }, { fileURLToPath }] = + await Promise.all([ import(/* @vite-ignore */ DO_NOT_BUNDLE + 'node:fs/promises'), import(/* @vite-ignore */ DO_NOT_BUNDLE + 'node:path'), import(/* @vite-ignore */ DO_NOT_BUNDLE + 'node:url'), ]); - const pagesDir = join(dirname(fileURLToPath(importMetaUrl)), pages); - files = await readdir(pagesDir, { - encoding: 'utf8', - recursive: true, - }); - files = files.flatMap((file) => { - const myExt = extname(file); - const myExtIndex = EXTENSIONS.indexOf(myExt); - if (myExtIndex === -1) { - return []; - } - // HACK: replace "_slug_" to "[slug]" for build - file = file.replace(/(?<=^|\/|\\)_([^/]+)_(?=\/|\\|\.)/g, '[$1]'); - // For Windows - file = sep === '/' ? file : file.replace(/\\/g, '/'); - // HACK: resolve different extensions for build - const exts = [myExt, ...EXTENSIONS]; - exts.splice(myExtIndex + 1, 1); // remove the second myExt - for (const ext of exts) { - const f = file.slice(0, -myExt.length) + ext; - if (loadPage(f)) { - return [f]; - } + const pagesDir = join(dirname(fileURLToPath(importMetaUrl)), pages); + files = await readdir(pagesDir, { + encoding: 'utf8', + recursive: true, + }); + files = files!.flatMap((file) => { + const myExt = extname(file); + const myExtIndex = EXTENSIONS.indexOf(myExt); + if (myExtIndex === -1) { + return []; + } + // HACK: replace "_slug_" to "[slug]" for build + file = file.replace(/(?<=^|\/|\\)_([^/]+)_(?=\/|\\|\.)/g, '[$1]'); + // For Windows + file = sep === '/' ? file : file.replace(/\\/g, '/'); + // HACK: resolve different extensions for build + const exts = [myExt, ...EXTENSIONS]; + exts.splice(myExtIndex + 1, 1); // remove the second myExt + for (const ext of exts) { + const f = file.slice(0, -myExt.length) + ext; + if (loadPage(f)) { + return [f]; } - throw new Error('Failed to resolve ' + file); - }); - } - for (const file of files) { - const mod = await loadPage(file); - const config = await mod.getConfig?.(); - const pathItems = file - .replace(/\.\w+$/, '') - .split('/') - .filter(Boolean); - const path = - '/' + - (['_layout', 'index'].includes(pathItems.at(-1)!) - ? pathItems.slice(0, -1) - : pathItems - ).join('/'); - unstable_setBuildData(path, files); // FIXME toooooo naive, not efficient - if (pathItems.at(-1) === '_layout') { - createLayout({ - path, - component: mod.default, - render: 'static', - ...config, - }); - } else { - createPage({ - path, - component: mod.default, - render: 'dynamic', - ...config, - }); } + throw new Error('Failed to resolve ' + file); + }); + } + platformObject.buildData ||= {}; + platformObject.buildData.fsRouterFiles = files; + for (const file of files) { + const mod = await loadPage(file); + const config = await mod.getConfig?.(); + const pathItems = file + .replace(/\.\w+$/, '') + .split('/') + .filter(Boolean); + const path = + '/' + + (['_layout', 'index'].includes(pathItems.at(-1)!) + ? pathItems.slice(0, -1) + : pathItems + ).join('/'); + if (pathItems.at(-1) === '_layout') { + createLayout({ + path, + component: mod.default, + render: 'static', + ...config, + }); + } else { + createPage({ + path, + component: mod.default, + render: 'dynamic', + ...config, + }); } - }, - ); + } + }); } diff --git a/packages/waku/src/server.ts b/packages/waku/src/server.ts index 176393a69..38fd0ab10 100644 --- a/packages/waku/src/server.ts +++ b/packages/waku/src/server.ts @@ -17,14 +17,12 @@ export type BuildConfig = { }[]; context?: Record; customCode?: string; // optional code to inject TODO hope to remove this - customData?: unknown; // should be serializable with JSON.stringify }[]; export type RenderEntries = ( input: string, options: { params: unknown | undefined; - buildConfig: BuildConfig | undefined; }, ) => Promise; @@ -36,7 +34,6 @@ export type GetSsrConfig = ( pathname: string, options: { searchParams: URLSearchParams; - buildConfig?: BuildConfig | undefined; }, ) => Promise<{ input: string; @@ -59,9 +56,9 @@ export type EntriesDev = { export type EntriesPrd = EntriesDev & { loadConfig: () => Promise; loadModule: (id: string) => Promise; - buildConfig?: BuildConfig; dynamicHtmlPaths: [pathSpec: PathSpec, htmlHead: string][]; publicIndexHtml: string; + buildData?: Record; // must be JSON serializable }; let serverEnv: Readonly> = {}; @@ -151,7 +148,7 @@ export function unstable_getHeaders(): Record { } type PlatformObject = { - buildData?: unknown; // must be JSON serializable + buildData?: Record; // must be JSON serializable buildOptions?: { deploy?: | 'vercel-static' From abbdefed77cab0eafe35332bfba6daf9f6817136 Mon Sep 17 00:00:00 2001 From: daishi Date: Fri, 30 Aug 2024 19:14:18 +0900 Subject: [PATCH 3/5] fix test --- packages/waku/tests/create-pages.test.ts | 55 ------------------------ 1 file changed, 55 deletions(-) diff --git a/packages/waku/tests/create-pages.test.ts b/packages/waku/tests/create-pages.test.ts index 0df9b1782..ba0ac354f 100644 --- a/packages/waku/tests/create-pages.test.ts +++ b/packages/waku/tests/create-pages.test.ts @@ -240,7 +240,6 @@ describe('createPages', () => { expect( await getComponent!('test/page', { unstable_setShouldSkip: setShouldSkip, - unstable_buildConfig: undefined, }), ).toBe(TestPage); expect(setShouldSkip).toHaveBeenCalledTimes(1); @@ -275,7 +274,6 @@ describe('createPages', () => { expect( await getComponent('test/page', { unstable_setShouldSkip: setShouldSkip, - unstable_buildConfig: undefined, }), ).toBe(TestPage); expect(setShouldSkip).toHaveBeenCalledTimes(1); @@ -318,7 +316,6 @@ describe('createPages', () => { expect( await getComponent('test/page', { unstable_setShouldSkip: setShouldSkip, - unstable_buildConfig: undefined, }), ).toBe(TestPage); expect(setShouldSkip).toHaveBeenCalledTimes(1); @@ -328,7 +325,6 @@ describe('createPages', () => { expect( await getComponent('layout', { unstable_setShouldSkip: setShouldSkipLayout, - unstable_buildConfig: undefined, }), ).toBe(TestLayout); expect(setShouldSkipLayout).toHaveBeenCalledTimes(1); @@ -371,7 +367,6 @@ describe('createPages', () => { expect( await getComponent('test/page', { unstable_setShouldSkip: setShouldSkip, - unstable_buildConfig: undefined, }), ).toBe(TestPage); expect(setShouldSkip).toHaveBeenCalledTimes(1); @@ -381,7 +376,6 @@ describe('createPages', () => { expect( await getComponent('layout', { unstable_setShouldSkip: setShouldSkipLayout, - unstable_buildConfig: undefined, }), ).toBe(TestLayout); expect(setShouldSkipLayout).toHaveBeenCalledTimes(1); @@ -420,7 +414,6 @@ describe('createPages', () => { expect( await getComponent('test/nested/page', { unstable_setShouldSkip: setShouldSkip, - unstable_buildConfig: undefined, }), ).toBe(TestPage); expect(setShouldSkip).toHaveBeenCalledTimes(1); @@ -459,7 +452,6 @@ describe('createPages', () => { expect( await getComponent('test/nested/page', { unstable_setShouldSkip: setShouldSkip, - unstable_buildConfig: undefined, }), ).toBe(TestPage); expect(setShouldSkip).toHaveBeenCalledTimes(1); @@ -525,7 +517,6 @@ describe('createPages', () => { const setShouldSkip = vi.fn(); const WrappedComponent = await getComponent('test/w/x/page', { unstable_setShouldSkip: setShouldSkip, - unstable_buildConfig: undefined, }); assert(WrappedComponent); expect(setShouldSkip).toHaveBeenCalledTimes(1); @@ -570,7 +561,6 @@ describe('createPages', () => { const setShouldSkip = vi.fn(); const WrappedComponent = await getComponent('test/w/x/page', { unstable_setShouldSkip: setShouldSkip, - unstable_buildConfig: undefined, }); assert(WrappedComponent); expect(setShouldSkip).toHaveBeenCalledTimes(1); @@ -616,7 +606,6 @@ describe('createPages', () => { const setShouldSkip = vi.fn(); const WrappedComponent = await getComponent('test/a/b/page', { unstable_setShouldSkip: setShouldSkip, - unstable_buildConfig: undefined, }); assert(WrappedComponent); expect(setShouldSkip).toHaveBeenCalledTimes(1); @@ -657,7 +646,6 @@ describe('createPages', () => { const setShouldSkip = vi.fn(); const WrappedComponent = await getComponent('test/a/b/page', { unstable_setShouldSkip: setShouldSkip, - unstable_buildConfig: undefined, }); assert(WrappedComponent); expect(setShouldSkip).toHaveBeenCalledTimes(1); @@ -727,49 +715,6 @@ describe('createPages', () => { ]); }); - it('allows to inject build data', async () => { - createPages(async ({ createPage, unstable_setBuildData }) => { - unstable_setBuildData('/static', { foo: 'bar' }); - createPage({ - render: 'static', - path: '/static', - component: () => null, - }); - createPage({ - render: 'dynamic', - path: '/dynamic', - component: () => null, - }); - }); - const { getPathConfig } = injectedFunctions(); - expect(await getPathConfig()).toEqual([ - { - data: { foo: 'bar' }, - isStatic: true, - noSsr: false, - path: [ - { - name: 'static', - type: 'literal', - }, - ], - pattern: '^/static$', - }, - { - data: undefined, - isStatic: false, - noSsr: false, - path: [ - { - name: 'dynamic', - type: 'literal', - }, - ], - pattern: '^/dynamic$', - }, - ]); - }); - it('fails if duplicated dynamic paths are registered', async () => { createPages(async ({ createPage }) => { createPage({ From f5303fa8cbe8e5a43346f730922810141a8b9f2d Mon Sep 17 00:00:00 2001 From: daishi Date: Fri, 30 Aug 2024 19:19:16 +0900 Subject: [PATCH 4/5] fix pathConfig in buildData --- packages/waku/src/router/define-router.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/waku/src/router/define-router.ts b/packages/waku/src/router/define-router.ts index 3fe5150d1..fc490c6ec 100644 --- a/packages/waku/src/router/define-router.ts +++ b/packages/waku/src/router/define-router.ts @@ -210,7 +210,7 @@ globalThis.__WAKU_ROUTER_PREFETCH__ = (path) => { }); } platformObject.buildData ||= {}; - platformObject.buildData.defineRouterPathConfigs = buildConfig; + platformObject.buildData.defineRouterPathConfigs = pathConfig; return buildConfig; }; From 3e89ad96f0611df4e440c3864d367a515d97f766 Mon Sep 17 00:00:00 2001 From: daishi Date: Fri, 30 Aug 2024 19:44:21 +0900 Subject: [PATCH 5/5] move to the end --- packages/waku/src/lib/builder/build.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/waku/src/lib/builder/build.ts b/packages/waku/src/lib/builder/build.ts index 561fbc8ba..b809bb51f 100644 --- a/packages/waku/src/lib/builder/build.ts +++ b/packages/waku/src/lib/builder/build.ts @@ -762,10 +762,6 @@ export async function build(options: { getClientModules, clientBuildOutput, ); - await appendFile( - distEntriesFile, - `export const buildData = ${JSON.stringify(platformObject.buildData)};`, - ); if (options.deploy?.startsWith('vercel-')) { await emitVercelOutput( @@ -788,4 +784,9 @@ export async function build(options: { } else if (options.deploy === 'aws-lambda') { await emitAwsLambdaOutput(config); } + + await appendFile( + distEntriesFile, + `export const buildData = ${JSON.stringify(platformObject.buildData)};`, + ); }