Skip to content

Commit

Permalink
feat: support postrender
Browse files Browse the repository at this point in the history
  • Loading branch information
aralroca committed Mar 22, 2024
1 parent a003bfa commit 6393ab7
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 99 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ It is necessary to do it this way because this configuration will be executed wh

The `prerenderConfig` named export needs this mandatory configuration to work:

| Parameter | Description | Mandatory |
| ------------ | ------------------------------------------------------------------------------------------------------------------- | --------- |
| `render` | Function to render the component on your framework ([AOT](https://en.wikipedia.org/wiki/Ahead-of-time_compilation)) | `true` |
| `postRender` | Function to make a post rendering in runtime ([JIT](https://en.wikipedia.org/wiki/Just-in-time_compilation)) | `false` |
| Parameter | Description | Mandatory | Can be async |
| ------------ | ------------------------------------------------------------------------------------------------------------------- | --------- | ------------ |
| `render` | Function to render the component on your framework ([AOT](https://en.wikipedia.org/wiki/Ahead-of-time_compilation)) | `true` | `yes` |
| `postRender` | Function to make a post rendering in runtime ([JIT](https://en.wikipedia.org/wiki/Just-in-time_compilation)) | `false` | `no` |

> [!NOTE]
>
Expand All @@ -157,14 +157,14 @@ The `prerenderConfig` named export needs this mandatory configuration to work:
Configuration example:

```tsx
import prerenderMacroPlugin from "prerender-macro";
import prerenderMacroPlugin, { type Config } from "prerender-macro";
import { dangerHTML } from "brisa";
import { renderToString } from "brisa/server";

export const prerenderConfig = {
render: async (Component: any, props: any) =>
render: async (Component, props) =>
dangerHTML(await renderToString(<Component {...props} />)),
};
} satisfies Config;

export const plugin = prerenderMacroPlugin({
prerenderConfigPath: import.meta.url,
Expand Down Expand Up @@ -192,7 +192,7 @@ For React components, since React does not have a built-in function for injectin
Configuration example:

```tsx
import prerenderMacroPlugin from "prerender-macro";
import prerenderMacroPlugin, { type Config } from "prerender-macro";
import { renderToString } from "react-dom/server";

export const prerenderConfig = {
Expand All @@ -202,7 +202,7 @@ export const prerenderConfig = {
postRender: (htmlString) => (
<div dangerouslySetInnerHTML={{ __html: htmlString }} />
),
};
} satisfies Config;

export const plugin = prerenderMacroPlugin({
prerenderConfigPath: import.meta.url,
Expand Down Expand Up @@ -240,19 +240,19 @@ For Preact components, since Preact does not have a built-in function for inject
Configuration example:

```tsx
import prerenderMacroPlugin from "prerender-macro";
import prerenderMacroPlugin, { type Config } from "prerender-macro";
import { render } from "preact-render-to-string";
import { h } from "preact";

export const prerenderConfig = {
render: async (Component: any, props: any) => {
render: async (Component, props) => {
return (
<div
dangerouslySetInnerHTML={{ __html: render(<Component {...props} />) }}
/>
);
},
};
} satisfies Config;

export const plugin = prerenderMacroPlugin({
prerenderConfigPath: import.meta.url,
Expand Down
32 changes: 21 additions & 11 deletions examples-and-tests/brisa/plugin.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const currentFile = import.meta.url.replace("file://", "");
describe("Brisa", () => {
describe("plugin", () => {
it('should not transform if there is not an import attribute with type "prerender"', () => {
const input = `
const code = `
import Foo from "./components";
import { Bar } from "./components";
Expand All @@ -23,13 +23,15 @@ describe("Brisa", () => {
);
}
`;
const output = normalizeQuotes(transpile(input, currentFile, configPath));
const expected = normalizeQuotes(input);
const output = normalizeQuotes(
transpile({ code, path: currentFile, prerenderConfigPath: configPath }),
);
const expected = normalizeQuotes(code);

expect(output).toBe(expected);
});
it("should transform a static component", () => {
const input = `
const code = `
import Foo from "./components" with { type: "prerender" };
import { Bar } from "./components";
Expand All @@ -42,7 +44,9 @@ describe("Brisa", () => {
);
}
`;
const output = normalizeQuotes(transpile(input, currentFile, configPath));
const output = normalizeQuotes(
transpile({ code, path: currentFile, prerenderConfigPath: configPath }),
);
const expected = normalizeQuotes(`
import Foo from "./components";
import {Bar} from "./components";
Expand All @@ -59,7 +63,7 @@ describe("Brisa", () => {
});

it("should transform a static component from named export", () => {
const input = `
const code = `
import { Bar } from "./components" with { type: "prerender" };
import Foo from "./components";
Expand All @@ -72,7 +76,9 @@ describe("Brisa", () => {
);
}
`;
const output = normalizeQuotes(transpile(input, currentFile, configPath));
const output = normalizeQuotes(
transpile({ code, path: currentFile, prerenderConfigPath: configPath }),
);
const expected = normalizeQuotes(`
import {Bar} from "./components";
import Foo from "./components";
Expand All @@ -89,14 +95,16 @@ describe("Brisa", () => {
});

it("should transform a static component when is not inside JSX", () => {
const input = `
const code = `
import { Bar } from "./components" with { type: "prerender" };
export default function Test() {
return <Bar />;
}
`;
const output = normalizeQuotes(transpile(input, currentFile, configPath));
const output = normalizeQuotes(
transpile({ code, path: currentFile, prerenderConfigPath: configPath }),
);
const expected = normalizeQuotes(`
import {Bar} from "./components";
Expand All @@ -109,14 +117,16 @@ describe("Brisa", () => {
});

it("should transform a static component with props", () => {
const input = `
const code = `
import Foo from "./components" with { type: "prerender" };
export default function Test() {
return <Foo name="Brisa" nested={{ foo: ' works' }} />;
}
`;
const output = normalizeQuotes(transpile(input, currentFile, configPath));
const output = normalizeQuotes(
transpile({ code, path: currentFile, prerenderConfigPath: configPath }),
);
const expected = normalizeQuotes(`
import Foo from "./components";
Expand Down
32 changes: 21 additions & 11 deletions examples-and-tests/preact/plugin.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const currentFile = import.meta.url.replace("file://", "");
describe("Preact", () => {
describe("plugin", () => {
it('should not transform if there is not an import attribute with type "prerender"', () => {
const input = `
const code = `
import Foo from "./components";
import { Bar } from "./components";
Expand All @@ -22,13 +22,15 @@ describe("Preact", () => {
);
}
`;
const output = format(transpile(input, currentFile, configPath));
const expected = format(input);
const output = format(
transpile({ code, path: currentFile, prerenderConfigPath: configPath }),
);
const expected = format(code);

expect(output).toBe(expected);
});
it("should transform a static component", () => {
const input = `
const code = `
import Foo from "./components" with { type: "prerender" };
import { Bar } from "./components";
Expand All @@ -41,7 +43,9 @@ describe("Preact", () => {
);
}
`;
const output = format(transpile(input, currentFile, configPath));
const output = format(
transpile({ code, path: currentFile, prerenderConfigPath: configPath }),
);
const expected = format(`
import Foo from "./components";
import {Bar} from "./components";
Expand All @@ -59,7 +63,7 @@ describe("Preact", () => {
});

it("should transform a static component from named export", () => {
const input = `
const code = `
import { Bar } from "./components" with { type: "prerender" };
import Foo from "./components";
Expand All @@ -72,7 +76,9 @@ describe("Preact", () => {
);
}
`;
const output = format(transpile(input, currentFile, configPath));
const output = format(
transpile({ code, path: currentFile, prerenderConfigPath: configPath }),
);
const expected = format(`
import {Bar} from "./components";
import Foo from "./components";
Expand All @@ -91,14 +97,16 @@ describe("Preact", () => {
});

it("should transform a static component when is not inside JSX", () => {
const input = `
const code = `
import { Bar } from "./components" with { type: "prerender" };
export default function Test() {
return <Bar />;
}
`;
const output = format(transpile(input, currentFile, configPath));
const output = format(
transpile({ code, path: currentFile, prerenderConfigPath: configPath }),
);
const expected = format(`
import {Bar} from "./components";
Expand All @@ -111,14 +119,16 @@ describe("Preact", () => {
});

it("should transform a static component with props", () => {
const input = `
const code = `
import Foo from "./components" with { type: "prerender" };
export default function Test() {
return <Foo name="Preact" nested={{ foo: ' works' }} />;
}
`;
const output = format(transpile(input, currentFile, configPath));
const output = format(
transpile({ code, path: currentFile, prerenderConfigPath: configPath }),
);
const expected = format(`
import Foo from "./components";
Expand Down
3 changes: 2 additions & 1 deletion examples-and-tests/react/config.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { renderToString } from "react-dom/server";
import type { Config } from "prerender-macro";

export const prerenderConfig = {
render: async (Component: any, props: any) => {
Expand All @@ -7,4 +8,4 @@ export const prerenderConfig = {
postRender: (htmlString: string) => (
<div dangerouslySetInnerHTML={{ __html: htmlString }} />
),
};
} satisfies Config;
Loading

0 comments on commit 6393ab7

Please sign in to comment.