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): vercel serve and constants #877

Merged
merged 6 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 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/",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this to test the hono server serving the website from vercel?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! thanks 👍

"website:prd": "pnpm run website:build && (cd packages/website && pnpm start)"
},
"prettier": {
Expand Down
4 changes: 4 additions & 0 deletions packages/waku/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
"types": "./dist/internals.d.ts",
"default": "./dist/internals.js"
},
"./hono": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious here, but why do /hono and /internals need to be in the published package json exports if they are for internal use only?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did it at first (locally without commits), but it feels like mixed. /internals are "more internal" than /hono, so to speak. 😄
In any case, I might be reconsidering it in the future, before finalizing v1.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I noticed we need to re-export Hono to avoid adding in peer dependencies. So, /internals are weird for it.

Copy link
Owner Author

@dai-shi dai-shi Sep 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I misunderstood your question probably.

  • /internals are for someone who might want to experiment something (hypothetical use case).
  • /hono is for deploy plugins (actual use case), and I wish this could be avoided.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this makes more sense! Thanks for the explanation

"types": "./dist/hono.d.ts",
"default": "./dist/hono.js"
},
"./client": {
"types": "./dist/client.d.ts",
"default": "./dist/client.js"
Expand Down
7 changes: 7 additions & 0 deletions packages/waku/src/hono.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// These exports are for internal use only and subject to change without notice.

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';
24 changes: 22 additions & 2 deletions packages/waku/src/internals.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
export { runner as unstable_runner } from './lib/hono/runner.js';
export { build as unstable_build } from './lib/builder/build.js';
// These exports are for internal use only and subject to change without notice.

export { build } from './lib/builder/build.js';

export { deployAwsLambdaPlugin } from './lib/plugins/vite-plugin-deploy-aws-lambda.js';
export { deployCloudflarePlugin } from './lib/plugins/vite-plugin-deploy-cloudflare.js';
export { deployDenoPlugin } from './lib/plugins/vite-plugin-deploy-deno.js';
export { deployNetlifyPlugin } from './lib/plugins/vite-plugin-deploy-netlify.js';
export { deployPartykitPlugin } from './lib/plugins/vite-plugin-deploy-partykit.js';
export { deployVercelPlugin } from './lib/plugins/vite-plugin-deploy-vercel.js';
export { devCommonJsPlugin } from './lib/plugins/vite-plugin-dev-commonjs.js';
export { nonjsResolvePlugin } from './lib/plugins/vite-plugin-nonjs-resolve.js';
export { rscAnalyzePlugin } from './lib/plugins/vite-plugin-rsc-analyze.js';
export { rscDelegatePlugin } from './lib/plugins/vite-plugin-rsc-delegate.js';
export { rscEntriesPlugin } from './lib/plugins/vite-plugin-rsc-entries.js';
export { rscEnvPlugin } from './lib/plugins/vite-plugin-rsc-env.js';
export { rscHmrPlugin } from './lib/plugins/vite-plugin-rsc-hmr.js';
export { rscIndexPlugin } from './lib/plugins/vite-plugin-rsc-index.js';
export { rscManagedPlugin } from './lib/plugins/vite-plugin-rsc-managed.js';
export { rscPrivatePlugin } from './lib/plugins/vite-plugin-rsc-private.js';
export { rscRsdwPlugin } from './lib/plugins/vite-plugin-rsc-rsdw.js';
export { rscTransformPlugin } from './lib/plugins/vite-plugin-rsc-transform.js';
3 changes: 2 additions & 1 deletion packages/waku/src/lib/builder/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ 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';
import { resolveConfig } from '../config.js';
import { EXTENSIONS } from '../constants.js';
import type { PathSpec } from '../utils/path.js';
import {
decodeFilePathFromAbsolute,
Expand Down
28 changes: 0 additions & 28 deletions packages/waku/src/lib/builder/serve-vercel.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/waku/src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,3 @@ export async function resolveConfig(config: Config) {
};
return resolvedConfig;
}

export const EXTENSIONS = ['.js', '.ts', '.tsx', '.jsx', '.mjs', '.cjs'];
3 changes: 3 additions & 0 deletions packages/waku/src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const EXTENSIONS = ['.js', '.ts', '.tsx', '.jsx', '.mjs', '.cjs'];
export const SRC_MAIN = 'main';
export const SRC_ENTRIES = 'entries';
8 changes: 2 additions & 6 deletions packages/waku/src/lib/middleware/dev-server-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import viteReact from '@vitejs/plugin-react';

import type { EntriesDev } from '../../server.js';
import { resolveConfig } from '../config.js';
import { SRC_MAIN, SRC_ENTRIES } from '../constants.js';
import {
joinPath,
fileURLToFilePath,
Expand All @@ -22,12 +23,7 @@ import { rscHmrPlugin, hotUpdate } from '../plugins/vite-plugin-rsc-hmr.js';
import type { HotUpdatePayload } from '../plugins/vite-plugin-rsc-hmr.js';
import { rscEnvPlugin } from '../plugins/vite-plugin-rsc-env.js';
import { rscPrivatePlugin } from '../plugins/vite-plugin-rsc-private.js';
import {
// HACK depending on these constants is not ideal
SRC_ENTRIES,
SRC_MAIN,
rscManagedPlugin,
} from '../plugins/vite-plugin-rsc-managed.js';
import { rscManagedPlugin } from '../plugins/vite-plugin-rsc-managed.js';
import { rscDelegatePlugin } from '../plugins/vite-plugin-rsc-delegate.js';
import { mergeUserViteConfig } from '../utils/merge-vite-config.js';
import type { ClonableModuleNode, Middleware } from './types.js';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ import { existsSync, writeFileSync } from 'node:fs';
import { normalizePath } from 'vite';
import type { Plugin } from 'vite';

// HACK: Depending on a different plugin isn't ideal.
// Maybe we could put in vite config object?
import { SRC_ENTRIES } from './vite-plugin-rsc-managed.js';

import { unstable_getPlatformObject } from '../../server.js';
import { EXTENSIONS } from '../config.js';
import { EXTENSIONS, SRC_ENTRIES } from '../constants.js';
import {
decodeFilePathFromAbsolute,
extname,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@ import {
import { normalizePath } from 'vite';
import type { Plugin } from 'vite';

// HACK: Depending on a different plugin isn't ideal.
// Maybe we could put in vite config object?
import { SRC_ENTRIES } from './vite-plugin-rsc-managed.js';

import { unstable_getPlatformObject } from '../../server.js';
import { EXTENSIONS } from '../config.js';
import { EXTENSIONS, SRC_ENTRIES } from '../constants.js';
import {
decodeFilePathFromAbsolute,
extname,
Expand Down
6 changes: 1 addition & 5 deletions packages/waku/src/lib/plugins/vite-plugin-deploy-deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ import { existsSync } from 'node:fs';
import { normalizePath } from 'vite';
import type { Plugin } from 'vite';

// HACK: Depending on a different plugin isn't ideal.
// Maybe we could put in vite config object?
import { SRC_ENTRIES } from './vite-plugin-rsc-managed.js';

import { unstable_getPlatformObject } from '../../server.js';
import { EXTENSIONS } from '../config.js';
import { EXTENSIONS, SRC_ENTRIES } from '../constants.js';
import {
decodeFilePathFromAbsolute,
extname,
Expand Down
6 changes: 1 addition & 5 deletions packages/waku/src/lib/plugins/vite-plugin-deploy-netlify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { normalizePath } from 'vite';
import type { Plugin } from 'vite';

// HACK: Depending on a different plugin isn't ideal.
// Maybe we could put in vite config object?
import { SRC_ENTRIES } from './vite-plugin-rsc-managed.js';

import { unstable_getPlatformObject } from '../../server.js';
import { EXTENSIONS } from '../config.js';
import { EXTENSIONS, SRC_ENTRIES } from '../constants.js';
import {
decodeFilePathFromAbsolute,
extname,
Expand Down
6 changes: 1 addition & 5 deletions packages/waku/src/lib/plugins/vite-plugin-deploy-partykit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ import { existsSync, writeFileSync } from 'node:fs';
import { normalizePath } from 'vite';
import type { Plugin } from 'vite';

// HACK: Depending on a different plugin isn't ideal.
// Maybe we could put in vite config object?
import { SRC_ENTRIES } from './vite-plugin-rsc-managed.js';

import { unstable_getPlatformObject } from '../../server.js';
import { EXTENSIONS } from '../config.js';
import { EXTENSIONS, SRC_ENTRIES } from '../constants.js';
import {
decodeFilePathFromAbsolute,
extname,
Expand Down
84 changes: 41 additions & 43 deletions packages/waku/src/lib/plugins/vite-plugin-deploy-vercel.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
import path from 'node:path';
import { cpSync, existsSync, mkdirSync, writeFileSync } from 'node:fs';
import { normalizePath } from 'vite';
import type { Plugin } from 'vite';

// HACK: Depending on a different plugin isn't ideal.
// Maybe we could put in vite config object?
import { SRC_ENTRIES } from './vite-plugin-rsc-managed.js';

import { unstable_getPlatformObject } from '../../server.js';
import { EXTENSIONS } from '../config.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 getServeJsContent = (
distDir: string,
distPublic: string,
srcEntriesFile: string,
) => `
import path from 'node:path';
import { existsSync, readFileSync } from 'node:fs';
import { runner, Hono, getRequestListener } from 'waku/hono';

const resolveFileName = (fname: string) => {
for (const ext of EXTENSIONS) {
const resolvedName = fname.slice(0, -extname(fname).length) + ext;
if (existsSync(resolvedName)) {
return resolvedName;
}
const distDir = '${distDir}';
const publicDir = '${distPublic}';
const loadEntries = () => import('${srcEntriesFile}');

const app = new Hono();
app.use('*', runner({ cmd: 'start', loadEntries, env: process.env }));
app.notFound((c) => {
// FIXME better implementation using node stream?
const file = path.join(distDir, publicDir, '404.html');
if (existsSync(file)) {
return c.html(readFileSync(file, 'utf8'), 404);
}
return fname; // returning the default one
};
return c.text('404 Not Found', 404);
});

const srcServeFile = decodeFilePathFromAbsolute(
joinPath(fileURLToFilePath(import.meta.url), '../../builder/serve-vercel.js'),
);
export default getRequestListener(app.fetch);
`;

export function deployVercelPlugin(opts: {
srcDir: string;
Expand All @@ -40,6 +42,8 @@ export function deployVercelPlugin(opts: {
}): Plugin {
const platformObject = unstable_getPlatformObject();
let rootDir: string;
let entriesFile: string;
const serveJs = 'serve-vercel.js';
return {
name: 'deploy-vercel-plugin',
config(viteConfig) {
Expand All @@ -50,30 +54,24 @@ export function deployVercelPlugin(opts: {
) {
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[serveJs.replace(/\.js$/, '')] = `${opts.srcDir}/${serveJs}`;
}
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) {
rootDir = config.root;
entriesFile = `${rootDir}/${opts.srcDir}/${SRC_ENTRIES}`;
},
resolveId(source) {
if (source === `${opts.srcDir}/${serveJs}`) {
return source;
}
},
load(id) {
if (id === `${opts.srcDir}/${serveJs}`) {
return getServeJsContent(opts.distDir, DIST_PUBLIC, entriesFile);
}
},
closeBundle() {
const { deploy, unstable_phase } = platformObject.buildOptions || {};
Expand Down Expand Up @@ -112,7 +110,7 @@ export function deployVercelPlugin(opts: {
}
const vcConfigJson = {
runtime: 'nodejs20.x',
handler: `${opts.distDir}/${DIST_SERVE_JS}`,
handler: `${opts.distDir}/${serveJs}`,
launcherType: 'Nodejs',
};
writeFileSync(
Expand Down
2 changes: 1 addition & 1 deletion packages/waku/src/lib/plugins/vite-plugin-nonjs-resolve.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Plugin } from 'vite';

import { EXTENSIONS } from '../config.js';
import { EXTENSIONS } from '../constants.js';
import { extname } from '../utils/path.js';

export function nonjsResolvePlugin(): Plugin {
Expand Down
2 changes: 1 addition & 1 deletion packages/waku/src/lib/plugins/vite-plugin-rsc-analyze.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Plugin } from 'vite';
import * as swc from '@swc/core';

import { EXTENSIONS } from '../config.js';
import { EXTENSIONS } from '../constants.js';
import { extname } from '../utils/path.js';
import { parseOpts } from '../utils/swc.js';
// HACK: Is it common to depend on another plugin like this?
Expand Down
2 changes: 1 addition & 1 deletion packages/waku/src/lib/plugins/vite-plugin-rsc-delegate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Plugin, ViteDevServer } from 'vite';
import * as swc from '@swc/core';

import { EXTENSIONS } from '../config.js';
import { EXTENSIONS } from '../constants.js';
import { extname } from '../utils/path.js';
import { parseOpts } from '../utils/swc.js';
import type { HotUpdatePayload } from './vite-plugin-rsc-hmr.js';
Expand Down
5 changes: 1 addition & 4 deletions packages/waku/src/lib/plugins/vite-plugin-rsc-entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import path from 'node:path';
import { normalizePath } from 'vite';
import type { Plugin } from 'vite';

// HACK Depending on a different plugin isn't ideal.
// Maybe we could put in vite config object?
import { SRC_ENTRIES } from './vite-plugin-rsc-managed.js';

import { SRC_ENTRIES } from '../constants.js';
import { extname, joinPath } from '../utils/path.js';

const stripExt = (fname: string) => {
Expand Down
4 changes: 1 addition & 3 deletions packages/waku/src/lib/plugins/vite-plugin-rsc-index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { Plugin } from 'vite';

// HACK Depending on a different plugin isn't ideal.
// Maybe we could put in vite config object?
import { SRC_MAIN } from './vite-plugin-rsc-managed.js';
import { SRC_MAIN } from '../constants.js';

export const DEFAULT_HTML_HEAD = `
<meta charset="utf-8" />
Expand Down
5 changes: 1 addition & 4 deletions packages/waku/src/lib/plugins/vite-plugin-rsc-managed.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import type { Plugin } from 'vite';

import { EXTENSIONS } from '../config.js';
import { EXTENSIONS, SRC_MAIN, SRC_ENTRIES } from '../constants.js';
import { extname, joinPath } from '../utils/path.js';

export const SRC_MAIN = 'main';
export const SRC_ENTRIES = 'entries';

const stripExt = (fname: string) => {
const ext = extname(fname);
return ext ? fname.slice(0, -ext.length) : fname;
Expand Down
2 changes: 1 addition & 1 deletion packages/waku/src/lib/plugins/vite-plugin-rsc-transform.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Plugin } from 'vite';
import * as swc from '@swc/core';

import { EXTENSIONS } from '../config.js';
import { EXTENSIONS } from '../constants.js';
import { extname } from '../utils/path.js';
import { parseOpts } from '../utils/swc.js';

Expand Down
Loading
Loading