From 13ac56757ae4a10e47c2bccac5feb24d15c63ab6 Mon Sep 17 00:00:00 2001 From: Tom Herni Date: Sat, 3 May 2025 22:36:29 +0200 Subject: [PATCH] fix: dev and prod builds have different entry points --- package.json | 4 +- src/generator/build-dev.ts | 3 -- src/generator/build-prod.ts | 10 ----- src/generator/build.ts | 86 ++++++++++++++++++------------------- src/generator/constants.ts | 3 ++ src/generator/utils.ts | 22 ++++++++++ 6 files changed, 70 insertions(+), 58 deletions(-) delete mode 100644 src/generator/build-dev.ts delete mode 100644 src/generator/build-prod.ts diff --git a/package.json b/package.json index 8aee943..736d787 100644 --- a/package.json +++ b/package.json @@ -10,8 +10,8 @@ }, "scripts": { "start": "tsx scripts/dev-server.ts", - "build": "tsc -noEmit && tsx src/generator/build-prod.ts", - "build:dev": "tsx src/generator/build-dev.ts", + "build": "tsc -noEmit && tsx src/generator/build.ts", + "build:dev": "tsx src/generator/build.ts", "lint": "eslint \"**/*.{js,ts}\" --fix && stylelint \"src/**/*.css\" --fix", "lint:check": "eslint \"**/*.{js,ts}\" && stylelint \"src/**/*.css\"", "format": "prettier \"**/*.{css,html,js,json,ts}\" --write --ignore-path .gitignore", diff --git a/src/generator/build-dev.ts b/src/generator/build-dev.ts deleted file mode 100644 index e2424c7..0000000 --- a/src/generator/build-dev.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { build } from './build'; - -await build({ env: 'DEV', baseUrl: 'http://localhost:3000/' }); diff --git a/src/generator/build-prod.ts b/src/generator/build-prod.ts deleted file mode 100644 index 8c8311e..0000000 --- a/src/generator/build-prod.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { build } from './build'; - -// Test: verifying envs in Netlify deploy preview and prod builds -console.log('NODE_ENV:', process.env.NODE_ENV); -console.log('CONTEXT:', process.env.CONTEXT); -console.log('DEPLOY_PRIME_URL:', process.env.DEPLOY_PRIME_URL); -console.log('REVIEW_ID:', process.env.REVIEW_ID); -console.log('URL:', process.env.URL); - -await build({ env: 'PROD', baseUrl: 'https://tomherni.dev/' }); diff --git a/src/generator/build.ts b/src/generator/build.ts index 8b1b8e6..bb014b9 100644 --- a/src/generator/build.ts +++ b/src/generator/build.ts @@ -1,68 +1,68 @@ import fs from 'node:fs'; -import path from 'node:path'; import { siteConfig } from '../config'; import { getPosts } from './get-posts/get-posts'; import { createAtomFeed } from './feeds/create-atom-feed'; import { createRssFeed } from './feeds/create-rss-feed'; import { createSitemap } from './feeds/create-sitemap'; import { renderPages } from './render-pages/render-pages'; -import { DIR_DIST, DIR_SRC_PUBLIC } from './constants'; +import { + BASE_URL_DEV, + BASE_URL_PROD, + DIR_DIST, + DIR_SRC_PUBLIC, +} from './constants'; import { optimize } from './optimize/optimize'; import { setState } from './state'; +import { copySourceToTarget } from './utils'; type PartialBuildConfig = { env: 'DEV' | 'PROD'; baseUrl: string; }; -export async function build(buildConfig: PartialBuildConfig): Promise { - const config = { - config: siteConfig, - build: { ...buildConfig, buildDate: new Date() }, - }; - - // Prepare the DIST directory. - fs.rmSync(DIR_DIST, { recursive: true, force: true }); - fs.mkdirSync(DIR_DIST); +const config = { + config: siteConfig, + build: { ...getDeployInfo(), buildDate: new Date() }, +}; - // Copy the content to DIST. - copySourceToTarget(DIR_SRC_PUBLIC, DIR_DIST); +// Prepare the DIST directory. +fs.rmSync(DIR_DIST, { recursive: true, force: true }); +fs.mkdirSync(DIR_DIST); - // Prepare the site data object to be able to create HTML pages. - const posts = getPosts(config.build); - const tags = [...new Set(posts.map((post) => post.meta.tags || []).flat())]; - setState({ ...config, posts, tags }); +// Copy the content to DIST. +copySourceToTarget(DIR_SRC_PUBLIC, DIR_DIST); - // Create the HTML pages. - const pages = await renderPages(); +// Prepare the site data object to be able to create HTML pages. +const posts = getPosts(config.build); +const tags = [...new Set(posts.map((post) => post.meta.tags || []).flat())]; +setState({ ...config, posts, tags }); - // Optimize HTML pages and assets. - await optimize(); +// Create the HTML pages. +const pages = await renderPages(); - // Create feeds. - createAtomFeed(); - createRssFeed(); - createSitemap(pages); -} +// Optimize HTML pages and assets. +await optimize(); -/** - * Copy the source directory to the target directory. - */ -function copySourceToTarget(source: string, target: string): void { - if (!fs.existsSync(target)) { - fs.mkdirSync(target); - } +// Create feeds. +createAtomFeed(); +createRssFeed(); +createSitemap(pages); - const entries = fs.readdirSync(source, { withFileTypes: true }); +function getDeployInfo(): PartialBuildConfig { + const { CONTEXT, DEPLOY_PRIME_URL } = process.env; - for (const entry of entries) { - const sourcePath = path.join(source, entry.name); - const targetPath = path.join(target, entry.name); - - if (entry.isDirectory()) { - copySourceToTarget(sourcePath, targetPath); - } else if (!['.md', '.ts'].includes(path.extname(sourcePath))) { - fs.copyFileSync(sourcePath, targetPath); - } + // Detect Netlify's Deploy Preview deployment. + if (CONTEXT === 'deploy-preview' && DEPLOY_PRIME_URL) { + return { baseUrl: DEPLOY_PRIME_URL, env: 'PROD' }; + } + // Detect Netlify's production deployment. + if (CONTEXT === 'production') { + return { baseUrl: BASE_URL_PROD, env: 'PROD' }; + } + // Ensure the build is not running in an unexpected Netlify environment. + // The base URL would need to be verified first. + if (typeof CONTEXT !== 'undefined') { + throw new Error('Unknown build environment'); } + return { baseUrl: BASE_URL_DEV, env: 'DEV' }; } diff --git a/src/generator/constants.ts b/src/generator/constants.ts index 49c4178..6ffbd04 100644 --- a/src/generator/constants.ts +++ b/src/generator/constants.ts @@ -1,5 +1,8 @@ import path from 'node:path'; +export const BASE_URL_DEV = 'http://localhost:3000/'; +export const BASE_URL_PROD = 'https://tomherni.dev/'; + export const DIR_DIST = path.resolve('dist/'); export const DIR_SRC_LAYOUTS = path.resolve('src/includes/layouts/'); export const DIR_SRC_PUBLIC = path.resolve('src/public/'); diff --git a/src/generator/utils.ts b/src/generator/utils.ts index 528b8db..5e9fb11 100644 --- a/src/generator/utils.ts +++ b/src/generator/utils.ts @@ -39,6 +39,28 @@ export function createFile(file: string, content: string): void { fs.writeFileSync(file, content); } +/** + * Copy the source directory to the target directory. + */ +export function copySourceToTarget(source: string, target: string): void { + if (!fs.existsSync(target)) { + fs.mkdirSync(target); + } + + const entries = fs.readdirSync(source, { withFileTypes: true }); + + for (const entry of entries) { + const sourcePath = path.join(source, entry.name); + const targetPath = path.join(target, entry.name); + + if (entry.isDirectory()) { + copySourceToTarget(sourcePath, targetPath); + } else if (!['.md', '.ts'].includes(path.extname(sourcePath))) { + fs.copyFileSync(sourcePath, targetPath); + } + } +} + /** * Deep find all files in a directory by file extension. */