Skip to content

Commit

Permalink
feat: optional ssr hmr (disabled by default)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Jan 31, 2024
1 parent 9b40a28 commit 95d6705
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packages/vite-node-miniflare/examples/react/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export default defineConfig({
},
plugins: [
vitePluginViteNodeMiniflare({
// TODO: ssr hmr plugin https://github.com/hi-ogawa/vite-plugins/pull/157
// hmr: true,
debug: true,
entry: "./src/worker-entry.tsx",
miniflareOptions(options) {
Expand Down
6 changes: 3 additions & 3 deletions packages/vite-node-miniflare/src/client/worker-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface Env {
__VITE_NODE_SERVER_RPC_URL: string;
__VITE_NODE_RUNNER_OPTIONS: any;
__VITE_NODE_DEBUG: boolean;
__VITE_RUNTIME_HMR: boolean;
__WORKER_ENTRY: string;
}

Expand All @@ -31,9 +32,8 @@ export default {
const payloads = await client.rpc.getHMRPayloads();
for (const payload of payloads) {
console.log("[handleHMRUpdate]", payload);
// TODO: for now, we do module tree invalidation instead of hmr so that standard react plugin integration works like before.
// TODO: make it configurable
if (payload.type === "update") {
// simple module tree invalidation instead of hmr for client-only hmr plugin compatibility
if (!env.__VITE_RUNTIME_HMR && payload.type === "update") {
for (const update of payload.updates) {
// TODO: unwrapId?
const invalidated = client.runtime.moduleCache.invalidateDepTree([
Expand Down
3 changes: 3 additions & 0 deletions packages/vite-node-miniflare/src/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { setupViteNodeServerRpc } from "./vite-node";
export function vitePluginViteNodeMiniflare(pluginOptions: {
entry: string;
debug?: boolean;
// ssr hmr is disabled by default for normal client-only hmr plugin compatibility
hmr?: boolean;
// hooks to customize options
miniflareOptions?: (options: MiniflareOptions) => void;
viteNodeServerOptions?: (options: ViteNodeServerOptions) => void;
Expand Down Expand Up @@ -82,6 +84,7 @@ export function vitePluginViteNodeMiniflare(pluginOptions: {
entry: pluginOptions.entry,
rpcOrigin: ctx.url.origin,
debug: pluginOptions.debug,
hmr: pluginOptions.hmr,
viteNodeRunnerOptions,
});
pluginOptions.miniflareOptions?.(miniflareOptions);
Expand Down
2 changes: 2 additions & 0 deletions packages/vite-node-miniflare/src/server/vite-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export function setupViteNodeServerRpc(
entry: string;
rpcOrigin: string;
debug?: boolean;
hmr?: boolean;
viteNodeRunnerOptions: Partial<ViteNodeRunnerOptions>;
}) {
return {
Expand All @@ -111,6 +112,7 @@ export function setupViteNodeServerRpc(
__VITE_NODE_SERVER_RPC_URL: options.rpcOrigin + rpcBase,
__VITE_NODE_RUNNER_OPTIONS: options.viteNodeRunnerOptions as any,
__VITE_NODE_DEBUG: options.debug ?? false,
__VITE_RUNTIME_HMR: options.hmr ?? false,
},
} satisfies MiniflareOptions;
}
Expand Down

0 comments on commit 95d6705

Please sign in to comment.