Skip to content
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

chore(templates): add SSG template #1

Merged
merged 5 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
24 changes: 19 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const brandColor = /** @type {const} */ ([174, 128, 255]);
kl.trueColor(...brandColor)('Preact - Fast 3kB alternative to React with the same modern API')
);

const { dir, language, useRouter, useESLint } = await prompts.group(
const { dir, language, useRouter, useESLint, type } = await prompts.group(
{
dir: () =>
prompts.text({
Expand All @@ -36,6 +36,14 @@ const brandColor = /** @type {const} */ ([174, 128, 255]);
}
},
}),
type: () => prompts.select({
JoviDeCroock marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -45,11 +53,11 @@ const brandColor = /** @type {const} */ ([174, 128, 255]);
{ value: 'ts', label: 'TypeScript' },
],
}),
useRouter: () =>
useRouter: ({ results }) => results.type === 'spa' ?
prompts.confirm({
message: 'Use router?',
initialValue: false,
}),
}) : new Promise(res => res(false)),
useESLint: () =>
prompts.confirm({
message: 'Use ESLint?',
Expand All @@ -68,7 +76,7 @@ const brandColor = /** @type {const} */ ([174, 128, 255]);

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

Expand Down Expand Up @@ -105,6 +113,7 @@ async function useSpinner(startMessage, fn, finishMessage) {
* @property {boolean} useTS
* @property {boolean} useRouter
* @property {boolean} useESLint
* @property {"spa" | "ssg"} type
*/

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

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

if (opts.useRouter) {
await templateDir(
Expand All @@ -132,6 +145,7 @@ async function scaffold(to, opts) {

const htmlPath = resolve(to, 'index.html');
const html = (await fs.readFile(htmlPath, 'utf-8')).replace('index.jsx', 'index.tsx');
// TODO: should we also rename all current jsx files to tsx?
JoviDeCroock marked this conversation as resolved.
Show resolved Hide resolved
return await fs.writeFile(htmlPath, html);
}

Expand Down
24 changes: 24 additions & 0 deletions templates/ssr/_gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
13 changes: 13 additions & 0 deletions templates/ssr/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + Preact</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/index.jsx"></script>
</body>
</html>
13 changes: 13 additions & 0 deletions templates/ssr/jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"moduleResolution": "bundler",
"noEmit": true,
"allowJs": true,
"checkJs": true,
"jsx": "react-jsx",
"jsxImportSource": "preact"
},
"include": ["node_modules/vite/client.d.ts", "**/*"]
}
Loading