mirror of
https://github.com/withastro/astro.git
synced 2025-03-03 22:57:08 -05:00
Move remaining tests to the static build (#2712)
* Move lit test to the static build * Migrate astro-env plugin to work in the static build * Do not remove vite:define * Adds a changeset
This commit is contained in:
parent
556e51eecc
commit
28f54182e4
4 changed files with 46 additions and 27 deletions
.changeset
packages/astro
5
.changeset/fresh-ladybugs-think.md
Normal file
5
.changeset/fresh-ladybugs-think.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Fixes use of private .env variables with the static build
|
|
@ -1,5 +1,6 @@
|
|||
import type * as vite from 'vite';
|
||||
import type { AstroConfig } from '../@types/astro';
|
||||
import type { TransformPluginContext } from 'rollup';
|
||||
import MagicString from 'magic-string';
|
||||
import { fileURLToPath } from 'url';
|
||||
import { loadEnv } from 'vite';
|
||||
|
@ -30,7 +31,7 @@ function getPrivateEnv(viteConfig: vite.ResolvedConfig, astroConfig: AstroConfig
|
|||
if (privateKeys.length === 0) {
|
||||
return null;
|
||||
}
|
||||
return Object.fromEntries(privateKeys.map((key) => [key, fullEnv[key]]));
|
||||
return Object.fromEntries(privateKeys.map((key) => [key, JSON.stringify(fullEnv[key])]));
|
||||
}
|
||||
|
||||
function referencesPrivateKey(source: string, privateEnv: Record<string, any>) {
|
||||
|
@ -43,39 +44,56 @@ function referencesPrivateKey(source: string, privateEnv: Record<string, any>) {
|
|||
export default function envVitePlugin({ config: astroConfig }: EnvPluginOptions): vite.PluginOption {
|
||||
let privateEnv: Record<string, any> | null;
|
||||
let config: vite.ResolvedConfig;
|
||||
let replacements: Record<string, string>;
|
||||
let pattern: RegExp | undefined;
|
||||
return {
|
||||
name: 'astro:vite-plugin-env',
|
||||
enforce: 'pre',
|
||||
|
||||
configResolved(resolvedConfig) {
|
||||
config = resolvedConfig;
|
||||
if (config.envPrefix) {
|
||||
}
|
||||
},
|
||||
|
||||
async transform(source, id, options) {
|
||||
const ssr = options?.ssr === true;
|
||||
if (!ssr) return source;
|
||||
if (!source.includes('import.meta')) return source;
|
||||
if (!/\benv\b/.test(source)) return source;
|
||||
|
||||
if(!ssr) {
|
||||
return source;
|
||||
}
|
||||
|
||||
if(!source.includes('import.meta') || !/\benv\b/.test(source)) {
|
||||
return source;
|
||||
}
|
||||
|
||||
if (typeof privateEnv === 'undefined') {
|
||||
privateEnv = getPrivateEnv(config, astroConfig);
|
||||
if(privateEnv) {
|
||||
const entries = Object.entries(privateEnv).map(([key, value]) => ([`import.meta.env.${key}`, value]));
|
||||
replacements = Object.fromEntries(entries);
|
||||
pattern = new RegExp(
|
||||
// Do not allow preceding '.', but do allow preceding '...' for spread operations
|
||||
'(?<!(?<!\\.\\.)\\.)\\b(' +
|
||||
Object.keys(replacements)
|
||||
.map((str) => {
|
||||
return str.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&');
|
||||
})
|
||||
.join('|') +
|
||||
// prevent trailing assignments
|
||||
')\\b(?!\\s*?=[^=])', 'g');
|
||||
}
|
||||
}
|
||||
if (!privateEnv) return source;
|
||||
|
||||
if (!privateEnv || !pattern) return source;
|
||||
if (!referencesPrivateKey(source, privateEnv)) return source;
|
||||
|
||||
// Find matches for *private* env and do our own replacement.
|
||||
const s = new MagicString(source);
|
||||
// prettier-ignore
|
||||
s.prepend(`import.meta.env = new Proxy(import.meta.env, {` +
|
||||
`get(target, prop, reciever) {` +
|
||||
`const PRIVATE = ${JSON.stringify(privateEnv)};` +
|
||||
`if (typeof PRIVATE[prop] !== 'undefined') {` +
|
||||
`return PRIVATE[prop];` +
|
||||
`}` +
|
||||
`return Reflect.get(target, prop, reciever);` +
|
||||
`}` +
|
||||
`});\n`);
|
||||
let match: RegExpExecArray | null
|
||||
|
||||
while ((match = pattern.exec(source))) {
|
||||
const start = match.index
|
||||
const end = start + match[0].length
|
||||
const replacement = '' + replacements[match[1]]
|
||||
s.overwrite(start, end, replacement)
|
||||
}
|
||||
|
||||
return s.toString();
|
||||
},
|
||||
|
|
|
@ -7,7 +7,6 @@ describe('Environment Variables', () => {
|
|||
before(async () => {
|
||||
fixture = await loadFixture({
|
||||
projectRoot: './fixtures/astro-envs/',
|
||||
buildOptions: { legacyBuild: true } // TODO make this test work without legacyBuild
|
||||
});
|
||||
|
||||
await fixture.build();
|
||||
|
@ -25,7 +24,7 @@ describe('Environment Variables', () => {
|
|||
});
|
||||
|
||||
it('includes public env in client-side JS', async () => {
|
||||
let dirs = await fixture.readdir('/assets');
|
||||
let dirs = await fixture.readdir('/');
|
||||
let found = false;
|
||||
|
||||
// Look in all of the .js files to see if the public env is inlined.
|
||||
|
@ -34,7 +33,7 @@ describe('Environment Variables', () => {
|
|||
await Promise.all(
|
||||
dirs.map(async (path) => {
|
||||
if (path.endsWith('.js')) {
|
||||
let js = await fixture.readFile(`/assets/${path}`);
|
||||
let js = await fixture.readFile(`/${path}`);
|
||||
if (js.includes('BLUE_BAYOU')) {
|
||||
found = true;
|
||||
}
|
||||
|
@ -46,7 +45,7 @@ describe('Environment Variables', () => {
|
|||
});
|
||||
|
||||
it('does not include private env in client-side JS', async () => {
|
||||
let dirs = await fixture.readdir('/assets');
|
||||
let dirs = await fixture.readdir('/');
|
||||
let found = false;
|
||||
|
||||
// Look in all of the .js files to see if the public env is inlined.
|
||||
|
@ -55,7 +54,7 @@ describe('Environment Variables', () => {
|
|||
await Promise.all(
|
||||
dirs.map(async (path) => {
|
||||
if (path.endsWith('.js')) {
|
||||
let js = await fixture.readFile(`/assets/${path}`);
|
||||
let js = await fixture.readFile(`/${path}`);
|
||||
if (js.includes('CLUB_33')) {
|
||||
found = true;
|
||||
}
|
||||
|
|
|
@ -18,9 +18,6 @@ describe('LitElement test', function () {
|
|||
fixture = await loadFixture({
|
||||
projectRoot: './fixtures/lit-element/',
|
||||
renderers: ['@astrojs/renderer-lit'],
|
||||
buildOptions: {
|
||||
legacyBuild: true
|
||||
}
|
||||
});
|
||||
await fixture.build();
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue