Fixes variable definitions in <style define:vars> (#3241)
* adding SSRResult.styles back to the rendered head * adding test for define:vars in static build * chore: adding changeset
This commit is contained in:
parent
f244b1c6e4
commit
d25dc4c448
4 changed files with 34 additions and 2 deletions
5
.changeset/curly-crews-cross.md
Normal file
5
.changeset/curly-crews-cross.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Fix a bug in define:vars preventing variables from being included in rendered styles
|
|
@ -543,7 +543,9 @@ const uniqueElements = (item: any, index: number, all: any[]) => {
|
|||
// Renders a page to completion by first calling the factory callback, waiting for its result, and then appending
|
||||
// styles and scripts into the head.
|
||||
export async function renderHead(result: SSRResult): Promise<string> {
|
||||
const styles = [];
|
||||
const styles = Array.from(result.styles)
|
||||
.filter(uniqueElements)
|
||||
.map((style) => renderElement('style', style));
|
||||
let needsHydrationStyles = false;
|
||||
const scripts = Array.from(result.scripts)
|
||||
.filter(uniqueElements)
|
||||
|
|
|
@ -18,6 +18,22 @@ describe('Directives', async () => {
|
|||
expect($('script#inline').toString()).to.include('let foo = "bar"');
|
||||
});
|
||||
|
||||
it('Passes define:vars to style elements', async () => {
|
||||
const html = await fixture.readFile('/define-vars/index.html');
|
||||
const $ = cheerio.load(html);
|
||||
|
||||
expect($('style')).to.have.lengthOf(1);
|
||||
expect($('style').toString()).to.include('--bg: white;');
|
||||
expect($('style').toString()).to.include('--fg: black;');
|
||||
|
||||
const scopedClass = $('html').attr('class').split(' ')
|
||||
.find((name) => /^astro-[A-Za-z0-9-]+/.test(name));
|
||||
|
||||
expect($('style').toString().replace(/\s+/g, '')).to.equal(
|
||||
`<style>.${scopedClass}{--bg:white;--fg:black;}body{background:var(--bg);color:var(--fg)}</style>`
|
||||
);
|
||||
});
|
||||
|
||||
it('set:html', async () => {
|
||||
const html = await fixture.readFile('/set-html/index.html');
|
||||
const $ = cheerio.load(html);
|
||||
|
|
|
@ -1,9 +1,18 @@
|
|||
---
|
||||
let foo = 'bar'
|
||||
let bg = 'white'
|
||||
let fg = 'black'
|
||||
---
|
||||
|
||||
<html>
|
||||
<head></head>
|
||||
<head>
|
||||
<style define:vars={{bg, fg}}>
|
||||
body {
|
||||
background: var(--bg);
|
||||
color: var(--fg);
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<script id="inline" define:vars={{ foo }}>
|
||||
console.log(foo);
|
||||
|
|
Loading…
Reference in a new issue