vite-plugin-runtime-env
Version:
Vite plugin which enables you to configure your environment variables when deploying your app.
105 lines (104 loc) • 3 kB
JavaScript
// src/index.ts
import MagicString from "magic-string";
var viteInternalEnvs = ["MODE", "BASE_URL", "PROD", "DEV", "SSR"];
function createNameFilter(envPrefix, ignoreEnv) {
const hasPrefix = (name) => {
if (typeof envPrefix === "string") {
return name.startsWith(envPrefix);
}
return envPrefix.some((prefix) => name.startsWith(prefix));
};
const shouldBeIgnored = (name) => {
if (viteInternalEnvs.includes(name)) {
return true;
}
if (!ignoreEnv) {
return false;
}
if (typeof ignoreEnv === "function") {
return ignoreEnv(name);
}
return ignoreEnv.includes(name);
};
return (name) => hasPrefix(name) && !shouldBeIgnored(name);
}
function createVariableDecorator(substitutionSyntax = "dollar-curly") {
return (name) => {
switch (substitutionSyntax) {
case "dollar-basic":
return `$${name}`;
case "handlebars":
return `{{${name}}}`;
case "dollar-curly":
default:
return `\${${name}}`;
}
};
}
function runtimeEnv(options = {}) {
const matches = /* @__PURE__ */ new Set();
let envPrefix = "VITE_";
let needSourceMap = false;
let isDevServer = false;
const {
variableName = "window.env",
injectHtml = true
} = options;
const shouldReplaceName = createNameFilter(envPrefix, options.ignoreEnv);
const wrapVariable = createVariableDecorator(options.substitutionSyntax);
return {
name: "runtime-env",
configResolved: (resolvedConfig) => {
if (resolvedConfig.envPrefix) {
envPrefix = resolvedConfig.envPrefix;
}
if (resolvedConfig.build.sourcemap) {
needSourceMap = true;
}
if (resolvedConfig.command === "serve") {
isDevServer = true;
}
},
transform: (code) => {
if (isDevServer) {
return null;
}
const magicString = new MagicString(code);
const importMetaPattern = /import\.meta\.env\.([A-Z0-9_]+)/g;
let match;
while (match = importMetaPattern.exec(code)) {
const start = match.index;
const end = start + match[0].length;
const name = match[1];
if (shouldReplaceName(name)) {
magicString.overwrite(start, end, `${variableName}.${name}`);
matches.add(name);
}
}
if (!magicString.hasChanged()) {
return null;
}
if (!needSourceMap) {
return magicString.toString();
}
return {
code: magicString.toString(),
map: magicString.generateMap({ hires: true })
};
},
transformIndexHtml: () => {
if (isDevServer || !injectHtml || matches.size === 0) {
return void 0;
}
const vars = [...matches.values()].map((name) => `"${name}":"${wrapVariable(name)}"`).join(",");
return [{
tag: "script",
attrs: { type: "application/javascript" },
children: `${variableName}=JSON.parse('{${vars}}');`
}];
}
};
}
export {
runtimeEnv as default
};