Skip to content

Commit

Permalink
Update cloudflare with hono/context-storage external build fix (#911)
Browse files Browse the repository at this point in the history
re: #882 (comment)
Applies the same fix used for handling hono/context-storage with aws
builds.

---------

Co-authored-by: daishi <[email protected]>
  • Loading branch information
rmarscher and dai-shi authored Sep 28, 2024
1 parent 18a827b commit 09289da
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 40 deletions.
11 changes: 3 additions & 8 deletions packages/waku/src/lib/plugins/vite-plugin-deploy-aws-lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
19 changes: 8 additions & 11 deletions packages/waku/src/lib/plugins/vite-plugin-deploy-cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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') ||
Expand All @@ -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}`) {
Expand Down
10 changes: 5 additions & 5 deletions packages/waku/src/lib/plugins/vite-plugin-deploy-deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') ||
Expand All @@ -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}`) {
Expand Down
20 changes: 12 additions & 8 deletions packages/waku/src/lib/plugins/vite-plugin-deploy-netlify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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__;
Expand Down Expand Up @@ -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}`) {
Expand Down
20 changes: 12 additions & 8 deletions packages/waku/src/lib/plugins/vite-plugin-deploy-vercel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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}`) {
Expand Down
1 change: 1 addition & 0 deletions packages/waku/src/unstable_hono.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 09289da

Please sign in to comment.