UNPKG

@37bytes/vite-build-time-environment

Version:

Vite plugin for integrating build-time environment variables like Git branch and commit hash.

52 lines (50 loc) 2.32 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/buildTimeEnvironmentSupport.ts var buildTimeEnvironmentSupport_exports = {}; __export(buildTimeEnvironmentSupport_exports, { default: () => buildTimeEnvironmentSupport_default }); module.exports = __toCommonJS(buildTimeEnvironmentSupport_exports); var buildTimeIdentifiersPlugin = ({ packageName, packageVersion, aliases }) => { if (!packageName) { throw new Error("packageName cannot be falsy"); } if (!packageVersion) { throw new Error("packageVersion cannot be falsy"); } return { name: "@37bytes/vite-build-time-environment", config(config) { const version = process.env[aliases?.VERSION_FIELD_NAME ?? "VERSION"]; const branch = process.env[aliases?.BRANCH_FIELD_NAME ?? "BRANCH"] || "[unknown git branch]"; const commitHash = process.env[aliases?.COMMIT_HASH_FIELD_NAME ?? "COMMIT_HASH"] || "[unknown commit hash]"; const releaseName = `${packageName}@${packageVersion}${version ? `+${version}` : ""}`; config.define = { ...config.define, PACKAGE_INFO_FIELD_NAME: JSON.stringify(packageName), PACKAGE_INFO_FIELD_VERSION: JSON.stringify(packageVersion), BUILD_TIME_VARIABLES_VERSION: JSON.stringify(version || "[unknown build version]"), BUILD_TIME_VARIABLES_BRANCH: JSON.stringify(branch), BUILD_TIME_VARIABLES_COMMIT_HASH: JSON.stringify(commitHash), BUILD_TIME_VARIABLES_RELEASE_NAME: JSON.stringify(releaseName) }; } }; }; var buildTimeEnvironmentSupport_default = buildTimeIdentifiersPlugin;