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

feat: waku/config #368

Merged
merged 6 commits into from
Jan 14, 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
7 changes: 5 additions & 2 deletions packages/waku/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
"types": "./dist/main.d.ts",
"default": "./dist/main.js"
},
"./config": {
"types": "./dist/config.d.ts",
"default": "./dist/config.js"
},
"./prd": {
"types": "./dist/prd.d.ts",
"default": "./dist/prd.js"
Expand Down Expand Up @@ -52,8 +56,7 @@
},
"files": [
"src",
"dist",
"patches"
"dist"
],
"scripts": {
"dev": "swc src -d dist -w",
Expand Down
37 changes: 33 additions & 4 deletions packages/waku/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
#!/usr/bin/env node

import path from 'node:path';
import { existsSync, readFileSync } from 'node:fs';
import { existsSync, readFileSync, writeFileSync, unlinkSync } from 'node:fs';
import { pathToFileURL } from 'node:url';
import { parseArgs } from 'node:util';
import { createRequire } from 'node:module';
import { randomBytes } from 'node:crypto';
import { Hono } from 'hono';
import { serve } from '@hono/node-server';
import { serveStatic } from '@hono/node-server/serve-static';
import * as swc from '@swc/core';

import type { Config } from './config.js';
import { resolveConfig } from './lib/config.js';
import { honoMiddleware as honoDevMiddleware } from './lib/middleware/hono-dev.js';
import { honoMiddleware as honoPrdMiddleware } from './lib/middleware/hono-prd.js';
Expand Down Expand Up @@ -47,6 +50,7 @@ const { values, positionals } = parseArgs({
});

loadEnv();
const config = await loadConfig();

const cmd = positionals[0];

Expand Down Expand Up @@ -80,14 +84,18 @@ if (values.version) {

async function runDev(options: { ssr: boolean }) {
const app = new Hono();
app.use('*', honoDevMiddleware({ ...options, env: process.env as any }));
app.use(
'*',
honoDevMiddleware({ ...options, config, env: process.env as any }),
);
const port = parseInt(process.env.PORT || '3000', 10);
startServer(app, port);
}

async function runBuild(options: { ssr: boolean }) {
await build({
...options,
config,
env: process.env as any,
vercel:
values['with-vercel'] ?? !!process.env.VERCEL
Expand All @@ -101,14 +109,14 @@ async function runBuild(options: { ssr: boolean }) {
}

async function runStart(options: { ssr: boolean }) {
const { distDir, publicDir, entriesJs } = await resolveConfig({});
const { distDir, publicDir, entriesJs } = await resolveConfig(config);
const entries = import(
pathToFileURL(path.resolve(distDir, entriesJs)).toString()
);
const app = new Hono();
app.use(
'*',
honoPrdMiddleware({ ...options, entries, env: process.env as any }),
honoPrdMiddleware({ ...options, config, entries, env: process.env as any }),
);
app.use('*', serveStatic({ root: path.join(distDir, publicDir) }));
const port = parseInt(process.env.PORT || '8080', 10);
Expand Down Expand Up @@ -165,3 +173,24 @@ function loadEnv() {
}
}
}

// TODO is this a good idea?
async function loadConfig(): Promise<Config> {
if (!existsSync('waku.config.ts')) {
return {};
}
const { code } = swc.transformFileSync('waku.config.ts', {
swcrc: false,
jsc: {
parser: { syntax: 'typescript' },
target: 'es2022',
},
});
const temp = path.resolve(`.temp-${randomBytes(8).toString('hex')}.js`);
try {
writeFileSync(temp, code);
return (await import(pathToFileURL(temp).toString())).default;
} finally {
unlinkSync(temp);
}
}
63 changes: 63 additions & 0 deletions packages/waku/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
export interface Config {
/**
* The base path for serve HTTP.
* Defaults to "/".
*/
basePath?: string;
/**
* The source directory relative to root.
* This will be the actual root in the development mode.
* Defaults to "src".
*/
srcDir?: string;
/**
* The dist directory relative to root.
* This will be the actual root in the production mode.
* Defaults to "dist".
*/
distDir?: string;
/**
* The public directory relative to distDir.
* It's different from Vite's build.publicDir config.
* Defaults to "public".
*/
publicDir?: string;
/**
* The assets directory relative to distDir and publicDir.
* Defaults to "assets".
*/
assetsDir?: string;
/**
* The index.html file for any directories.
* Defaults to "index.html".
*/
indexHtml?: string;
/**
* The client main file relative to srcDir.
* Defaults to "main.tsx".
*/
mainJs?: string;
/**
* The entries.js file relative to srcDir or distDir.
* The extension should be `.js`,
* but resolved with `.ts`, `.tsx` and `.jsx` in the development mode.
* Defaults to "entries.js".
*/
entriesJs?: string;
/**
* Prefix for HTTP requests to indicate RSC requests.
* Defaults to "RSC".
*/
rscPath?: string;
/**
* HTML headers to inject.
* Defaults to:
* <meta charset="utf-8" />
* <meta name="viewport" content="width=device-width, initial-scale=1" />
*/
htmlHead?: string;
}

export function defineConfig(config: Config) {
return config;
}
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 @@ -6,8 +6,9 @@ import { build as buildVite, resolveConfig as resolveViteConfig } from 'vite';
import viteReact from '@vitejs/plugin-react';
import type { RollupLog, LoggingFunction } from 'rollup';

import type { Config } from '../../config.js';
import { resolveConfig } from '../config.js';
import type { Config, ResolvedConfig } from '../config.js';
import type { ResolvedConfig } from '../config.js';
import {
joinPath,
extname,
Expand Down
60 changes: 1 addition & 59 deletions packages/waku/src/lib/config.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,4 @@
export interface Config {
/**
* The base path for serve HTTP.
* Defaults to "/".
*/
basePath?: string;
/**
* The source directory relative to root.
* This will be the actual root in the development mode.
* Defaults to "src".
*/
srcDir?: string;
/**
* The dist directory relative to root.
* This will be the actual root in the production mode.
* Defaults to "dist".
*/
distDir?: string;
/**
* The public directory relative to distDir.
* It's different from Vite's build.publicDir config.
* Defaults to "public".
*/
publicDir?: string;
/**
* The assets directory relative to distDir and publicDir.
* Defaults to "assets".
*/
assetsDir?: string;
/**
* The index.html file for any directories.
* Defaults to "index.html".
*/
indexHtml?: string;
/**
* The client main file relative to srcDir.
* Defaults to "main.tsx".
*/
mainJs?: string;
/**
* The entries.js file relative to srcDir or distDir.
* The extension should be `.js`,
* but resolved with `.ts`, `.tsx` and `.jsx` in the development mode.
* Defaults to "entries.js".
*/
entriesJs?: string;
/**
* Prefix for HTTP requests to indicate RSC requests.
* Defaults to "RSC".
*/
rscPath?: string;
/**
* HTML headers to inject.
* Defaults to:
* <meta charset="utf-8" />
* <meta name="viewport" content="width=device-width, initial-scale=1" />
*/
htmlHead?: string;
}
import type { Config } from '../config.js';

type DeepRequired<T> = T extends (...args: any[]) => any
? T
Expand Down
2 changes: 1 addition & 1 deletion packages/waku/src/lib/handlers/handler-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Readable, Writable } from 'node:stream';
import { createServer as createViteServer } from 'vite';
import { default as viteReact } from '@vitejs/plugin-react';

import type { Config } from '../../config.js';
import { resolveConfig } from '../config.js';
import type { Config } from '../config.js';
import { joinPath, decodeFilePathFromAbsolute } from '../utils/path.js';
import { endStream } from '../utils/stream.js';
import { renderHtml } from '../renderers/html-renderer.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/waku/src/lib/handlers/handler-prd.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { EntriesPrd } from '../../server.js';
import type { Config } from '../../config.js';
import { resolveConfig } from '../config.js';
import type { Config } from '../config.js';
import { endStream } from '../utils/stream.js';
import { renderHtml } from '../renderers/html-renderer.js';
import { decodeInput, hasStatusCode, deepFreeze } from '../renderers/utils.js';
Expand Down
Loading