This commit is contained in:
pfych 2025-04-06 09:48:41 +10:00
parent c473ed5412
commit 1335632304
4 changed files with 30 additions and 48 deletions

View File

@ -5,7 +5,7 @@
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"compile": "esbuild ssg/build.tsx --bundle --outfile=.tmp/ssg/build.js --jsx-import-source=@kitajs/html --minify --sourcemap --platform=node --external:esbuild --external:prettier", "compile": "esbuild ssg/build.tsx --bundle --outfile=.tmp/ssg/build.js --jsx-import-source=@kitajs/html --minify --sourcemap --platform=node --external:esbuild --external:prettier",
"buld": "npm run compile && node .tmp/ssg/build.js" "build": "npm run compile && node .tmp/ssg/build.js"
}, },
"keywords": [], "keywords": [],
"author": "", "author": "",

View File

@ -1,9 +1,7 @@
import { readFile } from 'node:fs/promises'; import { readFile } from 'node:fs/promises';
import matter from 'gray-matter'; import matter from 'gray-matter';
import { parse } from 'marked'; import { parse } from 'marked';
import { getPaths, getStylePath } from './utils'; import { buildFile, getStylePath } from './utils';
import { build } from 'esbuild';
import { sassPlugin } from 'esbuild-sass-plugin';
import { format } from 'prettier'; import { format } from 'prettier';
import Root from './root'; import Root from './root';
import { writeFileSync } from 'node:fs'; import { writeFileSync } from 'node:fs';
@ -11,7 +9,6 @@ import { resolve } from 'path';
import crypto from 'node:crypto'; import crypto from 'node:crypto';
export const buildBlog = async (blog: string) => { export const buildBlog = async (blog: string) => {
console.log(`Building ${blog}`);
const fileContent = await readFile(blog, 'utf-8'); const fileContent = await readFile(blog, 'utf-8');
const content = matter(fileContent); const content = matter(fileContent);
const postHtml = parse(content.content); const postHtml = parse(content.content);
@ -33,25 +30,7 @@ export const buildBlog = async (blog: string) => {
.replace(content.data.template, blogFileName) .replace(content.data.template, blogFileName)
.replace('.tsx', '.html'); .replace('.tsx', '.html');
await build({ await buildFile(templatePath, tmpJsPath);
entryPoints: [templatePath],
bundle: true,
outfile: tmpJsPath,
jsxImportSource: '@kitajs/html',
minify: true,
platform: 'node',
external: ['esbuild'],
plugins: [
sassPlugin({
filter: /\.module\.scss$/,
type: 'local-css',
}),
sassPlugin({
filter: /\.scss$/,
type: 'css',
}),
],
});
const { default: Page } = await import(tmpJsPath); const { default: Page } = await import(tmpJsPath);
const html = Page.default({ children: postHtml }); const html = Page.default({ children: postHtml });

View File

@ -1,32 +1,12 @@
import { getPaths, getStylePath } from './utils'; import { buildFile, getPagePaths, getStylePath } from './utils';
import { build } from 'esbuild';
import { sassPlugin } from 'esbuild-sass-plugin';
import { format } from 'prettier'; import { format } from 'prettier';
import Root from './root'; import Root from './root';
import { writeFileSync } from 'node:fs'; import { writeFileSync } from 'node:fs';
export const buildPage = async (page: string) => { export const buildPage = async (page: string) => {
const { pagePath, tmpJsPath, pageOutputHtmlPath } = getPaths(page); const { pagePath, tmpJsPath, pageOutputHtmlPath } = getPagePaths(page);
await build({ await buildFile(pagePath, tmpJsPath);
entryPoints: [pagePath],
bundle: true,
outfile: tmpJsPath,
jsxImportSource: '@kitajs/html',
minify: true,
platform: 'node',
external: ['esbuild'],
plugins: [
sassPlugin({
filter: /\.module\.scss$/,
type: 'local-css',
}),
sassPlugin({
filter: /\.scss$/,
type: 'css',
}),
],
});
const { default: Page } = await import(tmpJsPath); const { default: Page } = await import(tmpJsPath);
const html = Page.default(); const html = Page.default();

View File

@ -1,5 +1,7 @@
import { cpSync, existsSync } from 'node:fs'; import { cpSync, existsSync } from 'node:fs';
import { resolve } from 'path'; import { resolve } from 'path';
import { sassPlugin } from 'esbuild-sass-plugin';
import { build } from 'esbuild';
export const outDir = resolve('.out'); export const outDir = resolve('.out');
@ -20,7 +22,7 @@ export const getStylePath = (tmpJsPath: string, pageOutputHtmlPath: string) => {
return stylePaths; return stylePaths;
}; };
export const getPaths = (rawPath: string) => { export const getPagePaths = (rawPath: string) => {
const pagePath = resolve(rawPath); const pagePath = resolve(rawPath);
const tmpJsPath = pagePath.replace('src', '.tmp').replace('.tsx', '.js'); const tmpJsPath = pagePath.replace('src', '.tmp').replace('.tsx', '.js');
const pageOutputHtmlPath = pagePath const pageOutputHtmlPath = pagePath
@ -32,3 +34,24 @@ export const getPaths = (rawPath: string) => {
return { pagePath, tmpJsPath, pageOutputHtmlPath, blogOutputHtmlPath }; return { pagePath, tmpJsPath, pageOutputHtmlPath, blogOutputHtmlPath };
}; };
export const buildFile = async (input: string, output: string) =>
await build({
entryPoints: [input],
bundle: true,
outfile: output,
jsxImportSource: '@kitajs/html',
minify: true,
platform: 'node',
external: ['esbuild'],
plugins: [
sassPlugin({
filter: /\.module\.scss$/,
type: 'local-css',
}),
sassPlugin({
filter: /\.scss$/,
type: 'css',
}),
],
});