From 5d111fcfebdb0314ce11b74e3bfb2cd3d6b33d72 Mon Sep 17 00:00:00 2001 From: Mason McElvain <52104630+masonmcelvain@users.noreply.github.com> Date: Sun, 15 Oct 2023 10:52:56 -0700 Subject: [PATCH] refactor(build): use env vars as constants --- build.ts | 43 +++++++++++++++++-------------------------- 1 file changed, 17 insertions(+), 26 deletions(-) diff --git a/build.ts b/build.ts index 99a55d92..6450d6d3 100755 --- a/build.ts +++ b/build.ts @@ -5,56 +5,47 @@ import * as esbuild from "esbuild"; import { z } from "zod"; const TargetSchema = z.union([z.literal("chrome"), z.literal("firefox")]); -type Target = z.infer; -const target = TargetSchema.parse(process.env.TARGET); +const TARGET = TargetSchema.parse(process.env.TARGET); const NodeEnvSchema = z.union([ z.literal("development"), z.literal("production"), ]); -type NodeEnv = z.infer; -const nodeEnv = NodeEnvSchema.parse(process.env.NODE_ENV); +const NODE_ENV = NodeEnvSchema.parse(process.env.NODE_ENV); -const cleanupPlugin = ({ target }: { target: Target }): esbuild.Plugin => ({ +const cleanupPlugin = (): esbuild.Plugin => ({ name: "cleanup", setup() { - execSync(`rm -rf dist/${target}`); - execSync(`rm -f dist/${target}.zip`); + execSync(`rm -rf dist/${TARGET}`); + execSync(`rm -f dist/${TARGET}.zip`); }, }); -const copyAssetsPlugin = ({ target }: { target: Target }): esbuild.Plugin => ({ +const copyAssetsPlugin = (): esbuild.Plugin => ({ name: "copy-assets", setup(build) { build.onEnd(() => { - execSync(`cp -r src/public/${target}/* dist/${target}`); + execSync(`cp -r src/public/${TARGET}/* dist/${TARGET}`); }); }, }); -type TailwindPluginProps = { - target: Target; - nodeEnv: NodeEnv; -}; -const tailwindPlugin = ({ - target, - nodeEnv, -}: TailwindPluginProps): esbuild.Plugin => ({ +const tailwindPlugin = (): esbuild.Plugin => ({ name: "tailwind", setup(build) { build.onStart(() => { - const minify = nodeEnv === "production" ? "--minify" : ""; + const minify = NODE_ENV === "production" ? "--minify" : ""; execSync( - `tailwindcss -i src/globals.css -o dist/${target}/output.css ${minify}`, + `tailwindcss -i src/globals.css -o dist/${TARGET}/output.css ${minify}`, ); }); }, }); -const zipPlugin = ({ target }: { target: Target }): esbuild.Plugin => ({ +const zipPlugin = (): esbuild.Plugin => ({ name: "zip", setup(build) { build.onEnd(() => { - execSync(`zip -r ../${target}.zip *`, { cwd: `dist/${target}` }); + execSync(`zip -r ../${TARGET}.zip *`, { cwd: `dist/${TARGET}` }); }); }, }); @@ -66,13 +57,13 @@ const baseOptions: esbuild.BuildOptions = { ".html": "copy", }, plugins: [ - cleanupPlugin({ target }), - copyAssetsPlugin({ target }), - tailwindPlugin({ target, nodeEnv }), - zipPlugin({ target }), + cleanupPlugin(), + copyAssetsPlugin(), + tailwindPlugin(), + zipPlugin(), ], target: ["chrome58", "firefox57"], - outdir: `dist/${target}`, + outdir: `dist/${TARGET}`, }; async function build() {