diff --git a/.changeset/chilled-colts-kick.md b/.changeset/chilled-colts-kick.md new file mode 100644 index 000000000..8342a186b --- /dev/null +++ b/.changeset/chilled-colts-kick.md @@ -0,0 +1,5 @@ +--- +'@astrojs/compiler': patch +--- + +Adds metadata on client:only components diff --git a/internal/printer/printer.go b/internal/printer/printer.go index 63b5a7930..3d298388e 100644 --- a/internal/printer/printer.go +++ b/internal/printer/printer.go @@ -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) @@ -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", @@ -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", @@ -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 { @@ -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 { @@ -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") } diff --git a/internal/printer/printer_test.go b/internal/printer/printer_test.go index ca3e3363e..73c3e7759 100644 --- a/internal/printer/printer_test.go +++ b/internal/printer/printer_test.go @@ -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 { @@ -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: ` @@ -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: ` @@ -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: ` @@ -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 { diff --git a/lib/compiler/deno/astro.wasm b/lib/compiler/deno/astro.wasm index 389460aa6..a4244f92f 100755 Binary files a/lib/compiler/deno/astro.wasm and b/lib/compiler/deno/astro.wasm differ