From b9a635a2eb3d56da197400d5cc330073c0525cd0 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Wed, 1 Jun 2022 12:21:32 -0400 Subject: [PATCH] Update rollup to prevent empty slot bug (#3496) * Update rollup to prevent empty slot bug * Adds a changeset * Updated lockfile * provide import.meta.env.SITE when there are private envs --- package.json | 2 +- src/core/build/static-build.ts | 7 +++++-- src/vite-plugin-env/index.ts | 1 + test/astro-envs.test.js | 5 +++++ test/fixtures/astro-envs/astro.config.mjs | 1 + .../fixtures/astro-envs/src/pages/index.astro | 1 + .../unused-slot/src/components/Card.astro | 21 +++++++++++++++++++ .../unused-slot/src/pages/index.astro | 10 +++++++++ test/unused-slot.test.js | 19 +++++++++++++++++ 9 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/unused-slot/src/components/Card.astro create mode 100644 test/fixtures/unused-slot/src/pages/index.astro create mode 100644 test/unused-slot.test.js diff --git a/package.json b/package.json index 6cec560a8635..dddbe0f820a1 100644 --- a/package.json +++ b/package.json @@ -119,7 +119,7 @@ "prompts": "^2.4.2", "recast": "^0.20.5", "resolve": "^1.22.0", - "rollup": "^2.75.4", + "rollup": "^2.75.5", "semver": "^7.3.7", "serialize-javascript": "^6.0.0", "shiki": "^0.10.1", diff --git a/src/core/build/static-build.ts b/src/core/build/static-build.ts index 3fa065373691..8a76e2e059df 100644 --- a/src/core/build/static-build.ts +++ b/src/core/build/static-build.ts @@ -132,8 +132,11 @@ export async function staticBuild(opts: StaticBuildOptions) { timer.generate = performance.now(); if (opts.buildConfig.staticMode) { - await generatePages(ssrResult, opts, internals, facadeIdToPageDataMap); - await cleanSsrOutput(opts); + try { + await generatePages(ssrResult, opts, internals, facadeIdToPageDataMap); + } finally { + await cleanSsrOutput(opts); + } } else { info(opts.logging, null, `\n${bgMagenta(black(' finalizing server assets '))}\n`); await ssrMoveAssets(opts); diff --git a/src/vite-plugin-env/index.ts b/src/vite-plugin-env/index.ts index a510f2eee8ca..672fe5a0f0da 100644 --- a/src/vite-plugin-env/index.ts +++ b/src/vite-plugin-env/index.ts @@ -85,6 +85,7 @@ export default function envVitePlugin({ replacements = Object.fromEntries(entries); // These additional replacements are needed to match Vite replacements = Object.assign(replacements, { + 'import.meta.env.SITE': astroConfig.site ? `'${astroConfig.site}'` : 'undefined', // This catches destructed `import.meta.env` calls, // BUT we only want to inject private keys referenced in the file. // We overwrite this value on a per-file basis. diff --git a/test/astro-envs.test.js b/test/astro-envs.test.js index f44b007c8592..da2332c9eb07 100644 --- a/test/astro-envs.test.js +++ b/test/astro-envs.test.js @@ -30,6 +30,11 @@ describe('Environment Variables', () => { expect(indexHtml).to.include('BLUE_BAYOU'); }); + it('does render builtin SITE env', async () => { + let indexHtml = await fixture.readFile('/index.html'); + expect(indexHtml).to.include('http://example.com'); + }); + it('includes public env in client-side JS', async () => { let dirs = await fixture.readdir('/'); let found = false; diff --git a/test/fixtures/astro-envs/astro.config.mjs b/test/fixtures/astro-envs/astro.config.mjs index 88193061246b..7bb1b85f1d15 100644 --- a/test/fixtures/astro-envs/astro.config.mjs +++ b/test/fixtures/astro-envs/astro.config.mjs @@ -3,5 +3,6 @@ import vue from '@astrojs/vue'; // https://astro.build/config export default defineConfig({ + site: 'http://example.com', integrations: [vue()], }); diff --git a/test/fixtures/astro-envs/src/pages/index.astro b/test/fixtures/astro-envs/src/pages/index.astro index f71c11db75dc..3c265dbef736 100644 --- a/test/fixtures/astro-envs/src/pages/index.astro +++ b/test/fixtures/astro-envs/src/pages/index.astro @@ -3,4 +3,5 @@ import Client from '../components/Client.vue'; --- {import.meta.env.PUBLIC_PLACE} {import.meta.env.SECRET_PLACE} +{import.meta.env.SITE} diff --git a/test/fixtures/unused-slot/src/components/Card.astro b/test/fixtures/unused-slot/src/components/Card.astro new file mode 100644 index 000000000000..61fafb04c01e --- /dev/null +++ b/test/fixtures/unused-slot/src/components/Card.astro @@ -0,0 +1,21 @@ +--- +export interface Props { + title: string, + body: string, + href: string, +} +const {href, title, body} = Astro.props; +debugger; +--- + diff --git a/test/fixtures/unused-slot/src/pages/index.astro b/test/fixtures/unused-slot/src/pages/index.astro new file mode 100644 index 000000000000..07f362b22dc1 --- /dev/null +++ b/test/fixtures/unused-slot/src/pages/index.astro @@ -0,0 +1,10 @@ +--- +import Card from '../components/Card.astro'; +--- + + Testing + +

Test

+ + + diff --git a/test/unused-slot.test.js b/test/unused-slot.test.js new file mode 100644 index 000000000000..2c7ce226f10f --- /dev/null +++ b/test/unused-slot.test.js @@ -0,0 +1,19 @@ +import { expect } from 'chai'; +import * as cheerio from 'cheerio'; +import { loadFixture } from './test-utils.js'; + +describe('Unused slot', () => { + let fixture; + + before(async () => { + fixture = await loadFixture({ root: './fixtures/unused-slot/' }); + await fixture.build(); + }); + + it('is able to build with the slot missing', async () => { + let html = await fixture.readFile('/index.html'); + let $ = cheerio.load(html); + // No children, slot rendered as empty + expect($('body p').children().length).to.equal(0); + }); +});