@astrapi69/vite-plugin-build-version
v0.1.0
Published
Vite plugin that emits the version.json build manifest and defines the build-time version/hash/date literals consumed by @astrapi69/pwa-update
Maintainers
Readme
@astrapi69/vite-plugin-build-version
The build half of the PWA update kit: emits the version.json manifest next
to your bundle and defines the build-time literals the runtime compares
against.
npm install -D @astrapi69/vite-plugin-build-versionUsage
import { defineConfig } from "vite";
import { buildVersion } from "@astrapi69/vite-plugin-build-version";
import pkg from "./package.json" with { type: "json" };
export default defineConfig({
plugins: [
buildVersion({
version: pkg.version,
buildHash: process.env.VITE_BUILD_HASH,
buildDate: process.env.VITE_BUILD_DATE,
}),
],
});Produces:
dist/version.json→{"version": "...", "buildHash": "...", "buildDate": "..."}__APP_VERSION__,__BUILD_HASH__,__BUILD_DATE__as build-time literals
Declare the literals for TypeScript:
// vite-env.d.ts
declare const __APP_VERSION__: string;
declare const __BUILD_HASH__: string;
declare const __BUILD_DATE__: string;Extra literals (a deploy channel, a branch) go through extraDefines.
Keep the manifest out of your precache
With vite-plugin-pwa / Workbox, do not include json in
workbox.globPatterns. The manifest must always be fetched fresh — a
precached version.json reports the build it shipped with, forever.
Porting to another bundler
The runtime package is build-tool agnostic. Reimplementing this plugin is the whole port:
- Write
{version, buildHash, buildDate}toversion.jsonin the output dir. - Define the three literals (or pass the values into
createUpdateStoredirectly from anywhere else).
License
MIT © Asterios Raptis
