-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: run define commands async and gzip bundle
- Loading branch information
Showing
5 changed files
with
79 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
"type": "module", | ||
"scripts": { | ||
"prepare": "husky", | ||
"dev": "vite build --watch", | ||
"build": "vite build", | ||
"build:tsc": "tsc && vite build", | ||
"typecheck": "tsc", | ||
|
@@ -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" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}), | ||
}), | ||
); |