From 09289da16378fbe69b3ac592326b2b3572480951 Mon Sep 17 00:00:00 2001 From: Rob Marscher Date: Fri, 27 Sep 2024 23:06:33 -0400 Subject: [PATCH] Update cloudflare with hono/context-storage external build fix (#911) re: https://github.com/dai-shi/waku/pull/882#issuecomment-2378365279 Applies the same fix used for handling hono/context-storage with aws builds. --------- Co-authored-by: daishi --- .../plugins/vite-plugin-deploy-aws-lambda.ts | 11 +++------- .../plugins/vite-plugin-deploy-cloudflare.ts | 19 ++++++++---------- .../lib/plugins/vite-plugin-deploy-deno.ts | 10 +++++----- .../lib/plugins/vite-plugin-deploy-netlify.ts | 20 +++++++++++-------- .../lib/plugins/vite-plugin-deploy-vercel.ts | 20 +++++++++++-------- packages/waku/src/unstable_hono.ts | 1 + 6 files changed, 41 insertions(+), 40 deletions(-) diff --git a/packages/waku/src/lib/plugins/vite-plugin-deploy-aws-lambda.ts b/packages/waku/src/lib/plugins/vite-plugin-deploy-aws-lambda.ts index 473239b02..3d7eb779e 100644 --- a/packages/waku/src/lib/plugins/vite-plugin-deploy-aws-lambda.ts +++ b/packages/waku/src/lib/plugins/vite-plugin-deploy-aws-lambda.ts @@ -17,24 +17,19 @@ const getServeJsContent = ( ) => ` import path from 'node:path'; import { existsSync, readFileSync } from 'node:fs'; -import { runner, importHono, importHonoNodeServerServeStatic, importHonoAwsLambda } from 'waku/unstable_hono'; +import { runner, importHono, importHonoContextStorage, importHonoNodeServerServeStatic, importHonoAwsLambda } from 'waku/unstable_hono'; const { Hono } = await importHono(); +const { contextStorage } = await importHonoContextStorage(); const { serveStatic } = await importHonoNodeServerServeStatic(); const { ${lambdaStreaming ? 'streamHandle:' : ''}handle } = await importHonoAwsLambda(); -let contextStorage; -try { - ({ contextStorage } = await import('hono/context-storage')); -} catch {} const distDir = '${distDir}'; const publicDir = '${distPublic}'; const loadEntries = () => import('${srcEntriesFile}'); const app = new Hono(); -if (contextStorage) { - app.use(contextStorage()); -} +app.use(contextStorage()); app.use('*', serveStatic({ root: distDir + '/' + publicDir })); app.use('*', runner({ cmd: 'start', loadEntries, env: process.env })); app.notFound(async (c) => { diff --git a/packages/waku/src/lib/plugins/vite-plugin-deploy-cloudflare.ts b/packages/waku/src/lib/plugins/vite-plugin-deploy-cloudflare.ts index 8ef6a153c..077a6c201 100644 --- a/packages/waku/src/lib/plugins/vite-plugin-deploy-cloudflare.ts +++ b/packages/waku/src/lib/plugins/vite-plugin-deploy-cloudflare.ts @@ -17,21 +17,16 @@ import { DIST_ENTRIES_JS, DIST_PUBLIC } from '../builder/constants.js'; const SERVE_JS = 'serve-cloudflare.js'; const getServeJsContent = (srcEntriesFile: string) => ` -import { runner, importHono } from 'waku/unstable_hono'; +import { runner, importHono, importHonoContextStorage } from 'waku/unstable_hono'; const { Hono } = await importHono(); -let contextStorage; -try { - ({ contextStorage } = await import('hono/context-storage')); -} catch {} +const { contextStorage } = await importHonoContextStorage(); const loadEntries = () => import('${srcEntriesFile}'); let serveWaku; const app = new Hono(); -if (contextStorage) { - app.use(contextStorage()); -} +app.use(contextStorage()); app.use('*', (c, next) => serveWaku(c, next)); app.notFound(async (c) => { const assetsFetcher = c.env.ASSETS; @@ -100,6 +95,11 @@ export function deployCloudflarePlugin(opts: { rootDir = config.root; entriesFile = `${rootDir}/${opts.srcDir}/${SRC_ENTRIES}`; const { deploy, unstable_phase } = platformObject.buildOptions || {}; + if (deploy === 'cloudflare' && Array.isArray(config.ssr.external)) { + config.ssr.external = config.ssr.external.filter( + (item) => item !== 'hono/context-storage', + ); + } if ( (unstable_phase !== 'buildServerBundle' && unstable_phase !== 'buildSsrBundle') || @@ -118,9 +118,6 @@ export function deployCloudflarePlugin(opts: { if (source === `${opts.srcDir}/${SERVE_JS}`) { return source; } - if (source === 'hono/context-storage') { - return { id: source, external: true }; - } }, load(id) { if (id === `${opts.srcDir}/${SERVE_JS}`) { diff --git a/packages/waku/src/lib/plugins/vite-plugin-deploy-deno.ts b/packages/waku/src/lib/plugins/vite-plugin-deploy-deno.ts index c032dc641..e103346cb 100644 --- a/packages/waku/src/lib/plugins/vite-plugin-deploy-deno.ts +++ b/packages/waku/src/lib/plugins/vite-plugin-deploy-deno.ts @@ -61,6 +61,11 @@ export function deployDenoPlugin(opts: { configResolved(config) { entriesFile = `${config.root}/${opts.srcDir}/${SRC_ENTRIES}`; const { deploy, unstable_phase } = platformObject.buildOptions || {}; + if (deploy === 'deno' && Array.isArray(config.ssr.external)) { + config.ssr.external = config.ssr.external.filter( + (item) => item !== 'hono/context-storage', + ); + } if ( (unstable_phase !== 'buildServerBundle' && unstable_phase !== 'buildSsrBundle') || @@ -74,11 +79,6 @@ export function deployDenoPlugin(opts: { config.ssr.resolve.conditions.push('worker'); config.ssr.resolve.externalConditions ||= []; config.ssr.resolve.externalConditions.push('worker'); - if (Array.isArray(config.ssr.external)) { - config.ssr.external = config.ssr.external.filter( - (item) => item !== 'hono/context-storage', - ); - } }, resolveId(source) { if (source === `${opts.srcDir}/${SERVE_JS}`) { diff --git a/packages/waku/src/lib/plugins/vite-plugin-deploy-netlify.ts b/packages/waku/src/lib/plugins/vite-plugin-deploy-netlify.ts index 3ef427d16..18c1b7237 100644 --- a/packages/waku/src/lib/plugins/vite-plugin-deploy-netlify.ts +++ b/packages/waku/src/lib/plugins/vite-plugin-deploy-netlify.ts @@ -9,20 +9,15 @@ import { DIST_PUBLIC } from '../builder/constants.js'; const SERVE_JS = 'serve-netlify.js'; const getServeJsContent = (srcEntriesFile: string) => ` -import { runner, importHono } from 'waku/unstable_hono'; +import { runner, importHono, importHonoContextStorage } from 'waku/unstable_hono'; const { Hono } = await importHono(); -let contextStorage; -try { - ({ contextStorage } = await import('hono/context-storage')); -} catch {} +const { contextStorage } = await importHonoContextStorage(); const loadEntries = () => import('${srcEntriesFile}'); const app = new Hono(); -if (contextStorage) { - app.use(contextStorage()); -} +app.use(contextStorage()); app.use('*', runner({ cmd: 'start', loadEntries, env: process.env })); app.notFound((c) => { const notFoundHtml = globalThis.__WAKU_NOT_FOUND_HTML__; @@ -61,6 +56,15 @@ export function deployNetlifyPlugin(opts: { configResolved(config) { rootDir = config.root; entriesFile = `${rootDir}/${opts.srcDir}/${SRC_ENTRIES}`; + const { deploy } = platformObject.buildOptions || {}; + if ( + deploy === 'netlify-functions' && + Array.isArray(config.ssr.external) + ) { + config.ssr.external = config.ssr.external.filter( + (item) => item !== 'hono/context-storage', + ); + } }, resolveId(source) { if (source === `${opts.srcDir}/${SERVE_JS}`) { diff --git a/packages/waku/src/lib/plugins/vite-plugin-deploy-vercel.ts b/packages/waku/src/lib/plugins/vite-plugin-deploy-vercel.ts index 0005ebfc8..1ebef3d87 100644 --- a/packages/waku/src/lib/plugins/vite-plugin-deploy-vercel.ts +++ b/packages/waku/src/lib/plugins/vite-plugin-deploy-vercel.ts @@ -15,23 +15,18 @@ const getServeJsContent = ( ) => ` import path from 'node:path'; import { existsSync, readFileSync } from 'node:fs'; -import { runner, importHono, importHonoNodeServer } from 'waku/unstable_hono'; +import { runner, importHono, importHonoContextStorage, importHonoNodeServer } from 'waku/unstable_hono'; const { Hono } = await importHono(); +const { contextStorage } = await importHonoContextStorage(); const { getRequestListener } = await importHonoNodeServer(); -let contextStorage; -try { - ({ contextStorage } = await import('hono/context-storage')); -} catch {} const distDir = '${distDir}'; const publicDir = '${distPublic}'; const loadEntries = () => import('${srcEntriesFile}'); const app = new Hono(); -if (contextStorage) { - app.use(contextStorage()); -} +app.use(contextStorage()); app.use('*', runner({ cmd: 'start', loadEntries, env: process.env })); app.notFound((c) => { // FIXME better implementation using node stream? @@ -73,6 +68,15 @@ export function deployVercelPlugin(opts: { configResolved(config) { rootDir = config.root; entriesFile = `${rootDir}/${opts.srcDir}/${SRC_ENTRIES}`; + const { deploy } = platformObject.buildOptions || {}; + if ( + deploy === 'vercel-serverless' && + Array.isArray(config.ssr.external) + ) { + config.ssr.external = config.ssr.external.filter( + (item) => item !== 'hono/context-storage', + ); + } }, resolveId(source) { if (source === `${opts.srcDir}/${SERVE_JS}`) { diff --git a/packages/waku/src/unstable_hono.ts b/packages/waku/src/unstable_hono.ts index a6c1e8c14..1663b81f8 100644 --- a/packages/waku/src/unstable_hono.ts +++ b/packages/waku/src/unstable_hono.ts @@ -3,6 +3,7 @@ export { runner } from './lib/hono/runner.js'; export const importHono = () => import('hono'); +export const importHonoContextStorage = () => import('hono/context-storage'); export const importHonoNodeServer: any = () => import('@hono/node-server'); export const importHonoNodeServerServeStatic = () => import('@hono/node-server/serve-static');