Skip to content
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/chilled-colts-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Adds metadata on client:only components
13 changes: 13 additions & 0 deletions internal/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ func (p *printer) printTopLevelAstro(opts transform.TransformOptions) {
func (p *printer) printComponentMetadata(doc *astro.Node, opts transform.TransformOptions, source []byte) {
var specs []string
var asrts []string
var conlyspecs []string

modCount := 1
loc, statement := js_scanner.NextImportStatement(source, 0)
Expand All @@ -351,6 +352,7 @@ func (p *printer) printComponentMetadata(doc *astro.Node, opts transform.Transfo
Type: astro.ExpressionAttribute,
}
n.Attr = append(n.Attr, pathAttr)
conlyspecs = append(conlyspecs, statement.Specifier)

exportAttr := astro.Attribute{
Key: "client:component-export",
Expand All @@ -370,6 +372,7 @@ func (p *printer) printComponentMetadata(doc *astro.Node, opts transform.Transfo
Type: astro.ExpressionAttribute,
}
n.Attr = append(n.Attr, pathAttr)
conlyspecs = append(conlyspecs, statement.Specifier)

exportAttr := astro.Attribute{
Key: "client:component-export",
Expand Down Expand Up @@ -440,6 +443,14 @@ func (p *printer) printComponentMetadata(doc *astro.Node, opts transform.Transfo
p.print(node.Data)
}
}
// Client-Only Components
p.print("], clientOnlyComponents: [")
for i, spec := range conlyspecs {
if i > 0 {
p.print(", ")
}
p.print(fmt.Sprintf("'%s'", spec))
}
p.print("], hydrationDirectives: new Set([")
i := 0
for directive := range doc.HydrationDirectives {
Expand All @@ -449,6 +460,7 @@ func (p *printer) printComponentMetadata(doc *astro.Node, opts transform.Transfo
p.print(fmt.Sprintf("'%s'", directive))
i++
}
// Hoisted scripts
p.print("]), hoisted: [")
for i, node := range doc.Scripts {
if i > 0 {
Expand All @@ -462,5 +474,6 @@ func (p *printer) printComponentMetadata(doc *astro.Node, opts transform.Transfo
p.print(fmt.Sprintf("{ type: 'inline', value: `%s` }", escapeInterpolation(escapeBackticks(node.FirstChild.Data))))
}
}

p.print("] });\n\n")
}
29 changes: 22 additions & 7 deletions internal/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ type want struct {
}

type metadata struct {
hoisted []string
hydratedComponents []string
modules []string
hydrationDirectives []string
hoisted []string
hydratedComponents []string
clientOnlyComponents []string
modules []string
hydrationDirectives []string
}

type testcase struct {
Expand Down Expand Up @@ -283,7 +284,8 @@ import Component from '../components';
want: want{
frontmatter: []string{"import Component from '../components';"},
metadata: metadata{
hydrationDirectives: []string{"only"},
hydrationDirectives: []string{"only"},
clientOnlyComponents: []string{"../components"},
},
// Specifically do NOT render any metadata here, we need to skip this import
code: `<html>
Expand Down Expand Up @@ -311,7 +313,8 @@ import { Component } from '../components';
want: want{
frontmatter: []string{"import { Component } from '../components';"},
metadata: metadata{
hydrationDirectives: []string{"only"},
hydrationDirectives: []string{"only"},
clientOnlyComponents: []string{"../components"},
},
// Specifically do NOT render any metadata here, we need to skip this import
code: `<html>
Expand Down Expand Up @@ -339,7 +342,8 @@ import * as components from '../components';
want: want{
frontmatter: []string{"import * as components from '../components';"},
metadata: metadata{
hydrationDirectives: []string{"only"},
hydrationDirectives: []string{"only"},
clientOnlyComponents: []string{"../components"},
},
// Specifically do NOT render any metadata here, we need to skip this import
code: `<html>
Expand Down Expand Up @@ -1783,6 +1787,17 @@ const items = ["Dog", "Cat", "Platipus"];
}
}
metadata += "]"
// metadata.clientOnlyComponents
metadata += ", clientOnlyComponents: ["
if len(tt.want.metadata.clientOnlyComponents) > 0 {
for i, c := range tt.want.clientOnlyComponents {
if i > 0 {
metadata += ", "
}
metadata += fmt.Sprintf("'%s'", c)
}
}
metadata += "]"
// directives
metadata += ", hydrationDirectives: new Set(["
if len(tt.want.hydrationDirectives) > 0 {
Expand Down
Binary file modified lib/compiler/deno/astro.wasm
Binary file not shown.