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

Skip printing createAstro code if unused #1002

Merged
merged 1 commit into from
Apr 19, 2024
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/khaki-ways-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Skips printing `createAstro` code if the `Astro` global is not referenced
17 changes: 12 additions & 5 deletions internal/printer/print-to-js.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ func render1(p *printer, n *Node, opts RenderOptions) {
return
}

// Decide whether to print code for `Astro` global variable. Use a loose check for now.
printAstroGlobal := strings.Contains(p.sourcetext, "Astro")

// Render frontmatter (will be the first node, if it exists)
if n.Type == FrontmatterNode {
if n.FirstChild == nil {
Expand Down Expand Up @@ -179,9 +182,11 @@ func render1(p *printer, n *Node, opts RenderOptions) {
// 1. Component imports, if any exist.
p.addNilSourceMapping()
p.printComponentMetadata(n.Parent, opts.opts, []byte(p.sourcetext))
// 2. Top-level Astro global.

p.printTopLevelAstro(opts.opts)
// 2. Top-level Astro global.
if printAstroGlobal {
p.printTopLevelAstro(opts.opts)
}

exports := make([][]byte, 0)
exportLocs := make([]loc.Loc, 0)
Expand Down Expand Up @@ -228,7 +233,7 @@ func render1(p *printer, n *Node, opts RenderOptions) {
}
}

p.printFuncPrelude(opts.opts)
p.printFuncPrelude(opts.opts, printAstroGlobal)
// PRINT BODY
if len(bodies) > 0 {
for i, body := range bodies {
Expand Down Expand Up @@ -267,10 +272,12 @@ func render1(p *printer, n *Node, opts RenderOptions) {
return
} else if !p.hasFuncPrelude {
p.printComponentMetadata(n.Parent, opts.opts, []byte{})
p.printTopLevelAstro(opts.opts)
if printAstroGlobal {
p.printTopLevelAstro(opts.opts)
}

// Render func prelude. Will only run for the first non-frontmatter node
p.printFuncPrelude(opts.opts)
p.printFuncPrelude(opts.opts, printAstroGlobal)
// This just ensures a newline
p.println("")

Expand Down
12 changes: 7 additions & 5 deletions internal/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,17 +264,19 @@ func (p *printer) printDefineVarsClose(n *astro.Node) {
}
}

func (p *printer) printFuncPrelude(opts transform.TransformOptions) {
func (p *printer) printFuncPrelude(opts transform.TransformOptions, printAstroGlobal bool) {
if p.hasFuncPrelude {
return
}
componentName := getComponentName(opts.Filename)
p.addNilSourceMapping()
p.println(fmt.Sprintf("const %s = %s(async (%s, $$props, %s) => {", componentName, CREATE_COMPONENT, RESULT, SLOTS))
p.addNilSourceMapping()
p.println(fmt.Sprintf("const Astro = %s.createAstro($$Astro, $$props, %s);", RESULT, SLOTS))
p.addNilSourceMapping()
p.println(fmt.Sprintf("Astro.self = %s;", componentName))
if printAstroGlobal {
p.addNilSourceMapping()
p.println(fmt.Sprintf("const Astro = %s.createAstro($$Astro, $$props, %s);", RESULT, SLOTS))
p.addNilSourceMapping()
p.println(fmt.Sprintf("Astro.self = %s;", componentName))
}
p.hasFuncPrelude = true
}

Expand Down
18 changes: 13 additions & 5 deletions internal/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ var INTERNAL_IMPORTS = fmt.Sprintf("import {\n %s\n} from \"%s\";\n", strings.J
"renderScript as " + RENDER_SCRIPT,
"createMetadata as " + CREATE_METADATA,
}, ",\n "), "http://localhost:3000/")
var PRELUDE = fmt.Sprintf(`const $$Component = %s(async ($$result, $$props, %s) => {
const Astro = $$result.createAstro($$Astro, $$props, %s);
Astro.self = $$Component;%s`, CREATE_COMPONENT, SLOTS, SLOTS, "\n\n")
var PRELUDE = fmt.Sprintf(`const $$Component = %s(async ($$result, $$props, %s) => {`, CREATE_COMPONENT, SLOTS)
var PRELUDE_ASTRO_GLOBAL = fmt.Sprintf(`const Astro = $$result.createAstro($$Astro, $$props, %s);
Astro.self = $$Component;`, SLOTS)
var RETURN = fmt.Sprintf("return %s%s", TEMPLATE_TAG, BACKTICK)
var SUFFIX = fmt.Sprintf("%s;", BACKTICK) + `
}, undefined, undefined);
Expand Down Expand Up @@ -3571,6 +3571,8 @@ const meta = { title: 'My App' };
}, h)
output := string(result.Output)

// The compiler prints Astro global code only if it's loosely used
printAstroGlobal := strings.Contains(tt.source, "Astro")
toMatch := INTERNAL_IMPORTS
if strings.Count(tt.source, "transition:") > 0 {
toMatch += `import "transitions.css";`
Expand Down Expand Up @@ -3654,11 +3656,17 @@ const meta = { title: 'My App' };
patharg = fmt.Sprintf("\"%s\"", escapedFilename)
}
toMatch += "\n\n" + fmt.Sprintf("export const %s = %s(%s, %s);\n\n", METADATA, CREATE_METADATA, patharg, metadata)
toMatch += test_utils.Dedent(CREATE_ASTRO_CALL) + "\n"
if printAstroGlobal {
toMatch += test_utils.Dedent(CREATE_ASTRO_CALL) + "\n"
}
if len(tt.want.getStaticPaths) > 0 {
toMatch += strings.TrimSpace(test_utils.Dedent(tt.want.getStaticPaths)) + "\n\n"
}
toMatch += test_utils.Dedent(PRELUDE) + "\n"
if printAstroGlobal {
toMatch += test_utils.Dedent(PRELUDE) + test_utils.Dedent(PRELUDE_ASTRO_GLOBAL) + "\n"
} else {
toMatch += test_utils.Dedent(PRELUDE) + "\n"
}
if len(tt.want.frontmatter) > 1 {
toMatch += strings.TrimSpace(test_utils.Dedent(tt.want.frontmatter[1]))
}
Expand Down