Skip to content

Commit

Permalink
refactor(build): use env vars as constants
Browse files Browse the repository at this point in the history
  • Loading branch information
masonmcelvain committed Oct 15, 2023
1 parent 1bb72ff commit 5d111fc
Showing 1 changed file with 17 additions and 26 deletions.
43 changes: 17 additions & 26 deletions build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof TargetSchema>;
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<typeof NodeEnvSchema>;
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}` });
});
},
});
Expand All @@ -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() {
Expand Down

0 comments on commit 5d111fc

Please sign in to comment.