diff --git a/.changeset/clever-snakes-perform.md b/.changeset/clever-snakes-perform.md new file mode 100644 index 000000000..18699fcde --- /dev/null +++ b/.changeset/clever-snakes-perform.md @@ -0,0 +1,5 @@ +--- +'@astrojs/compiler': patch +--- + +Fix small bugs with script/style hoisting behavior diff --git a/internal/printer/printer.go b/internal/printer/printer.go index 9c532dc15..ebf1ff62e 100644 --- a/internal/printer/printer.go +++ b/internal/printer/printer.go @@ -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))) } } diff --git a/internal/printer/printer_test.go b/internal/printer/printer_test.go index 92bef4730..7c4671076 100644 --- a/internal/printer/printer_test.go +++ b/internal/printer/printer_test.go @@ -794,6 +794,22 @@ import * as $$module2 from '../components/ZComponent.jsx';`}, code: `
`, }, }, + { + name: "Empty script", + source: ``, + want: want{ + scripts: []string{`{props:{"hoist":true}}`}, + code: ``, + }, + }, + { + name: "Empty style", + source: ``, + want: want{ + styles: []string{`{props:{"define:vars":({ color: "Gainsboro" }),"data-astro-id":"7HAAVZPE"}}`}, + code: ``, + }, + }, } for _, tt := range tests { diff --git a/internal/token_test.go b/internal/token_test.go index 970426fae..bacff9f4d 100644 --- a/internal/token_test.go +++ b/internal/token_test.go @@ -150,6 +150,11 @@ func TestBasic(t *testing.T) { ``, []TokenType{StartTagToken, StartTagToken, TextToken, EndTagToken, EndTagToken}, }, + { + "multiple scoped :global", + ``, + []TokenType{StartTagToken, TextToken, EndTagToken, StartTagToken, TextToken, EndTagToken}, + }, } runTokenTypeTest(t, Basic) diff --git a/internal/transform/scope-css.go b/internal/transform/scope-css.go index b7ad7a5ab..0df911b8f 100644 --- a/internal/transform/scope-css.go +++ b/internal/transform/scope-css.go @@ -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 := "" diff --git a/internal/transform/transform.go b/internal/transform/transform.go index b0fadfd8a..7704d0f91 100644 --- a/internal/transform/transform.go +++ b/internal/transform/transform.go @@ -37,7 +37,6 @@ func ExtractStyles(doc *tycho.Node) { case a.Style: // Do not extract + + + `, + want: ``, + }, } var b strings.Builder for _, tt := range tests {