From ff0294442a6559a9dda34eea46e001555f2cd909 Mon Sep 17 00:00:00 2001 From: bluwy Date: Tue, 30 Apr 2024 22:34:37 +0800 Subject: [PATCH] Skip printing async if await is not used --- .changeset/warm-zebras-rest.md | 5 +++++ internal/printer/printer.go | 9 ++++++++- internal/printer/printer_test.go | 13 +++++++++---- 3 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 .changeset/warm-zebras-rest.md diff --git a/.changeset/warm-zebras-rest.md b/.changeset/warm-zebras-rest.md new file mode 100644 index 000000000..da3ca05e5 --- /dev/null +++ b/.changeset/warm-zebras-rest.md @@ -0,0 +1,5 @@ +--- +'@astrojs/compiler': patch +--- + +Skips printing `async` for component functions if `await` is not used diff --git a/internal/printer/printer.go b/internal/printer/printer.go index befc1b954..f227cd9e7 100644 --- a/internal/printer/printer.go +++ b/internal/printer/printer.go @@ -269,8 +269,15 @@ func (p *printer) printFuncPrelude(opts transform.TransformOptions, printAstroGl return } componentName := getComponentName(opts.Filename) + + // Decide whether to print `async` if top-level await is used. Use a loose check for now. + funcPrefix := "" + if strings.Contains(p.sourcetext, "await") { + funcPrefix = "async " + } + p.addNilSourceMapping() - p.println(fmt.Sprintf("const %s = %s(async (%s, $$props, %s) => {", componentName, CREATE_COMPONENT, RESULT, SLOTS)) + p.println(fmt.Sprintf("const %s = %s(%s(%s, $$props, %s) => {", componentName, CREATE_COMPONENT, funcPrefix, RESULT, SLOTS)) if printAstroGlobal { p.addNilSourceMapping() p.println(fmt.Sprintf("const Astro = %s.createAstro($$Astro, $$props, %s);", RESULT, SLOTS)) diff --git a/internal/printer/printer_test.go b/internal/printer/printer_test.go index 723f77f0b..1b2863dfb 100644 --- a/internal/printer/printer_test.go +++ b/internal/printer/printer_test.go @@ -35,7 +35,8 @@ 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) => {`, CREATE_COMPONENT, SLOTS) +var PRELUDE = fmt.Sprintf(`const $$Component = %s(($$result, $$props, %s) => {`, CREATE_COMPONENT, SLOTS) +var PRELUDE_WITH_ASYNC = 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) @@ -3662,11 +3663,15 @@ const meta = { title: 'My App' }; if len(tt.want.getStaticPaths) > 0 { toMatch += strings.TrimSpace(test_utils.Dedent(tt.want.getStaticPaths)) + "\n\n" } - if printAstroGlobal { - toMatch += test_utils.Dedent(PRELUDE) + test_utils.Dedent(PRELUDE_ASTRO_GLOBAL) + "\n" + if strings.Contains(tt.source, "await") { + toMatch += test_utils.Dedent(PRELUDE_WITH_ASYNC) } else { - toMatch += test_utils.Dedent(PRELUDE) + "\n" + toMatch += test_utils.Dedent(PRELUDE) + } + if printAstroGlobal { + toMatch += test_utils.Dedent(PRELUDE_ASTRO_GLOBAL) } + toMatch += "\n" if len(tt.want.frontmatter) > 1 { toMatch += strings.TrimSpace(test_utils.Dedent(tt.want.frontmatter[1])) }