Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 1679 #115

Merged
merged 9 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/clever-snakes-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Fix small bugs with script/style hoisting behavior
2 changes: 1 addition & 1 deletion internal/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func (p *printer) printComponentMetadata(doc *astro.Node, source []byte) {
src := astro.GetAttribute(node, "src")
if src != nil {
p.print(fmt.Sprintf("{ type: 'remote', src: '%s' }", escapeSingleQuote(src.Val)))
} else {
} else if node.FirstChild != nil {
p.print(fmt.Sprintf("{ type: 'inline', value: '%s' }", escapeSingleQuote(node.FirstChild.Data)))
}
}
Expand Down
16 changes: 16 additions & 0 deletions internal/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,22 @@ import * as $$module2 from '../components/ZComponent.jsx';`},
code: `<html><head></head><body><svg><style>path { fill: red; }</style></svg></body></html>`,
},
},
{
name: "Empty script",
source: `<script hoist></script>`,
want: want{
scripts: []string{`{props:{"hoist":true}}`},
code: `<html><head></head><body></body></html>`,
},
},
{
name: "Empty style",
source: `<style define:vars={{ color: "Gainsboro" }}></style>`,
want: want{
styles: []string{`{props:{"define:vars":({ color: "Gainsboro" }),"data-astro-id":"7HAAVZPE"}}`},
code: `<html class="astro-7HAAVZPE"><head></head><body></body></html>`,
},
},
}

for _, tt := range tests {
Expand Down
5 changes: 5 additions & 0 deletions internal/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ func TestBasic(t *testing.T) {
`<svg><style><div>:root { color: red; }</style></svg>`,
[]TokenType{StartTagToken, StartTagToken, TextToken, EndTagToken, EndTagToken},
},
{
"multiple scoped :global",
`<style>:global(test-2) {}</style><style>test-1{}</style>`,
[]TokenType{StartTagToken, TextToken, EndTagToken, StartTagToken, TextToken, EndTagToken},
},
}

runTokenTypeTest(t, Basic)
Expand Down
3 changes: 3 additions & 0 deletions internal/transform/scope-css.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ outer:
Key: "data-astro-id",
Val: opts.Scope,
})
if n.FirstChild == nil {
continue
}
p := css.NewParser(bytes.NewBufferString(n.FirstChild.Data), false)
out := ""

Expand Down
1 change: 0 additions & 1 deletion internal/transform/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func ExtractStyles(doc *tycho.Node) {
case a.Style:
// Do not extract <style> inside of SVGs
if n.Parent != nil && n.Parent.DataAtom == atom.Svg {
fmt.Println(n.FirstChild.Data)
return
}
doc.Styles = append(doc.Styles, n)
Expand Down
9 changes: 9 additions & 0 deletions internal/transform/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ func TestTransformScoping(t *testing.T) {
`,
want: `<div class="astro-XXXXXX"></div>`,
},
{
name: "multiple scoped :global",
source: `
<style>:global(test-2) {}</style>
<style>:global(test-1) {}</style>
<div />
`,
want: `<div class="astro-XXXXXX"></div>`,
},
}
var b strings.Builder
for _, tt := range tests {
Expand Down