Skip to content

Commit

Permalink
feat: dev server with serve static support (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Aug 2, 2023
1 parent d5bf13e commit 3622418
Show file tree
Hide file tree
Showing 15 changed files with 382 additions and 262 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"consola": "^3.2.3",
"defu": "^6.1.2",
"get-port-please": "^3.0.1",
"h3": "^1.8.0-rc.2",
"http-shutdown": "^1.2.2",
"jiti": "^1.19.1",
"mlly": "^1.4.0",
Expand All @@ -58,7 +59,6 @@
"changelogen": "^0.5.4",
"eslint": "^8.46.0",
"eslint-config-unjs": "^0.2.1",
"h3": "^1.8.0-rc.2",
"ip-regex": "^5.0.0",
"prettier": "^3.0.0",
"typescript": "^5.1.6",
Expand Down
8 changes: 0 additions & 8 deletions playground/app.ts

This file was deleted.

10 changes: 7 additions & 3 deletions playground/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { toNodeListener } from "h3";
import { app } from "./app";
import { createApp, eventHandler } from "h3";

export default toNodeListener(app);
export const app = createApp();

app.use(
"/",
eventHandler(() => ({ hello: "world!" })),
);
Binary file added playground/public/favicon.ico
Binary file not shown.
1 change: 1 addition & 0 deletions playground/public/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Static asset works!
22 changes: 10 additions & 12 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 0 additions & 33 deletions src/_utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { promises as fs } from "node:fs";
import { networkInterfaces } from "node:os";
import { relative, resolve } from "node:path";
import { colors } from "consola/utils";
import { fileURLToPath } from "mlly";
import { isAbsolute } from "pathe";
import type { Certificate, HTTPSOptions } from "./types";

export async function resolveCert(
Expand Down Expand Up @@ -69,33 +66,3 @@ export function formatURL(url: string) {
),
);
}

export async function createImporter(input: string, _cwd?: string) {
const cwd = resolve(_cwd ? fileURLToPath(_cwd) : ".");

const jiti = await import("jiti").then((r) => r.default || r);
const _jitiRequire = jiti(cwd, {
esmResolve: true,
requireCache: false,
interopDefault: true,
});

if (!isAbsolute(input) && !input.startsWith(".")) {
input = `./${input}`;
}

const entry = _jitiRequire.resolve(input);

const _import = () => {
const r = _jitiRequire(input);
return Promise.resolve(r.default || r);
};

return {
cwd,
relative: (path: string) => relative(cwd, path),
formateRelative: (path: string) => `\`./${relative(cwd, path)}\``,
entry,
import: _import,
};
}
28 changes: 15 additions & 13 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { resolve } from "node:path";
import { WatchOptions } from "node:fs";
import { defineCommand, runMain as _runMain } from "citty";
import { isAbsolute } from "pathe";
import { name, description, version } from "../package.json";
import { listen } from "./listen";
import { listenAndWatch } from "./watch";
import type { ListenOptions, WatchOptions } from "./types";
import { createImporter } from "./_utils";
import { listenAndWatch } from "./server";
import type { ListenOptions } from "./types";
import { DevServerOptions, createDevServer } from "./server/dev";

export const main = defineCommand({
meta: {
Expand Down Expand Up @@ -63,12 +64,8 @@ export const main = defineCommand({
},
},
async run({ args }) {
const cwd = resolve(args.cwd || ".");
process.chdir(cwd);

const opts: Partial<ListenOptions & WatchOptions> = {
const opts: Partial<ListenOptions & WatchOptions & DevServerOptions> = {
...args,
cwd,
port: args.port,
hostname: args.host,
clipboard: args.clipboard,
Expand All @@ -78,12 +75,17 @@ export const main = defineCommand({
https: args.https, // TODO: Support custom cert
};

const entry =
isAbsolute(args.entry) || args.entry.startsWith(".")
? args.entry
: `./${args.entry}`;

if (args.watch) {
await listenAndWatch(args.entry, opts);
await listenAndWatch(entry, opts);
} else {
const importer = await createImporter(args.entry);
const handler = await importer.import();
await listen(handler, opts);
const devServer = await createDevServer(entry, opts);
await listen(devServer.nodeListener, opts);
await devServer.reload(true);
}
},
});
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from "./listen";
export * from "./types";
export * from "./watch";
export * from "./server";
33 changes: 33 additions & 0 deletions src/server/_resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { relative } from "node:path";

export async function createResolver() {
const jiti = await import("jiti").then((r) => r.default || r);

const _jitiRequire = jiti(process.cwd(), {
cache: true,
esmResolve: true,
requireCache: false,
interopDefault: true,
});

const _import = (id: string) => {
const r = _jitiRequire(id);
return Promise.resolve(r.default || r);
};

const resolve = (id: string) => _jitiRequire.resolve(id);

const tryResolve = (id: string) => {
try {
return resolve(id);
} catch {}
};

return {
relative: (path: string) => relative(process.cwd(), path),
formateRelative: (path: string) => `\`./${relative(process.cwd(), path)}\``,
import: _import,
resolve,
tryResolve,
};
}

0 comments on commit 3622418

Please sign in to comment.