Skip to content

Commit

Permalink
refactor(vite-node-miniflare): remove vite-node (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Feb 9, 2024
1 parent 943abf3 commit f2a93ac
Show file tree
Hide file tree
Showing 26 changed files with 53 additions and 338 deletions.
9 changes: 4 additions & 5 deletions packages/vite-node-miniflare/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# vite-node-miniflare

> [!note]
> now it uses `ViteRuntime` introduced by https://github.com/vitejs/vite/pull/12165 instead of `vite-node`
Running [`vite-node`](https://github.com/vitest-dev/vitest/tree/main/packages/vite-node) on [`miniflare`](https://github.com/cloudflare/workers-sdk/tree/main/packages/miniflare).

See https://github.com/dario-piotrowicz/vite-workerd-ssr-request-handler-experimentation/pull/1 for the motivation.
Expand All @@ -23,11 +26,7 @@ import { defineConfig } from "vite";
import { vitePluginViteNodeMiniflare } from "@hiogawa/vite-node-miniflare";

export default defineConfig({
appType: "custom",
ssr: {
noExternal: true,
},
plugins: [vitePluginViteNodeMiniflare({ entry: "./worker-entry.ts" })],
plugins: [vitePluginViteNodeMiniflare({ entry: "/worker-entry.ts" })],
});

//
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { h, renderToString } from "@hiogawa/tiny-react";
import type { ViteNodeMiniflareClient } from "@hiogawa/vite-node-miniflare/client/vite-node";
import type { ViteNodeMiniflareClient } from "@hiogawa/vite-node-miniflare";
import { App } from "./app";

export default {
Expand Down
3 changes: 1 addition & 2 deletions packages/vite-node-miniflare/examples/basic/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ export default defineConfig({
plugins: [
vitePluginViteNodeMiniflare({
debug: true,
// TODO: must be a relative url for invalidation to work?
entry: "./src/worker-entry.ts",
entry: "/src/worker-entry.ts",
miniflareOptions(options) {
options.log = new Log();
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
globPageRoutesServer,
handleReactRouterServer,
} from "@hiogawa/vite-glob-routes/dist/react-router/server";
import type { ViteNodeMiniflareClient } from "@hiogawa/vite-node-miniflare/client/vite-node";
import type { ViteNodeMiniflareClient } from "@hiogawa/vite-node-miniflare";
import React from "react";
import { renderToString } from "react-dom/server";
import {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default defineConfig({
globRoutesPlugin({ root: "/src/routes" }),
vitePluginViteNodeMiniflare({
debug: true,
entry: "./src/server/worker-entry-wrapper.ts",
entry: "/src/server/worker-entry.ts",
miniflareOptions(options) {
options.log = new Log();
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ViteNodeMiniflareClient } from "@hiogawa/vite-node-miniflare/client/vite-node";
import type { ViteNodeMiniflareClient } from "@hiogawa/vite-node-miniflare";
import ReactDomServer from "react-dom/server";
import { App } from "./app";

Expand Down
2 changes: 1 addition & 1 deletion packages/vite-node-miniflare/examples/react/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default defineConfig({
plugins: [
vitePluginViteNodeMiniflare({
debug: true,
entry: "./src/worker-entry.tsx",
entry: "/src/worker-entry.tsx",
miniflareOptions(options) {
options.log = new Log();
},
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion packages/vite-node-miniflare/examples/remix/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default defineConfig({
plugins: [
vitePluginViteNodeMiniflare({
debug: true,
entry: "./app/worker-entry-wrapper.ts",
entry: "/app/worker-entry.ts",
miniflareOptions(options) {
options.log = new Log();
// @ts-ignore why type error?
Expand Down
14 changes: 2 additions & 12 deletions packages/vite-node-miniflare/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hiogawa/vite-node-miniflare",
"version": "0.0.0-pre.12",
"version": "0.0.0-pre.13",
"homepage": "https://github.com/hi-ogawa/vite-plugins/tree/main/packages/vite-node-miniflare",
"repository": {
"type": "git",
Expand All @@ -13,14 +13,6 @@
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./client/worker-entry": {
"types": "./dist/client/worker-entry.d.ts",
"default": "./dist/client/worker-entry.js"
},
"./client/vite-node": {
"types": "./dist/client/vite-node.d.ts",
"default": "./dist/client/vite-node.js"
}
},
"main": "./dist/index.js",
Expand All @@ -37,13 +29,11 @@
"@hattip/adapter-node": "^0.0.34",
"@hattip/compose": "^0.0.34",
"esbuild": "^0.18.6",
"miniflare": "^3.20231030.3",
"vite-node": "^1.0.4"
"miniflare": "^3.20231030.3"
},
"devDependencies": {
"@hiogawa/tiny-rpc": "^0.2.3-pre.11",
"@hiogawa/utils": "1.6.1-pre.10",
"pathe": "^1.1.1",
"vite": "^5.1.0"
},
"peerDependencies": {
Expand Down

This file was deleted.

17 changes: 0 additions & 17 deletions packages/vite-node-miniflare/src/client/polyfills/debug.ts

This file was deleted.

14 changes: 0 additions & 14 deletions packages/vite-node-miniflare/src/client/polyfills/node-fs.ts

This file was deleted.

This file was deleted.

This file was deleted.

20 changes: 0 additions & 20 deletions packages/vite-node-miniflare/src/client/polyfills/node-url.ts

This file was deleted.

20 changes: 0 additions & 20 deletions packages/vite-node-miniflare/src/client/polyfills/node-vm.ts

This file was deleted.

10 changes: 0 additions & 10 deletions packages/vite-node-miniflare/src/client/polyfills/usage-checker.ts

This file was deleted.

64 changes: 4 additions & 60 deletions packages/vite-node-miniflare/src/client/vite-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,22 @@ import {
httpClientAdapter,
proxyTinyRpc,
} from "@hiogawa/tiny-rpc";
import { tinyassert } from "@hiogawa/utils";
import type { HMRPayload } from "vite";
import type { ViteNodeRunnerOptions } from "vite-node";
import { ViteNodeRunner } from "vite-node/client";
import { installSourcemapsSupport } from "vite-node/source-map";
import { ViteRuntime } from "vite/runtime";
import type { ViteNodeRpc } from "..";
import { __setDebug } from "./polyfills/debug";
import { __setUnsafeEval } from "./polyfills/node-vm";
import type { ViteNodeRpc } from "../server/vite-node";

export interface ViteNodeMiniflareClient {
rpc: TinyRpcProxy<ViteNodeRpc>;
runner: ViteNodeRunner;
runtime: ViteRuntime;
runtimeHMRHandler: (payload: HMRPayload) => void;
}

export function createViteNodeClient(options: {
unsafeEval: any;
serverRpcUrl: string;
runnerOptions: Omit<ViteNodeRunnerOptions, "fetchModule" | "resolveId">;
root: string;
debug: boolean;
}): ViteNodeMiniflareClient {
__setUnsafeEval(options.unsafeEval);
__setDebug(options.debug);

const rpc = proxyTinyRpc<ViteNodeRpc>({
adapter: httpClientAdapter({ url: options.serverRpcUrl }),
});
Expand All @@ -37,7 +27,7 @@ export function createViteNodeClient(options: {

const runtime = new ViteRuntime(
{
root: options.runnerOptions.root,
root: options.root,
fetchModule(id, importer) {
return rpc.ssrFetchModule(id, importer);
},
Expand Down Expand Up @@ -80,51 +70,5 @@ export function createViteNodeClient(options: {
}
);

const runner = new ViteNodeRunner({
...options.runnerOptions,
fetchModule(id) {
return rpc.fetchModule(id);
},
resolveId(id, importer) {
return rpc.resolveId(id, importer);
},
});

// Since Vitest's getSourceMap/extractSourceMap relies on `Buffer.from(mapString, 'base64').toString('utf-8')`,
// we inject minimal Buffer polyfill temporary during this function.
// https://github.com/vitest-dev/vitest/blob/8dabef860a3f51f5a4c4debc10faa1837fdcdd71/packages/vite-node/src/source-map.ts#L57-L62
// prettier-ignore
0 && installSourcemapsSupport({
getSourceMap: (source) => {
const teardown = setupBufferPolyfill();
try {
return runner.moduleCache.getSourceMap(source);
} finally {
teardown();
}
},
});

return { rpc, runner, runtime, runtimeHMRHandler };
}

function setupBufferPolyfill() {
const prev = globalThis.Buffer;
globalThis.Buffer = BufferPolyfill as any;
return () => {
globalThis.Buffer = prev;
};
return { rpc, runtime, runtimeHMRHandler };
}

const BufferPolyfill = {
from: (s: unknown, encoding: unknown) => {
tinyassert(typeof s === "string");
tinyassert(encoding === "base64");
return {
toString: (encoding: unknown) => {
tinyassert(encoding === "utf-8");
return atob(s);
},
};
},
};
Loading

0 comments on commit f2a93ac

Please sign in to comment.