Skip to content

Commit

Permalink
refactor(build): vercel serve and constants (#877)
Browse files Browse the repository at this point in the history
following #866
  • Loading branch information
dai-shi authored Sep 14, 2024
1 parent 0b5d9f8 commit 52973c8
Show file tree
Hide file tree
Showing 24 changed files with 99 additions and 127 deletions.
10 changes: 7 additions & 3 deletions packages/waku/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@
"types": "./dist/middleware/*.d.ts",
"default": "./dist/middleware/*.js"
},
"./internals": {
"types": "./dist/internals.d.ts",
"default": "./dist/internals.js"
"./unstable_internals": {
"types": "./dist/unstable_internals.d.ts",
"default": "./dist/unstable_internals.js"
},
"./unstable_hono": {
"types": "./dist/unstable_hono.d.ts",
"default": "./dist/unstable_hono.js"
},
"./client": {
"types": "./dist/client.d.ts",
Expand Down
2 changes: 0 additions & 2 deletions packages/waku/src/internals.ts

This file was deleted.

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
85 changes: 42 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,39 @@
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 SERVE_JS = 'serve-vercel.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/unstable_hono';
const distDir = '${distDir}';
const publicDir = '${distPublic}';
const loadEntries = () => import('${srcEntriesFile}');
const resolveFileName = (fname: string) => {
for (const ext of EXTENSIONS) {
const resolvedName = fname.slice(0, -extname(fname).length) + ext;
if (existsSync(resolvedName)) {
return resolvedName;
}
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 +44,7 @@ export function deployVercelPlugin(opts: {
}): Plugin {
const platformObject = unstable_getPlatformObject();
let rootDir: string;
let entriesFile: string;
return {
name: 'deploy-vercel-plugin',
config(viteConfig) {
Expand All @@ -50,30 +55,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[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) {
rootDir = config.root;
entriesFile = `${rootDir}/${opts.srcDir}/${SRC_ENTRIES}`;
},
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);
}
},
closeBundle() {
const { deploy, unstable_phase } = platformObject.buildOptions || {};
Expand Down Expand Up @@ -112,7 +111,7 @@ export function deployVercelPlugin(opts: {
}
const vcConfigJson = {
runtime: 'nodejs20.x',
handler: `${opts.distDir}/${DIST_SERVE_JS}`,
handler: `${opts.distDir}/${SERVE_JS}`,
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
2 changes: 1 addition & 1 deletion packages/waku/src/lib/renderers/html-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { injectRSCPayload } from 'rsc-html-stream/server';

import type * as WakuClientType from '../../client.js';
import type { EntriesPrd } from '../../server.js';
import { SRC_MAIN } from '../constants.js';
import type { ResolvedConfig } from '../config.js';
import { concatUint8Arrays } from '../utils/stream.js';
import {
Expand All @@ -17,7 +18,6 @@ import {
import { encodeInput, hasStatusCode } from './utils.js';

// HACK depending on these constants is not ideal
import { SRC_MAIN } from '../plugins/vite-plugin-rsc-managed.js';
import { DEFAULT_HTML_HEAD } from '../plugins/vite-plugin-rsc-index.js';

export const CLIENT_MODULE_MAP = {
Expand Down
2 changes: 1 addition & 1 deletion packages/waku/src/router/fs-router.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { unstable_getPlatformObject } from '../server.js';
import { createPages } from './create-pages.js';

import { EXTENSIONS } from '../lib/config.js';
import { EXTENSIONS } from '../lib/constants.js';

const DO_NOT_BUNDLE = '';

Expand Down
7 changes: 7 additions & 0 deletions packages/waku/src/unstable_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';
Loading

0 comments on commit 52973c8

Please sign in to comment.