From f02b6de6bea70c02e8f312156b5b89450ef538fa Mon Sep 17 00:00:00 2001 From: bluwy Date: Thu, 18 Apr 2024 16:59:13 +0800 Subject: [PATCH] Skip printing `createAstro` code if unused --- .changeset/khaki-ways-wait.md | 5 +++++ internal/printer/print-to-js.go | 17 ++++++++++++----- internal/printer/printer.go | 12 +++++++----- internal/printer/printer_test.go | 18 +++++++++++++----- 4 files changed, 37 insertions(+), 15 deletions(-) create mode 100644 .changeset/khaki-ways-wait.md diff --git a/.changeset/khaki-ways-wait.md b/.changeset/khaki-ways-wait.md new file mode 100644 index 000000000..2e940abfb --- /dev/null +++ b/.changeset/khaki-ways-wait.md @@ -0,0 +1,5 @@ +--- +'@astrojs/compiler': patch +--- + +Skips printing `createAstro` code if the `Astro` global is not referenced diff --git a/internal/printer/print-to-js.go b/internal/printer/print-to-js.go index 6fda4411b..beb1f4833 100644 --- a/internal/printer/print-to-js.go +++ b/internal/printer/print-to-js.go @@ -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 { @@ -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) @@ -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 { @@ -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("") diff --git a/internal/printer/printer.go b/internal/printer/printer.go index 0a6274018..befc1b954 100644 --- a/internal/printer/printer.go +++ b/internal/printer/printer.go @@ -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 } diff --git a/internal/printer/printer_test.go b/internal/printer/printer_test.go index bbcfb40cb..723f77f0b 100644 --- a/internal/printer/printer_test.go +++ b/internal/printer/printer_test.go @@ -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); @@ -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";` @@ -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])) }