Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(transforms)!: remove unnecessary async #329

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/react-server/src/features/server-action/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ export function vitePluginClientUseServer({
return;
}
const ast = await parseAstAsync(code);
const output = await transformDirectiveProxyExport(ast, {
const result = transformDirectiveProxyExport(ast, {
directive: "use server",
id: manager.buildType ? hashString(id) : id,
runtime: "$$proxy",
});
if (!output) {
if (!result) {
return;
}
const output = result.output;
const importPath = options?.ssr ? ssrRuntimePath : runtimePath;
output.prepend(`\
import { createServerReference } from "${importPath}";
Expand Down
17 changes: 8 additions & 9 deletions packages/react-server/src/features/use-client/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import fs from "node:fs";
import nodePath from "node:path";
import {
getExportNames,
transformDirectiveProxyExport,
} from "@hiogawa/transforms";
import { transformDirectiveProxyExport } from "@hiogawa/transforms";
import { createDebug, memoize, tinyassert } from "@hiogawa/utils";
import {
type Plugin,
Expand Down Expand Up @@ -83,16 +80,17 @@ export function vitePluginServerUseClient({
// node_modules is already transpiled so we can parse it right away
const code = await fs.promises.readFile(meta.id, "utf-8");
const ast = await parseAstAsync(code);
meta.exportNames = new Set(getExportNames(ast, {}).exportNames);
// we need to transform to client reference directly
// otherwise `soruce` will be resolved infinitely by recursion
id = wrapId(id);
const output = await transformDirectiveProxyExport(ast, {
const transformed = transformDirectiveProxyExport(ast, {
directive: "use client",
id,
runtime: "$$proxy",
});
tinyassert(output);
tinyassert(transformed);
meta.exportNames = new Set(transformed.exportNames);
const output = transformed.output;
output.prepend(
`import { registerClientReference as $$proxy } from "${runtimePath}";\n`,
);
Expand Down Expand Up @@ -130,14 +128,15 @@ export function vitePluginServerUseClient({
return;
}
const ast = await parseAstAsync(code);
const output = await transformDirectiveProxyExport(ast, {
const result = transformDirectiveProxyExport(ast, {
directive: "use client",
id: await normalizeId(id),
runtime: "$$proxy",
});
if (!output) {
if (!result) {
return;
}
const output = result.output;
output.prepend(
`import { registerClientReference as $$proxy } from "${runtimePath}";\n`,
);
Expand Down
2 changes: 1 addition & 1 deletion packages/transforms/src/hoist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { debugSourceMap } from "./test-utils";
describe(transformHoistInlineDirective, () => {
async function testTransform(input: string) {
const ast = await parseAstAsync(input);
const { output } = await transformHoistInlineDirective(input, ast, {
const { output } = transformHoistInlineDirective(input, ast, {
id: "<id>",
runtime: "$$register",
directive: "use server",
Expand Down
2 changes: 1 addition & 1 deletion packages/transforms/src/hoist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import MagicString from "magic-string";
import { analyze } from "periscopic";
import { hasDirective } from "./utils";

export async function transformHoistInlineDirective(
export function transformHoistInlineDirective(
input: string,
ast: Program,
{
Expand Down
2 changes: 1 addition & 1 deletion packages/transforms/src/proxy-export.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { transformWrapExport } from "./wrap-export";

async function testTransform(input: string) {
const ast = await parseAstAsync(input);
const output = await transformProxyExport(ast, {
const { output } = transformProxyExport(ast, {
id: "<id>",
runtime: "$$proxy",
});
Expand Down
8 changes: 5 additions & 3 deletions packages/transforms/src/proxy-export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import type { Program } from "estree";
import MagicString from "magic-string";
import { getExportNames, hasDirective } from "./utils";

// TODO: remove async
export async function transformDirectiveProxyExport(
export function transformDirectiveProxyExport(
ast: Program,
options: {
directive: string;
Expand Down Expand Up @@ -36,5 +35,8 @@ export function transformProxyExport(
output += `export const ${name} = ${expr};\n`;
}
}
return new MagicString(output);
return {
output: new MagicString(output),
exportNames,
};
}
2 changes: 1 addition & 1 deletion packages/transforms/src/server-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { transformHoistInlineDirective } from "./hoist";
import { hasDirective } from "./utils";
import { transformWrapExport } from "./wrap-export";

export async function transformServerActionServer(
export function transformServerActionServer(
input: string,
ast: Program,
options: { id: string; runtime: string },
Expand Down
2 changes: 1 addition & 1 deletion packages/transforms/src/wrap-export.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { transformWrapExport } from "./wrap-export";

async function testTransform(input: string) {
const ast = await parseAstAsync(input);
const { output } = await transformWrapExport(input, ast, {
const { output } = transformWrapExport(input, ast, {
id: "<id>",
runtime: "$$wrap",
ignoreExportAllDeclaration: true,
Expand Down
5 changes: 2 additions & 3 deletions packages/transforms/src/wrap-export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Program } from "estree";
import MagicString from "magic-string";
import { extract_names } from "periscopic";

export async function transformWrapExport(
export function transformWrapExport(
input: string,
ast: Program,
options: {
Expand All @@ -12,7 +12,6 @@ export async function transformWrapExport(
},
) {
const output = new MagicString(input);
const exportNames: string[] = [];
const toAppend: string[] = [];

function wrapSimple(name: string) {
Expand Down Expand Up @@ -132,5 +131,5 @@ export async function transformWrapExport(
output.append(["", ...toAppend, ""].join(";\n"));
}

return { exportNames, output };
return { output };
}