From f696a95b9568cbe32bb32a4a850de5a9ac1c5989 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Sat, 22 Jun 2024 10:57:32 +0900 Subject: [PATCH] chore: replace old ones --- packages/tiny-refresh/src/transform.ts | 72 ++------------------------ packages/tiny-refresh/src/vite.ts | 5 +- packages/tiny-refresh/src/webpack.ts | 5 +- 3 files changed, 9 insertions(+), 73 deletions(-) diff --git a/packages/tiny-refresh/src/transform.ts b/packages/tiny-refresh/src/transform.ts index 12726250..1d5d9806 100644 --- a/packages/tiny-refresh/src/transform.ts +++ b/packages/tiny-refresh/src/transform.ts @@ -1,15 +1,7 @@ import type * as estree from "estree"; import { parseAstAsync } from "vite"; -interface TransformOptions { - // e.g. "react", "preact/compat", "@hiogawa/tiny-react" - runtime: string; - // allow "@hiogawa/tiny-react" to re-export refresh runtime by itself to simplify dependency - refreshRuntime: string; - debug?: boolean; -} - -export interface TransformOptions2 { +export interface TransformOptions { // "react", "preact/compat", "@hiogawa/tiny-react" runtime: string; // allow "@hiogawa/tiny-react" to re-export refresh runtime by itself to simplify dependency @@ -18,9 +10,9 @@ export interface TransformOptions2 { debug: boolean; } -export type RefreshRuntimeOptions = Pick; +export type RefreshRuntimeOptions = Pick; -export async function transform(code: string, options: TransformOptions2) { +export async function transform(code: string, options: TransformOptions) { const result = await analyzeCode(code); if (result.errors.length || result.entries.length === 0) { return; @@ -52,64 +44,6 @@ ${wrap} return result.outCode + footer; } -export async function transformVite(code: string, options: TransformOptions) { - const result = await analyzeCode(code); - if (result.errors.length || result.entries.length === 0) { - return; - } - let footer = /* js */ ` -import * as $$runtime from "${options.runtime}"; -import * as $$refresh from "${options.refreshRuntime}"; -if (import.meta.hot) { - () => import.meta.hot.accept(); // need a fake "accept" for Vite to notice - const $$manager = $$refresh.setupVite( - import.meta.hot, - $$runtime, - ${options.debug ?? false} - ); -`; - for (const { id, hooks } of result.entries) { - footer += `\ - ${id} = $$manager.wrap("${id}", ${id}, ${JSON.stringify(hooks.join("/"))}); -`; - } - footer += `\ - $$manager.setup(); -}`; - // no need to manipulate sourcemap since transform only appends - return result.outCode + footer; -} - -export async function transformWebpack( - code: string, - options: TransformOptions -) { - const result = await analyzeCode(code); - if (result.errors.length || result.entries.length === 0) { - return; - } - let footer = /* js */ ` -import * as $$runtime from "${options.runtime}"; -import * as $$refresh from "${options.refreshRuntime}"; -if (import.meta.webpackHot) { - const $$manager = $$refresh.setupWebpack( - import.meta.webpackHot, - $$runtime, - ${options.debug ?? false}, - ) -`; - for (const { id, hooks } of result.entries) { - footer += `\ - ${id} = $$manager.wrap("${id}", ${id}, ${JSON.stringify(hooks.join("/"))}); -`; - } - footer += `\ - $$manager.setup(); -} -`; - return result.outCode + footer; -} - // // extract component declarations // diff --git a/packages/tiny-refresh/src/vite.ts b/packages/tiny-refresh/src/vite.ts index a188a0a9..b57e1682 100644 --- a/packages/tiny-refresh/src/vite.ts +++ b/packages/tiny-refresh/src/vite.ts @@ -1,5 +1,5 @@ import { type FilterPattern, type Plugin, createFilter } from "vite"; -import { transformVite } from "./transform"; +import { transform } from "./transform"; export function vitePluginTinyRefresh(options?: { include?: FilterPattern; @@ -17,9 +17,10 @@ export function vitePluginTinyRefresh(options?: { apply: "serve", transform(code, id, transformOptions) { if (!transformOptions?.ssr && filter(id)) { - return transformVite(code, { + return transform(code, { runtime: options?.runtime ?? "react", refreshRuntime: options?.refreshRuntime ?? "@hiogawa/tiny-refresh", + mode: "vite", debug: true, }); } diff --git a/packages/tiny-refresh/src/webpack.ts b/packages/tiny-refresh/src/webpack.ts index 3cc06dd0..a5e3a71d 100644 --- a/packages/tiny-refresh/src/webpack.ts +++ b/packages/tiny-refresh/src/webpack.ts @@ -1,4 +1,4 @@ -import { transformWebpack } from "./transform"; +import { transform } from "./transform"; export type TinyRefreshLoaderOptions = { refreshRuntime?: string; @@ -9,9 +9,10 @@ export type TinyRefreshLoaderOptions = { export default function loader(this: any, input: string) { const callback = this.async(); const options = this.getOptions() as TinyRefreshLoaderOptions; - transformWebpack(input, { + transform(input, { refreshRuntime: options.refreshRuntime ?? "@hiogawa/tiny-refresh", runtime: "react", + mode: "webpack", debug: true, }).then( (result) => callback(null, result ?? input),