Skip to content

Commit

Permalink
Merge pull request #13 from preactjs/refactor/migrate-to-prerender-pl…
Browse files Browse the repository at this point in the history
…ugin

refactor: Migrate off of Vike and to `@preact/preset-vite`'s prerender functionality
  • Loading branch information
rschristian authored Jan 2, 2024
2 parents 39b6c2d + 565be4c commit cfe909a
Show file tree
Hide file tree
Showing 21 changed files with 95 additions and 348 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@
"typescript": "^5.0.0"
},
"prettier": "prettier-config-rschristian"
}
}
57 changes: 30 additions & 27 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const brandColor = /** @type {const} */ ([174, 128, 255]);
),
);

const { dir, language, useRouter, useESLint, appType } = await prompts.group(
const { dir, language, useRouter, usePrerender, useESLint } = await prompts.group(
{
dir: () =>
prompts.text({
Expand All @@ -38,15 +38,6 @@ const brandColor = /** @type {const} */ ([174, 128, 255]);
}
},
}),
appType: () =>
prompts.select({
message: 'Project Type:',
initialValue: 'spa',
options: [
{ value: 'spa', label: 'Single Page Application (only client-side)' },
{ value: 'ssg', label: 'Static Site Generation (prerenders pages)' },
],
}),
language: () =>
prompts.select({
message: 'Project language:',
Expand All @@ -56,13 +47,16 @@ const brandColor = /** @type {const} */ ([174, 128, 255]);
{ value: 'ts', label: 'TypeScript' },
],
}),
useRouter: ({ results }) =>
results.appType === 'spa'
? prompts.confirm({
message: 'Use router?',
initialValue: false,
})
: Promise.resolve(false),
useRouter: () =>
prompts.confirm({
message: 'Use router?',
initialValue: false,
}),
usePrerender: () =>
prompts.confirm({
message: 'Prerender app (SSG)?',
initialValue: false,
}),
useESLint: () =>
prompts.confirm({
message: 'Use ESLint?',
Expand All @@ -81,13 +75,13 @@ const brandColor = /** @type {const} */ ([174, 128, 255]);

await useSpinner(
'Setting up your project directory...',
() => scaffold(targetDir, { useTS, useRouter, useESLint, appType }),
() => scaffold(targetDir, { useTS, useRouter, usePrerender, useESLint }),
'Set up project directory',
);

await useSpinner(
'Installing project dependencies...',
() => installDeps(targetDir, packageManager, { useTS, useRouter, useESLint, appType }),
() => installDeps(targetDir, packageManager, { useTS, useRouter, usePrerender, useESLint }),
'Installed project dependencies',
);

Expand Down Expand Up @@ -116,9 +110,9 @@ async function useSpinner(startMessage, fn, finishMessage) {
/**
* @typedef {Object} ConfigOptions
* @property {boolean} useTS
* @property {unknown} useRouter
* @property {boolean} useRouter
* @property {boolean} usePrerender
* @property {boolean} useESLint
* @property {string} appType
*/

/**
Expand All @@ -131,11 +125,7 @@ async function scaffold(to, opts) {
await fs.mkdir(to, { recursive: true });

const __dirname = dirname(fileURLToPath(import.meta.url));
if (opts.appType === 'spa') {
await templateDir(resolve(__dirname, '../templates', 'base'), to, opts.useTS);
} else {
await templateDir(resolve(__dirname, '../templates', 'ssr'), to, opts.useTS);
}
await templateDir(resolve(__dirname, '../templates', 'base'), to, opts.useTS);

if (opts.useRouter) {
await templateDir(
Expand All @@ -145,6 +135,19 @@ async function scaffold(to, opts) {
);
}

if (opts.usePrerender) {
await templateDir(
resolve(
__dirname,
'../templates',
'config',
opts.useRouter ? 'prerender-router' : 'prerender',
),
to,
opts.useTS,
);
}

if (opts.useTS) {
await fs.rename(resolve(to, 'jsconfig.json'), resolve(to, 'tsconfig.json'));

Expand Down Expand Up @@ -203,7 +206,7 @@ async function installDeps(to, packageManager, opts) {
await install(['typescript'], { prefer: packageManager, cwd: to, dev: true });
}

if (opts.useRouter) {
if (opts.useRouter || opts.usePrerender) {
await install(['preact-iso', 'preact-render-to-string'], {
prefer: packageManager,
cwd: to,
Expand Down
1 change: 1 addition & 0 deletions templates/base/src/index.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { render } from 'preact';

import preactLogo from './assets/preact.svg';
import './style.css';

Expand Down
28 changes: 28 additions & 0 deletions templates/config/prerender-router/src/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { LocationProvider, Router, Route, hydrate, prerender as ssr } from 'preact-iso';

import { Header } from './components/Header.jsx';
import { Home } from './pages/Home/index.jsx';
import { NotFound } from './pages/_404.jsx';
import './style.css';

export function App() {
return (
<LocationProvider>
<Header />
<main>
<Router>
<Route path="/" component={Home} />
<Route default component={NotFound} />
</Router>
</main>
</LocationProvider>
);
}

if (typeof window !== 'undefined') {
hydrate(<App />, document.getElementById('app'));
}

export async function prerender(data) {
return await ssr(<App {...data} />);
}
15 changes: 15 additions & 0 deletions templates/config/prerender-router/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { defineConfig } from 'vite';
import preact from '@preact/preset-vite';

// https://vitejs.dev/config/
export default defineConfig({
plugins: [
preact({
prerender: {
enabled: true,
renderTarget: '#app',
additionalPrerenderRoutes: ['/404'],
},
}),
],
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import preactLogo from '../assets/preact.svg';
import { hydrate, prerender as ssr } from 'preact-iso';

import preactLogo from './assets/preact.svg';
import './style.css';

export function Page() {
export function App() {
return (
<div>
<a href="https://preactjs.com" target="_blank">
Expand Down Expand Up @@ -37,3 +39,11 @@ function Resource(props) {
</a>
);
}

if (typeof window !== 'undefined') {
hydrate(<App />, document.getElementById('app'));
}

export async function prerender(data) {
return await ssr(<App {...data} />);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { defineConfig } from 'vite';
import preact from '@preact/preset-vite';
import ssr from 'vike/plugin';

// https://vitejs.dev/config/
export default defineConfig({
plugins: [preact(), ssr({ prerender: true })],
plugins: [
preact({
prerender: {
enabled: true,
renderTarget: '#app',
},
}),
],
});
24 changes: 0 additions & 24 deletions templates/ssr/_gitignore

This file was deleted.

13 changes: 0 additions & 13 deletions templates/ssr/index.html

This file was deleted.

13 changes: 0 additions & 13 deletions templates/ssr/jsconfig.json

This file was deleted.

18 changes: 0 additions & 18 deletions templates/ssr/package.json

This file was deleted.

8 changes: 0 additions & 8 deletions templates/ssr/pages/404.page.jsx

This file was deleted.

1 change: 0 additions & 1 deletion templates/ssr/pages/assets/preact.svg

This file was deleted.

47 changes: 0 additions & 47 deletions templates/ssr/pages/index/style.css

This file was deleted.

1 change: 0 additions & 1 deletion templates/ssr/public/vite.svg

This file was deleted.

Loading

0 comments on commit cfe909a

Please sign in to comment.