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",
"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",
"buld": "npm run compile && node .tmp/ssg/build.js"
"build": "npm run compile && node .tmp/ssg/build.js"
},
"keywords": [],
"author": "",

View File

@ -1,9 +1,7 @@
import { readFile } from 'node:fs/promises';
import matter from 'gray-matter';
import { parse } from 'marked';
import { getPaths, getStylePath } from './utils';
import { build } from 'esbuild';
import { sassPlugin } from 'esbuild-sass-plugin';
import { buildFile, getStylePath } from './utils';
import { format } from 'prettier';
import Root from './root';
import { writeFileSync } from 'node:fs';
@ -11,7 +9,6 @@ import { resolve } from 'path';
import crypto from 'node:crypto';
export const buildBlog = async (blog: string) => {
console.log(`Building ${blog}`);
const fileContent = await readFile(blog, 'utf-8');
const content = matter(fileContent);
const postHtml = parse(content.content);
@ -33,25 +30,7 @@ export const buildBlog = async (blog: string) => {
.replace(content.data.template, blogFileName)
.replace('.tsx', '.html');
await build({
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',
}),
],
});
await buildFile(templatePath, tmpJsPath);
const { default: Page } = await import(tmpJsPath);
const html = Page.default({ children: postHtml });

View File

@ -1,32 +1,12 @@
import { getPaths, getStylePath } from './utils';
import { build } from 'esbuild';
import { sassPlugin } from 'esbuild-sass-plugin';
import { buildFile, getPagePaths, getStylePath } from './utils';
import { format } from 'prettier';
import Root from './root';
import { writeFileSync } from 'node:fs';
export const buildPage = async (page: string) => {
const { pagePath, tmpJsPath, pageOutputHtmlPath } = getPaths(page);
const { pagePath, tmpJsPath, pageOutputHtmlPath } = getPagePaths(page);
await build({
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',
}),
],
});
await buildFile(pagePath, tmpJsPath);
const { default: Page } = await import(tmpJsPath);
const html = Page.default();

View File

@ -1,5 +1,7 @@
import { cpSync, existsSync } from 'node:fs';
import { resolve } from 'path';
import { sassPlugin } from 'esbuild-sass-plugin';
import { build } from 'esbuild';
export const outDir = resolve('.out');
@ -20,7 +22,7 @@ export const getStylePath = (tmpJsPath: string, pageOutputHtmlPath: string) => {
return stylePaths;
};
export const getPaths = (rawPath: string) => {
export const getPagePaths = (rawPath: string) => {
const pagePath = resolve(rawPath);
const tmpJsPath = pagePath.replace('src', '.tmp').replace('.tsx', '.js');
const pageOutputHtmlPath = pagePath
@ -32,3 +34,24 @@ export const getPaths = (rawPath: string) => {
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',
}),
],
});