🎉 initiate project *astro_rewrite*
This commit is contained in:
parent
ffd4d5e86c
commit
2ba37bfbe3
8658 changed files with 2268794 additions and 2538 deletions
7
node_modules/astro/dist/vite-plugin-env/index.d.ts
generated
vendored
Normal file
7
node_modules/astro/dist/vite-plugin-env/index.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
import type * as vite from 'vite';
|
||||
import type { AstroSettings } from '../@types/astro';
|
||||
interface EnvPluginOptions {
|
||||
settings: AstroSettings;
|
||||
}
|
||||
export default function envVitePlugin({ settings }: EnvPluginOptions): vite.PluginOption;
|
||||
export {};
|
109
node_modules/astro/dist/vite-plugin-env/index.js
generated
vendored
Normal file
109
node_modules/astro/dist/vite-plugin-env/index.js
generated
vendored
Normal file
|
@ -0,0 +1,109 @@
|
|||
import MagicString from "magic-string";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { loadEnv } from "vite";
|
||||
function getPrivateEnv(viteConfig, astroConfig) {
|
||||
let envPrefixes = ["PUBLIC_"];
|
||||
if (viteConfig.envPrefix) {
|
||||
envPrefixes = Array.isArray(viteConfig.envPrefix) ? viteConfig.envPrefix : [viteConfig.envPrefix];
|
||||
}
|
||||
const fullEnv = loadEnv(
|
||||
viteConfig.mode,
|
||||
viteConfig.envDir ?? fileURLToPath(astroConfig.root),
|
||||
""
|
||||
);
|
||||
const privateEnv = {};
|
||||
for (const key in fullEnv) {
|
||||
if (envPrefixes.every((prefix) => !key.startsWith(prefix))) {
|
||||
if (typeof process.env[key] !== "undefined") {
|
||||
let value = process.env[key];
|
||||
if (typeof value !== "string") {
|
||||
value = `${value}`;
|
||||
}
|
||||
if (value === "0" || value === "1" || value === "true" || value === "false") {
|
||||
privateEnv[key] = value;
|
||||
} else {
|
||||
privateEnv[key] = `process.env.${key}`;
|
||||
}
|
||||
} else {
|
||||
privateEnv[key] = JSON.stringify(fullEnv[key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
privateEnv.SITE = astroConfig.site ? JSON.stringify(astroConfig.site) : "undefined";
|
||||
privateEnv.SSR = JSON.stringify(true);
|
||||
privateEnv.BASE_URL = astroConfig.base ? JSON.stringify(astroConfig.base) : "undefined";
|
||||
privateEnv.ASSETS_PREFIX = astroConfig.build.assetsPrefix ? JSON.stringify(astroConfig.build.assetsPrefix) : "undefined";
|
||||
return privateEnv;
|
||||
}
|
||||
function getReferencedPrivateKeys(source, privateEnv) {
|
||||
const references = /* @__PURE__ */ new Set();
|
||||
for (const key in privateEnv) {
|
||||
if (source.includes(key)) {
|
||||
references.add(key);
|
||||
}
|
||||
}
|
||||
return references;
|
||||
}
|
||||
function envVitePlugin({ settings }) {
|
||||
let privateEnv;
|
||||
let viteConfig;
|
||||
const { config: astroConfig } = settings;
|
||||
return {
|
||||
name: "astro:vite-plugin-env",
|
||||
enforce: "pre",
|
||||
config() {
|
||||
return {
|
||||
define: {
|
||||
"import.meta.env.BASE_URL": astroConfig.base ? JSON.stringify(astroConfig.base) : "undefined",
|
||||
"import.meta.env.ASSETS_PREFIX": astroConfig.build.assetsPrefix ? JSON.stringify(astroConfig.build.assetsPrefix) : "undefined"
|
||||
}
|
||||
};
|
||||
},
|
||||
configResolved(resolvedConfig) {
|
||||
viteConfig = resolvedConfig;
|
||||
},
|
||||
async transform(source, id, options) {
|
||||
if (!(options == null ? void 0 : options.ssr) || !source.includes("import.meta.env")) {
|
||||
return;
|
||||
}
|
||||
let s;
|
||||
const pattern = new RegExp(
|
||||
// Do not allow preceding '.', but do allow preceding '...' for spread operations
|
||||
`(?<!(?<!\\.\\.)\\.)\\b(import\\.meta\\.env\\.(.+?)|import\\.meta\\.env)\\b(?!\\s*?=[^=])`,
|
||||
"g"
|
||||
);
|
||||
let references;
|
||||
let match;
|
||||
while (match = pattern.exec(source)) {
|
||||
let replacement;
|
||||
if (match[0] === "import.meta.env") {
|
||||
privateEnv ??= getPrivateEnv(viteConfig, astroConfig);
|
||||
references ??= getReferencedPrivateKeys(source, privateEnv);
|
||||
replacement = `(Object.assign(import.meta.env,{`;
|
||||
for (const key of references.values()) {
|
||||
replacement += `${key}:${privateEnv[key]},`;
|
||||
}
|
||||
replacement += "}))";
|
||||
} else if (match[2]) {
|
||||
privateEnv ??= getPrivateEnv(viteConfig, astroConfig);
|
||||
replacement = privateEnv[match[2]];
|
||||
}
|
||||
if (replacement) {
|
||||
const start = match.index;
|
||||
const end = start + match[0].length;
|
||||
s ??= new MagicString(source);
|
||||
s.overwrite(start, end, replacement);
|
||||
}
|
||||
}
|
||||
if (s) {
|
||||
return {
|
||||
code: s.toString(),
|
||||
map: s.generateMap({ hires: true })
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
export {
|
||||
envVitePlugin as default
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue