Skip to content

Commit

Permalink
chore: replace old ones
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Jun 22, 2024
1 parent 04a4cbb commit f696a95
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 73 deletions.
72 changes: 3 additions & 69 deletions packages/tiny-refresh/src/transform.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -18,9 +10,9 @@ export interface TransformOptions2 {
debug: boolean;
}

export type RefreshRuntimeOptions = Pick<TransformOptions2, "mode" | "debug">;
export type RefreshRuntimeOptions = Pick<TransformOptions, "mode" | "debug">;

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;
Expand Down Expand Up @@ -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
//
Expand Down
5 changes: 3 additions & 2 deletions packages/tiny-refresh/src/vite.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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,
});
}
Expand Down
5 changes: 3 additions & 2 deletions packages/tiny-refresh/src/webpack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { transformWebpack } from "./transform";
import { transform } from "./transform";

export type TinyRefreshLoaderOptions = {
refreshRuntime?: string;
Expand All @@ -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),
Expand Down

0 comments on commit f696a95

Please sign in to comment.