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() {