🐛 BUG: Synchronous version actually returns Promise
What version of @astrojs/compiler are you using?
2.3.4
What package manager are you using?
Yarn Classic
What operating system are you using?
Linux
Describe the Bug
The synchronous version of @astrojs/compiler doesn't actually run synchronously and just returns a Promise.
$ node
Welcome to Node.js v20.10.0.
Type ".help" for more information.
> const compiler = require("@astrojs/compiler/sync")
undefined
> const result = compiler.transform("hi")
undefined
> result
Promise {
{
code: 'import {\n' +
' Fragment,\n' +
' render as $$render,\n' +
' createAstro as $$createAstro,\n' +
' createComponent as $$createComponent,\n' +
' renderComponent as $$renderComponent,\n' +
' renderHead as $$renderHead,\n' +
' maybeRenderHead as $$maybeRenderHead,\n' +
' unescapeHTML as $$unescapeHTML,\n' +
' renderSlot as $$renderSlot,\n' +
' mergeSlots as $$mergeSlots,\n' +
' addAttribute as $$addAttribute,\n' +
' spreadAttributes as $$spreadAttributes,\n' +
' defineStyleVars as $$defineStyleVars,\n' +
' defineScriptVars as $$defineScriptVars,\n' +
' renderTransition as $$renderTransition,\n' +
' createTransitionScope as $$createTransitionScope,\n' +
' createMetadata as $$createMetadata\n' +
'} from "astro/runtime/server/index.js";\n' +
'\n' +
'\n' +
'export const $$metadata = $$createMetadata("<stdin>", { modules: [], hydratedComponents: [], clientOnlyComponents: [], hydrationDirectives: new Set([]), hoisted: [] });\n' +
'\n' +
'const $$Astro = $$createAstro();\n' +
'const Astro = $$Astro;\n' +
'const $$stdin = $$createComponent(async ($$result, $$props, $$slots) => {\n' +
'const Astro = $$result.createAstro($$Astro, $$props, $$slots);\n' +
'Astro.self = $$stdin;\n' +
'\n' +
'return $$render`hi`;\n' +
"}, '<stdin>', undefined);\n" +
'export default $$stdin;\n',
diagnostics: [],
map: '',
scope: '5keef2pk',
css: [],
scripts: [],
hydratedComponents: [],
clientOnlyComponents: [],
containsHead: false,
styleError: [],
propagation: false
},
[Symbol(async_id_symbol)]: 210,
[Symbol(trigger_async_id_symbol)]: 6
}
> result.code
undefined
> (await result).code
'import {\n' +
' Fragment,\n' +
' render as $$render,\n' +
' createAstro as $$createAstro,\n' +
' createComponent as $$createComponent,\n' +
' renderComponent as $$renderComponent,\n' +
' renderHead as $$renderHead,\n' +
' maybeRenderHead as $$maybeRenderHead,\n' +
' unescapeHTML as $$unescapeHTML,\n' +
' renderSlot as $$renderSlot,\n' +
' mergeSlots as $$mergeSlots,\n' +
' addAttribute as $$addAttribute,\n' +
' spreadAttributes as $$spreadAttributes,\n' +
' defineStyleVars as $$defineStyleVars,\n' +
' defineScriptVars as $$defineScriptVars,\n' +
' renderTransition as $$renderTransition,\n' +
' createTransitionScope as $$createTransitionScope,\n' +
' createMetadata as $$createMetadata\n' +
'} from "astro/runtime/server/index.js";\n' +
'\n' +
'\n' +
'export const $$metadata = $$createMetadata("<stdin>", { modules: [], hydratedComponents: [], clientOnlyComponents: [], hydrationDirectives: new Set([]), hoisted: [] });\n' +
'\n' +
'const $$Astro = $$createAstro();\n' +
'const Astro = $$Astro;\n' +
'const $$stdin = $$createComponent(async ($$result, $$props, $$slots) => {\n' +
'const Astro = $$result.createAstro($$Astro, $$props, $$slots);\n' +
'Astro.self = $$stdin;\n' +
'\n' +
'return $$render`hi`;\n' +
"}, '<stdin>', undefined);\n" +
'export default $$stdin;\n'
Link to Minimal Reproducible Example
https://stackblitz.com/edit/stackblitz-starters-skqgj1?file=index.js
I think this is because, the internal Transform function returns a promise (the others don't)
https://github.com/withastro/compiler/blob/0c24ea165ca81db30063fcd75bdb5814a51520ce/cmd/astro-wasm/astro-wasm.go#L447
Not sure how it could be refactored though. Maybe @natemoo-re could have more context on this?
Oh this was definitely just an oversight. We don't actually use the sync transform function anywhere in our own code.
I think we'd have to remove the ability to pass an async callback for style compilation.