Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(deploy): deno serve #880

Merged
merged 3 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,7 @@ npx partykit dev # or deploy

```
npm run build -- --with-deno
DENO_DEPLOY_TOKEN=... deployctl deploy --project=... --prod dist/serve.js --exclude node_modules
deployctl deploy --prod dist/serve-deno.js --exclude node_modules
```

### AWS Lambda (experimental)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"examples:prd:24_nesting": "NAME=24_nesting pnpm run examples:prd",
"website:dev": "(cd packages/website && pnpm run dev)",
"website:build": "cd packages/website && pnpm run build",
"website:vercel": "pnpm run compile && pnpm run website:build --with-vercel-static && mv packages/website/.vercel/output .vercel/",
"website:vercel": "pnpm run compile && pnpm run website:build && mv packages/website/.vercel/output .vercel/",
"website:prd": "pnpm run website:build && (cd packages/website && pnpm start)"
},
"prettier": {
Expand Down
30 changes: 0 additions & 30 deletions packages/waku/src/lib/builder/serve-deno.ts

This file was deleted.

86 changes: 45 additions & 41 deletions packages/waku/src/lib/plugins/vite-plugin-deploy-deno.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,66 @@
import path from 'node:path';
import { existsSync } from 'node:fs';
import { normalizePath } from 'vite';
import type { Plugin } from 'vite';

import { unstable_getPlatformObject } from '../../server.js';
import { EXTENSIONS, SRC_ENTRIES } from '../constants.js';
import {
decodeFilePathFromAbsolute,
extname,
fileURLToFilePath,
joinPath,
} from '../utils/path.js';
import { DIST_SERVE_JS, DIST_PUBLIC } from '../builder/constants.js';
import { SRC_ENTRIES } from '../constants.js';
import { DIST_PUBLIC } from '../builder/constants.js';

const resolveFileName = (fname: string) => {
for (const ext of EXTENSIONS) {
const resolvedName = fname.slice(0, -extname(fname).length) + ext;
if (existsSync(resolvedName)) {
return resolvedName;
}
const SERVE_JS = 'serve-deno.js';

const getServeJsContent = (
distDir: string,
distPublic: string,
srcEntriesFile: string,
) => `
import { Hono } from 'https://deno.land/x/hono/mod.ts';
import { serveStatic } from 'https://deno.land/x/hono/middleware.ts';
import { runner } from 'waku/unstable_hono';

const distDir = '${distDir}';
const publicDir = '${distPublic}';
const loadEntries = () => import('${srcEntriesFile}');
const env = Deno.env.toObject();

const app = new Hono();
app.use('*', serveStatic({ root: distDir + '/' + publicDir }));
app.use('*', runner({ cmd: 'start', loadEntries, env }));
app.notFound(async (c) => {
const file = distDir + '/' + publicDir + '/404.html';
const info = await Deno.stat(file);
if (info.isFile) {
c.header('Content-Type', 'text/html; charset=utf-8');
return c.body(await Deno.readFile(file), 404);
}
return fname; // returning the default one
};
return c.text('404 Not Found', 404);
});

const srcServeFile = decodeFilePathFromAbsolute(
joinPath(fileURLToFilePath(import.meta.url), '../../builder/serve-deno.js'),
);
Deno.serve(app.fetch);
`;

export function deployDenoPlugin(opts: {
srcDir: string;
distDir: string;
}): Plugin {
const platformObject = unstable_getPlatformObject();
let entriesFile: string;
return {
name: 'deploy-deno-plugin',
config(viteConfig) {
const { deploy, unstable_phase } = platformObject.buildOptions || {};
if (unstable_phase !== 'buildServerBundle' || deploy !== 'deno') {
return;
}

// FIXME This seems too hacky (The use of viteConfig.root, '.', path.resolve and resolveFileName)
const entriesFile = normalizePath(
resolveFileName(
path.resolve(
viteConfig.root || '.',
opts.srcDir,
SRC_ENTRIES + '.jsx',
),
),
);
const { input } = viteConfig.build?.rollupOptions ?? {};
if (input && !(typeof input === 'string') && !(input instanceof Array)) {
input[DIST_SERVE_JS.replace(/\.js$/, '')] = srcServeFile;
input[SERVE_JS.replace(/\.js$/, '')] = `${opts.srcDir}/${SERVE_JS}`;
}
viteConfig.define = {
...viteConfig.define,
'import.meta.env.WAKU_ENTRIES_FILE': JSON.stringify(entriesFile),
'import.meta.env.WAKU_CONFIG_DIST_DIR': JSON.stringify(opts.distDir),
'import.meta.env.WAKU_CONFIG_PUBLIC_DIR': JSON.stringify(DIST_PUBLIC),
};
},
configResolved(config) {
entriesFile = `${config.root}/${opts.srcDir}/${SRC_ENTRIES}`;
const { deploy, unstable_phase } = platformObject.buildOptions || {};
if (
(unstable_phase !== 'buildServerBundle' &&
unstable_phase !== 'buildSsrBundle') ||
deploy !== 'cloudflare'
deploy !== 'deno'
) {
return;
}
Expand All @@ -77,5 +71,15 @@ export function deployDenoPlugin(opts: {
config.ssr.resolve.externalConditions ||= [];
config.ssr.resolve.externalConditions.push('worker');
},
resolveId(source) {
if (source === `${opts.srcDir}/${SERVE_JS}`) {
return source;
}
},
load(id) {
if (id === `${opts.srcDir}/${SERVE_JS}`) {
return getServeJsContent(opts.distDir, DIST_PUBLIC, entriesFile);
}
},
};
}
4 changes: 3 additions & 1 deletion packages/waku/src/lib/plugins/vite-plugin-deploy-netlify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import { DIST_PUBLIC } from '../builder/constants.js';
const SERVE_JS = 'serve-netlify.js';

const getServeJsContent = (srcEntriesFile: string) => `
import { runner, Hono } from 'waku/unstable_hono';
import { runner, importHono } from 'waku/unstable_hono';

const { Hono } = await importHono();

const loadEntries = () => import('${srcEntriesFile}');

Expand Down
5 changes: 4 additions & 1 deletion packages/waku/src/lib/plugins/vite-plugin-deploy-vercel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ const getServeJsContent = (
) => `
import path from 'node:path';
import { existsSync, readFileSync } from 'node:fs';
import { runner, Hono, getRequestListener } from 'waku/unstable_hono';
import { runner, importHono, importHonoNodeServer } from 'waku/unstable_hono';

const { Hono } = await importHono();
const { getRequestListener } = await importHonoNodeServer();

const distDir = '${distDir}';
const publicDir = '${distPublic}';
Expand Down
7 changes: 4 additions & 3 deletions packages/waku/src/unstable_hono.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

export { runner } from './lib/hono/runner.js';

export { Hono } from 'hono';
export { getRequestListener } from '@hono/node-server';
export { serveStatic } from '@hono/node-server/serve-static';
export const importHono = () => import('hono');
export const importHonoNodeServer: any = () => import('@hono/node-server');
export const importHonoNodeServerServeStatic = () =>
import('@hono/node-server/serve-static');
Loading