Skip to content

refactor: Switch to vite-prerender-plugin #153

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

Merged
merged 4 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
55 changes: 36 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"dev:build": "vite build demo",
"dev:preview": "vite preview demo",
"build": "rimraf dist && tsc && tsc -p tsconfig.cjs.json && node tools/postbuild.mjs",
"test": "node --test test",
"test": "rimraf demo/node_modules && node --test",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We create a temp dir in node_modules (node_modules/<@preact/preset-vite|vite-prerender-plugin>/headless-prerender/...) to allow users to use external, non-bundled node_modules whilst prerendering, but that screws with module resolution a tad. The demo will try to pull from that, but given it's just a holding place rather than an actual, installed module, that won't work.

Side effect of being monorepo-ish here.

"prepublishOnly": "npm run build"
},
"keywords": [
Expand All @@ -36,18 +36,14 @@
"dist/"
],
"dependencies": {
"@babel/code-frame": "^7.22.13",
"@babel/plugin-transform-react-jsx": "^7.22.15",
"@babel/plugin-transform-react-jsx-development": "^7.22.5",
"@prefresh/vite": "^2.4.1",
"@rollup/pluginutils": "^4.1.1",
"babel-plugin-transform-hook-names": "^1.0.2",
"debug": "^4.3.4",
"kolorist": "^1.8.0",
"magic-string": "0.30.5",
"node-html-parser": "^6.1.10",
"source-map": "^0.7.4",
"stack-trace": "^1.0.0-pre2"
"vite-prerender-plugin": "^0.5.2"
},
"peerDependencies": {
"@babel/core": "7.x",
Expand Down
21 changes: 12 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import prefresh from "@prefresh/vite";
import { preactDevtoolsPlugin } from "./devtools.js";
import { createFilter, parseId } from "./utils.js";
import { PrerenderPlugin, HTMLRoutingMiddlewarePlugin } from "./prerender.js";
import { vitePrerenderPlugin } from "vite-prerender-plugin";

Check failure on line 9 in src/index.ts

View workflow job for this annotation

GitHub Actions / Test

Cannot find module 'vite-prerender-plugin' or its corresponding type declarations.
import { transformAsync } from "@babel/core";

export type BabelOptions = Omit<
Expand Down Expand Up @@ -145,6 +145,16 @@
reactAliasesEnabled = reactAliasesEnabled ?? true;
prerender = prerender ?? { enabled: false };

const prerenderPlugin = vitePrerenderPlugin(prerender);
if (!prerender.previewMiddlewareEnabled) {
const idx = prerenderPlugin.findIndex(
p => p.name == "serve-prerendered-html",

Check failure on line 151 in src/index.ts

View workflow job for this annotation

GitHub Actions / Test

Parameter 'p' implicitly has an 'any' type.
);
if (idx > -1) {
prerenderPlugin.splice(idx, 1);
}
}

const jsxPlugin: Plugin = {
name: "vite:preact-jsx",
enforce: "pre",
Expand Down Expand Up @@ -275,14 +285,7 @@
...(prefreshEnabled
? [prefresh({ include, exclude, parserPlugins: baseParserOptions })]
: []),
...(prerender.enabled ? [PrerenderPlugin(prerender)] : []),
...(prerender.previewMiddlewareEnabled
? [
HTMLRoutingMiddlewarePlugin({
fallback: prerender.previewMiddlewareFallback,
}),
]
: []),
...(prerender.enabled ? prerenderPlugin : []),
];
}

Expand Down
Loading
Loading