@sexnine/vite-plugin-build-const
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

vite-plugin-build-const

Vite plugin to use build-time constants in your code.

Create a file ending with .const.js and import it in your code and all the exports will be evaluated at build time and replaced with their values.

Uses devalue under the hood.

Example Usage

vite.config.ts

import { sveltekit } from "@sveltejs/kit/vite";
import { defineConfig } from "vite";
import buildConst from "@sexnine/vite-plugin-build-const";

export default defineConfig({
    plugins: [
        sveltekit(),
        buildConst(),
    ],
});

src/lib/build-info.const.js

// All of this code will be run once at build time and replaced with their resulting values
import { execSync } from "node:child_process";
import { readFileSync } from "node:fs";

const prod = process.env.NODE_ENV === "production";

export const commitHash = prod ? execSync("git rev-parse HEAD").toString().trim() : "dev";
export const buildDate = new Date();
export const packageVersion =
    JSON.parse(readFileSync("package.json").toString()).version ?? "unknown";

src/routes/+page.svelte

<script lang="ts">
    import { commitHash, buildDate, packageVersion } from "$lib/build-info.const";
</script>

<p>commit hash: {commitHash}</p>
<p>build date: {buildDate.toLocaleString()}</p>
<p>package version: {packageVersion}</p>

Readme

Keywords

none

Package Sidebar

Install

npm i @sexnine/vite-plugin-build-const

Weekly Downloads

0

Version

1.0.3

License

MIT

Unpacked Size

7.29 kB

Total Files

7

Last publish

Collaborators

  • sexnine