From 1335632304a0dc7916f7944b21d3372d4eba6e9f Mon Sep 17 00:00:00 2001 From: pfych Date: Sun, 6 Apr 2025 09:48:41 +1000 Subject: [PATCH] Tidy --- package.json | 2 +- ssg/build-blog.tsx | 25 ++----------------------- ssg/build-page.tsx | 26 +++----------------------- ssg/utils.ts | 25 ++++++++++++++++++++++++- 4 files changed, 30 insertions(+), 48 deletions(-) diff --git a/package.json b/package.json index de83e55..a8e9292 100644 --- a/package.json +++ b/package.json @@ -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": "", diff --git a/ssg/build-blog.tsx b/ssg/build-blog.tsx index 81255b0..9ccb2eb 100644 --- a/ssg/build-blog.tsx +++ b/ssg/build-blog.tsx @@ -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 }); diff --git a/ssg/build-page.tsx b/ssg/build-page.tsx index a769c95..9baedc0 100644 --- a/ssg/build-page.tsx +++ b/ssg/build-page.tsx @@ -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(); diff --git a/ssg/utils.ts b/ssg/utils.ts index a6c2431..e0c6400 100644 --- a/ssg/utils.ts +++ b/ssg/utils.ts @@ -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', + }), + ], + });