Skip to content

Commit

Permalink
Update rollup to prevent empty slot bug (withastro#3496)
Browse files Browse the repository at this point in the history
* Update rollup to prevent empty slot bug

* Adds a changeset

* Updated lockfile

* provide import.meta.env.SITE when there are private envs
  • Loading branch information
matthewp committed Jun 1, 2022
1 parent 954165c commit b9a635a
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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",
Expand Down
7 changes: 5 additions & 2 deletions src/core/build/static-build.ts
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/vite-plugin-env/index.ts
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions test/astro-envs.test.js
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/astro-envs/astro.config.mjs
Expand Up @@ -3,5 +3,6 @@ import vue from '@astrojs/vue';

// https://astro.build/config
export default defineConfig({
site: 'http://example.com',
integrations: [vue()],
});
1 change: 1 addition & 0 deletions test/fixtures/astro-envs/src/pages/index.astro
Expand Up @@ -3,4 +3,5 @@ import Client from '../components/Client.vue';
---
<environment-variable>{import.meta.env.PUBLIC_PLACE}</environment-variable>
<environment-variable>{import.meta.env.SECRET_PLACE}</environment-variable>
<environment-variable>{import.meta.env.SITE}</environment-variable>
<Client client:load />
21 changes: 21 additions & 0 deletions 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;
---
<li class="link-card">
<a href={href}>
<h2>
{title}
<span>&rarr;</span>
</h2>
<p>
{body}
<slot name="icon" />
</p>
</a>
</li>
10 changes: 10 additions & 0 deletions test/fixtures/unused-slot/src/pages/index.astro
@@ -0,0 +1,10 @@
---
import Card from '../components/Card.astro';
---
<html>
<head><title>Testing</title></head>
<body>
<h1>Test</h1>
<Card title="A card" href="http://example.com" body="stuff" />
</body>
</html>
19 changes: 19 additions & 0 deletions 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);
});
});

0 comments on commit b9a635a

Please sign in to comment.