Skip to content

Commit

Permalink
feat(dev): update jiti to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Sep 25, 2024
1 parent b97bc01 commit 499e7bb
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 74 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"get-port-please": "^3.1.2",
"h3": "^1.12.0",
"http-shutdown": "^1.2.2",
"jiti": "^1.21.6",
"jiti": "^2.0.0",
"mlly": "^1.7.1",
"node-forge": "^1.3.1",
"pathe": "^1.1.2",
Expand All @@ -73,4 +73,4 @@
"vitest": "^2.1.1"
},
"packageManager": "pnpm@9.11.0"
}
}
74 changes: 37 additions & 37 deletions pnpm-lock.yaml

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

5 changes: 3 additions & 2 deletions scripts/listhen.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env node

import jiti from "jiti";
import { createJiti } from "jiti";

const { runMain } = jiti(import.meta.url)("../src/cli");
const jiti = createJiti(import.meta.url)
const { runMain } = await jiti.import("../src/cli");

runMain();
29 changes: 7 additions & 22 deletions src/server/_resolver.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,20 @@
import { relative } from "pathe";
import { join, relative } from "pathe";

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

const _jitiRequire = jiti(process.cwd(), {
const jiti = createJiti(join(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 {
// Ignore errors
}
};

return {
relative: (path: string) => relative(process.cwd(), path),
formateRelative: (path: string) => `\`./${relative(process.cwd(), path)}\``,
import: _import,
resolve,
tryResolve,
formatRelative: (path: string) => `\`./${relative(process.cwd(), path)}\``,
import: jiti.import,
resolve: (id: string) => jiti.esmResolve(id),
tryResolve: (id: string) => jiti.esmResolve(id, { try: true }),
};
}
10 changes: 5 additions & 5 deletions src/server/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { readFile, stat } from "node:fs/promises";
import { consola } from "consola";
import { dirname, join, resolve } from "pathe";
import type { ConsolaInstance } from "consola";
import { resolve as _resolve } from "mlly";
import { resolve as _resolve, fileURLToPath } from "mlly";
import type { CrossWSOptions, ListenOptions } from "../types";
import { createResolver } from "./_resolver";

Expand Down Expand Up @@ -39,7 +39,7 @@ export async function createDevServer(
for (const suffix of ["", "/server/src", "/server", "/src"]) {
const resolved = resolver.tryResolve(entry + suffix);
if (resolved) {
return resolved;
return fileURLToPath(resolved);
}
}
};
Expand Down Expand Up @@ -120,7 +120,7 @@ export async function createDevServer(
if (initial) {
for (const dir of staticDirs) {
logger.log(
`πŸ“ Serving static files from ${resolver.formateRelative(dir)}`,
`πŸ“ Serving static files from ${resolver.formatRelative(dir)}`,
);
}
}
Expand All @@ -136,11 +136,11 @@ export async function createDevServer(
}
if (initial) {
logger.log(
`πŸš€ Loading server entry ${resolver.formateRelative(_entry)}`,
`πŸš€ Loading server entry ${resolver.formatRelative(_entry)}`,
);
}

const _loadedEntry = await resolver.import(_entry);
const _loadedEntry = await resolver.import(_entry) as any;

let _handler =
_loadedEntry.handler ||
Expand Down
12 changes: 6 additions & 6 deletions src/server/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export async function listenAndWatch(
});

// Initialize listener
const listenter = await listen(devServer.nodeListener, {
const listener = await listen(devServer.nodeListener, {
...options,
_entry: devServer._entry,
ws: options.ws ? devServer._ws : undefined,
Expand All @@ -37,8 +37,8 @@ export async function listenAndWatch(
await devServer.reload(true);

// Hook close event to stop watcher too
const _close = listenter.close;
listenter.close = async () => {
const _close = listener.close;
listener.close = async () => {
if (watcher) {
await watcher.unsubscribe().catch((error) => {
logger.error(error);
Expand Down Expand Up @@ -67,7 +67,7 @@ export async function listenAndWatch(
}
const eventsString = filteredEvents
.map(
(e) => `${devServer.resolver.formateRelative(e.path)} ${e.type}d`,
(e) => `${devServer.resolver.formatRelative(e.path)} ${e.type}d`,
)
.join(", ");
logger.log(`πŸ”„ Reloading server (${eventsString})`);
Expand All @@ -83,7 +83,7 @@ export async function listenAndWatch(
);

logger.log(
`πŸ‘€ Watching ${devServer.resolver.formateRelative(
`πŸ‘€ Watching ${devServer.resolver.formatRelative(
devServer.cwd,
)} for changes`,
);
Expand All @@ -95,5 +95,5 @@ export async function listenAndWatch(
);
}

return listenter;
return listener;
}

0 comments on commit 499e7bb

Please sign in to comment.