diff --git a/lib/client.ts b/lib/client.ts index 886661a..0bb7634 100644 --- a/lib/client.ts +++ b/lib/client.ts @@ -50,7 +50,7 @@ export const clientPlugin = function ({ let watchers: Record = {}; - const microbundle = require("microbundle"); + const microbundle = (await import("microbundle")).default; const startBundlers = async function () { const assetsJsonPath = path.resolve(absDist, "clientAssets.json"); @@ -164,7 +164,7 @@ export const clientPlugin = function ({ } if (watch) { - const chokidar = require("chokidar"); + const chokidar = (await import("chokidar")).default; chokidar.watch(absSrc).on("all", startBundlers); } diff --git a/lib/devSocket.ts b/lib/devSocket.ts index 616ff7d..7c22391 100644 --- a/lib/devSocket.ts +++ b/lib/devSocket.ts @@ -3,13 +3,19 @@ import url from "url"; import { Ctx, useCtx } from "./ctx"; import { copy, emptyDir } from "fs-extra"; import path from "path"; +import { fileURLToPath } from "url"; const integrateClientJs = async function (ctx: Ctx) { const publicFolder = "sosseDevSocketClient"; if (!globalThis.sosseDevClientCopied) { + const dirName = + typeof __dirname !== "undefined" + ? __dirname + : path.dirname(fileURLToPath(import.meta.url)); + const clientJsPath = path.resolve( - __dirname, + dirName, "..", "dev-socket-client", "dist" @@ -53,7 +59,7 @@ export const devSocket = async function ({ return; } - const WebSocket = require("ws"); + const WebSocket = (await import("ws")).default; const ctx = useCtx(); diff --git a/lib/hsr.ts b/lib/hsr.ts index 8889102..3d221dc 100644 --- a/lib/hsr.ts +++ b/lib/hsr.ts @@ -83,7 +83,7 @@ export const hsr = async function ({ let listen; clearModule.all(); - const { setCtx, unsetCtx } = require("sosse"); + const { setCtx, unsetCtx } = await import("sosse"); setCtx(ctx); @@ -118,7 +118,7 @@ export const hsr = async function ({ } }, wait); - const chokidar = require("chokidar"); + const chokidar = (await import("chokidar")).default; const watcher = chokidar.watch(absWatch, { ignored: absExclude,