Skip to content

Commit

Permalink
build: run define commands async and gzip bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
jcwillox committed Jul 28, 2024
1 parent 8ef6e66 commit 5573bb6
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 34 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"type": "module",
"scripts": {
"prepare": "husky",
"dev": "vite build --watch",
"build": "vite build",
"build:tsc": "tsc && vite build",
"typecheck": "tsc",
Expand Down Expand Up @@ -38,10 +39,11 @@
"lint-staged": "15.2.7",
"prettier": "3.3.3",
"typescript": "5.5.4",
"vite": "5.3.4"
"vite": "5.3.4",
"vite-plugin-compression": "0.5.1"
},
"packageManager": "[email protected]",
"engines": {
"node": ">=16.x"
"node": ">=20.x"
}
}
42 changes: 42 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/canary-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class CanaryCard extends LitElement implements LovelaceCard {
console.info(`branch : ${__BRANCH__}`);
console.info(`commit : ${__COMMIT__}`);
console.info(`built at : ${__BUILD_TIME__}`);
console.info(__REPO_URL__);
console.info("https://github.com/jcwillox/lovelace-canary");
console.groupEnd();
}
})();
1 change: 0 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ declare global {
const __BRANCH__: string;
const __COMMIT__: string;
const __VERSION__: string;
const __REPO_URL__: string;
const __BUILD_TIME__: string;
}

Expand Down
62 changes: 32 additions & 30 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
// https://vitejs.dev/config/
import { execSync } from "child_process";
import { defineConfig } from "vite";
import { exec } from "node:child_process";
import { promisify } from "node:util";
import { UserConfig, defineConfig } from "vite";
import viteCompression from "vite-plugin-compression";
import pkg from "./package.json";

const quoteCommand = command => {
return JSON.stringify(execSync(command).toString().trim());
};
const $ = async (command: string, env = "") =>
process.env[env] ?? (await promisify(exec)(command)).stdout.trim();

const quoteCommandOrEnv = (command, env) => {
if (process.env[env]) {
return JSON.stringify(process.env[env]);
}
return quoteCommand(command);
};
const all = async (obj: Record<string, string | Promise<string>>) =>
Object.fromEntries(
await Promise.all(
Object.entries(obj).map(async ([k, v]) => [k, JSON.stringify(await v)]),
),
);

export default defineConfig({
build: {
lib: {
entry: "src/main.ts",
formats: ["es"],
export default defineConfig(
async (): Promise<UserConfig> => ({
build: {
lib: {
entry: "src/main.ts",
formats: ["es"],
},
},
},
esbuild: {
legalComments: "none",
},
define: {
__NAME__: JSON.stringify(pkg.name.toUpperCase()),
__BRANCH__: quoteCommand("git rev-parse --abbrev-ref HEAD"),
__COMMIT__: quoteCommandOrEnv("git rev-parse HEAD", "GITHUB_SHA"),
__VERSION__: quoteCommand("git describe --tags --dirty --always"),
__REPO_URL__: quoteCommand("git remote get-url origin").replace(".git", ""),
__BUILD_TIME__: JSON.stringify(new Date().toISOString()),
},
});
esbuild: {
legalComments: "none",
},
plugins: [viteCompression({ verbose: false })],
define: await all({
__NAME__: pkg.name.toUpperCase(),
__BRANCH__: $("git rev-parse --abbrev-ref HEAD", "GITHUB_REF_NAME"),
__VERSION__: $("git describe --tags --dirty --always", "VERSION"),
__COMMIT__: $("git rev-parse HEAD", "GITHUB_SHA"),
__BUILD_TIME__: new Date().toISOString(),
}),
}),
);

0 comments on commit 5573bb6

Please sign in to comment.