From 580b90646ecb30fd44d7df8c57bfb0ab76f45edf Mon Sep 17 00:00:00 2001 From: Aral Roca Date: Sun, 24 Mar 2024 09:38:01 +0100 Subject: [PATCH] fix: adapt config changes --- examples/brisa/src/prerender.tsx | 4 +-- examples/react/src/prerender.tsx | 4 +-- package/index.ts | 58 ++++++++++++++++++-------------- 3 files changed, 36 insertions(+), 30 deletions(-) diff --git a/examples/brisa/src/prerender.tsx b/examples/brisa/src/prerender.tsx index f45ede1..948aeef 100644 --- a/examples/brisa/src/prerender.tsx +++ b/examples/brisa/src/prerender.tsx @@ -1,8 +1,8 @@ -import { type Config } from "prerender-macro"; +import { type PrerenderConfig } from "prerender-macro"; import { dangerHTML } from "brisa"; import { renderToString } from "brisa/server"; export const prerenderConfig = { render: async (Component, props) => dangerHTML(await renderToString()), -} satisfies Config; +} satisfies PrerenderConfig; diff --git a/examples/react/src/prerender.tsx b/examples/react/src/prerender.tsx index ba22d33..8004b12 100644 --- a/examples/react/src/prerender.tsx +++ b/examples/react/src/prerender.tsx @@ -1,4 +1,4 @@ -import { type Config } from "prerender-macro"; +import { type PrerenderConfig } from "prerender-macro"; import { renderToString } from "react-dom/server"; export const prerenderConfig = { @@ -8,4 +8,4 @@ export const prerenderConfig = { postRender: (htmlString) => { return
; }, -} satisfies Config; +} satisfies PrerenderConfig; diff --git a/package/index.ts b/package/index.ts index aa487e6..cb40086 100644 --- a/package/index.ts +++ b/package/index.ts @@ -1,14 +1,16 @@ -import type { BunPlugin } from "bun"; +import type { BunPlugin, TranspilerOptions } from "bun"; import { dirname } from "node:path"; import ts from "typescript"; -import { prerender } from "./prerender"; -const transpiler = new Bun.Transpiler({ loader: "tsx" }); +let transpiler = new Bun.Transpiler({ loader: "tsx" }); const JSX_RUNTIME = ["jsx-runtime", "jsx-dev-runtime"]; -type PrerenderPluginParams = { prerenderConfigPath: string }; +export type PluginConfig = { + prerenderConfigPath: string; + tsconfig?: TranspilerOptions["tsconfig"]; +}; -export type Config = { +export type PrerenderConfig = { render: ( Component: any, props: any, @@ -16,20 +18,24 @@ export type Config = { postRender?: (htmlString: string) => JSX.Element; }; -export default function plugin({ prerenderConfigPath }: PrerenderPluginParams) { +export default function plugin(pluginConfig: PluginConfig) { + if (!pluginConfig?.prerenderConfigPath) { + throw new Error("prerender-macro: prerenderConfigPath config is required"); + } return { name: "prerender-plugin", async setup(build) { build.onLoad({ filter: /\.(tsx|jsx)$/ }, async ({ path, loader }) => { const code = await Bun.file(path).text(); - const config = (await import(prerenderConfigPath))?.prerenderConfig; + const prerenderConfig = (await import(pluginConfig.prerenderConfigPath)) + ?.prerenderConfig; try { const contents = transpile({ code, path, - prerenderConfigPath, - config, + pluginConfig, + prerenderConfig, }); return { contents, loader }; @@ -45,26 +51,26 @@ export default function plugin({ prerenderConfigPath }: PrerenderPluginParams) { export function transpile({ code, path, - prerenderConfigPath, - config, + pluginConfig, + prerenderConfig, }: { code: string; path: string; - prerenderConfigPath: string; - config?: Config; + pluginConfig: PluginConfig; + prerenderConfig?: PrerenderConfig; }) { const sourceFile = createSourceFile(code); const importsWithPrerender = getImportsWithPrerender(sourceFile, path); if (!importsWithPrerender.length) return code; - let modifiedAst = addExtraImports(sourceFile, prerenderConfigPath, config); + let modifiedAst = addExtraImports(sourceFile, pluginConfig, prerenderConfig); modifiedAst = replaceJSXToMacroCall( modifiedAst, importsWithPrerender, - prerenderConfigPath, - config, + pluginConfig, + prerenderConfig, ) as ts.SourceFile; const modifiedCode = ts @@ -135,8 +141,8 @@ function isPrerenderImport(node: ts.Node): node is ts.ImportDeclaration { function addExtraImports( ast: ts.SourceFile, - prerenderConfigPath: string, - config?: Config, + pluginConfig: PluginConfig, + prerenderConfig?: PrerenderConfig, ) { const allImports = [...ast.statements]; @@ -166,7 +172,7 @@ function addExtraImports( ), ); - if (config?.postRender) { + if (prerenderConfig?.postRender) { allImports.unshift( ts.factory.createImportDeclaration( undefined, @@ -181,7 +187,7 @@ function addExtraImports( ), ]), ), - ts.factory.createStringLiteral(prerenderConfigPath), + ts.factory.createStringLiteral(pluginConfig.prerenderConfigPath), ), ); } @@ -205,8 +211,8 @@ function addExtraImports( function replaceJSXToMacroCall( node: ts.Node, imports: any[], - prerenderConfigPath: string, - config?: Config, + pluginConfig: PluginConfig, + prerenderConfig?: PrerenderConfig, context?: ts.TransformationContext, ): ts.Node { if (ts.isJsxSelfClosingElement(node) || ts.isJsxOpeningElement(node)) { @@ -252,14 +258,14 @@ function replaceJSXToMacroCall( ), ts.factory.createPropertyAssignment( ts.factory.createIdentifier("prerenderConfigPath"), - ts.factory.createStringLiteral(prerenderConfigPath), + ts.factory.createStringLiteral(pluginConfig.prerenderConfigPath), ), ]), ], ); // Wrap with postRender function - if (config?.postRender) { + if (prerenderConfig?.postRender) { macroCall = ts.factory.createCallExpression( ts.factory.createPropertyAccessExpression( ts.factory.createIdentifier("prerenderConfig"), @@ -287,8 +293,8 @@ function replaceJSXToMacroCall( replaceJSXToMacroCall( child, imports, - prerenderConfigPath, - config, + pluginConfig, + prerenderConfig, context, ), context,