Skip to content

Commit

Permalink
fix: adapt config changes
Browse files Browse the repository at this point in the history
  • Loading branch information
aralroca committed Mar 24, 2024
1 parent 4ec67d7 commit 580b906
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 30 deletions.
4 changes: 2 additions & 2 deletions examples/brisa/src/prerender.tsx
Original file line number Diff line number Diff line change
@@ -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(<Component {...props} />)),
} satisfies Config;
} satisfies PrerenderConfig;
4 changes: 2 additions & 2 deletions examples/react/src/prerender.tsx
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -8,4 +8,4 @@ export const prerenderConfig = {
postRender: (htmlString) => {
return <div dangerouslySetInnerHTML={{ __html: htmlString }} />;
},
} satisfies Config;
} satisfies PrerenderConfig;
58 changes: 32 additions & 26 deletions package/index.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,41 @@
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,
) => JSX.Element | string | Promise<JSX.Element | string>;
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 };
Expand All @@ -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
Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -166,7 +172,7 @@ function addExtraImports(
),
);

if (config?.postRender) {
if (prerenderConfig?.postRender) {
allImports.unshift(
ts.factory.createImportDeclaration(
undefined,
Expand All @@ -181,7 +187,7 @@ function addExtraImports(
),
]),
),
ts.factory.createStringLiteral(prerenderConfigPath),
ts.factory.createStringLiteral(pluginConfig.prerenderConfigPath),
),
);
}
Expand All @@ -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)) {
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -287,8 +293,8 @@ function replaceJSXToMacroCall(
replaceJSXToMacroCall(
child,
imports,
prerenderConfigPath,
config,
pluginConfig,
prerenderConfig,
context,
),
context,
Expand Down

0 comments on commit 580b906

Please sign in to comment.