0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-03-31 23:31:30 -05:00

Fix env var replacement for export const prerender (#9807)

This commit is contained in:
Bjorn Lu 2024-01-24 22:42:24 +08:00 committed by GitHub
parent 5d7db1dbb0
commit b3f313138b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 57 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
"astro": patch
---
Fixes environment variables replacement for `export const prerender`

View file

@ -14,6 +14,9 @@ const importMetaEnvOnlyRe = /\bimport\.meta\.env\b(?!\.)/;
// Match valid JS variable names (identifiers), which accepts most alphanumeric characters,
// except that the first character cannot be a number.
const isValidIdentifierRe = /^[_$a-zA-Z][_$a-zA-Z0-9]*$/;
// Match `export const prerender = import.meta.env.*` since `vite=plugin-scanner` requires
// the `import.meta.env.*` to always be replaced.
const exportConstPrerenderRe = /\bexport\s+const\s+prerender\s*=\s*import\.meta\.env\.(.+?)\b/;
function getPrivateEnv(
viteConfig: vite.ResolvedConfig,
@ -156,6 +159,7 @@ export default function envVitePlugin({ settings }: EnvPluginOptions): vite.Plug
// In dev, we can assign the private env vars to `import.meta.env` directly for performance
if (isDev) {
const s = new MagicString(source);
if (!devImportMetaEnvPrepend) {
devImportMetaEnvPrepend = `Object.assign(import.meta.env,{`;
for (const key in privateEnv) {
@ -164,6 +168,16 @@ export default function envVitePlugin({ settings }: EnvPluginOptions): vite.Plug
devImportMetaEnvPrepend += '});';
}
s.prepend(devImportMetaEnvPrepend);
// EDGE CASE: We need to do a static replacement for `export const prerender` for `vite-plugin-scanner`
s.replace(exportConstPrerenderRe, (m, key) => {
if (privateEnv[key] != null) {
return `export const prerender = ${privateEnv[key]}`;
} else {
return m;
}
});
return {
code: s.toString(),
map: s.generateMap({ hires: 'boundary' }),

View file

@ -140,6 +140,44 @@ describe('Prerendering', () => {
expect($('h1').text()).to.equal('Two');
});
});
describe('Dev', () => {
let devServer;
before(async () => {
process.env.PRERENDER = true;
fixture = await loadFixture({
root: './fixtures/prerender/',
output: 'server',
adapter: nodejs({ mode: 'standalone' }),
});
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
delete process.env.PRERENDER;
});
it('Can render SSR route', async () => {
const res = await fixture.fetch(`/one`);
const html = await res.text();
const $ = cheerio.load(html);
expect(res.status).to.equal(200);
expect($('h1').text()).to.equal('One');
});
it('Can render prerendered route', async () => {
const res = await fixture.fetch(`/two`);
const html = await res.text();
const $ = cheerio.load(html);
expect(res.status).to.equal(200);
expect($('h1').text()).to.equal('Two');
});
});
});
describe('Hybrid rendering', () => {