diff --git a/src/@types/astro.ts b/src/@types/astro.ts index 8f6b73d5e3..509d8ddc51 100644 --- a/src/@types/astro.ts +++ b/src/@types/astro.ts @@ -24,7 +24,7 @@ export interface JsxItem { export interface TransformResult { script: string; imports: string[]; - items: JsxItem[]; + html: string; css?: string; } diff --git a/src/compiler/codegen.ts b/src/compiler/codegen.ts index 0447cbdc7f..a08db028f7 100644 --- a/src/compiler/codegen.ts +++ b/src/compiler/codegen.ts @@ -1,6 +1,6 @@ import type { CompileOptions } from '../@types/compiler'; import type { AstroConfig, ValidExtensionPlugins } from '../@types/astro'; -import type { Ast, TemplateNode } from '../parser/interfaces'; +import type { Ast, Script, Style, TemplateNode } from '../parser/interfaces'; import type { JsxItem, TransformResult } from '../@types/astro'; import eslexer from 'es-module-lexer'; @@ -262,17 +262,18 @@ async function acquireDynamicComponentImports(plugins: Set { - const { extensions = defaultExtensions, astroConfig } = compileOptions; - await eslexer.init; +type Components = Record; + +interface CodegenState { + filename: string; + components: Components; + css: string[]; + importExportStatements: Set; + dynamicImports: DynamicImportMap; +} + +function compileModule(module: Script, state: CodegenState, compileOptions: CompileOptions) { + const { extensions = defaultExtensions } = compileOptions; const componentImports: ImportDeclaration[] = []; const componentProps: VariableDeclarator[] = []; @@ -280,12 +281,10 @@ export async function codegen(ast: Ast, { compileOptions, filename }: CodeGenOpt let script = ''; let propsStatement = ''; - const importExportStatements: Set = new Set(); - const components: Record = {}; const componentPlugins = new Set(); - if (ast.module) { - const program = babelParser.parse(ast.module.content, { + if (module) { + const program = babelParser.parse(module.content, { sourceType: 'module', plugins: ['jsx', 'typescript', 'topLevelAwait'], }).program; @@ -317,7 +316,7 @@ export async function codegen(ast: Ast, { compileOptions, filename }: CodeGenOpt const componentType = path.posix.extname(importUrl); const componentName = path.posix.basename(importUrl, componentType); const plugin = extensions[componentType] || defaultExtensions[componentType]; - components[componentName] = { + state.components[componentName] = { type: componentType, plugin, url: importUrl, @@ -325,10 +324,10 @@ export async function codegen(ast: Ast, { compileOptions, filename }: CodeGenOpt if (plugin) { componentPlugins.add(plugin); } - importExportStatements.add(ast.module.content.slice(componentImport.start!, componentImport.end!)); + state.importExportStatements.add(module.content.slice(componentImport.start!, componentImport.end!)); } for (const componentImport of componentExports) { - importExportStatements.add(ast.module.content.slice(componentImport.start!, componentImport.end!)); + state.importExportStatements.add(module.content.slice(componentImport.start!, componentImport.end!)); } if (componentProps.length > 0) { @@ -345,18 +344,14 @@ export async function codegen(ast: Ast, { compileOptions, filename }: CodeGenOpt script = propsStatement + babelGenerator(program).code; } - const dynamicImports = await acquireDynamicComponentImports(componentPlugins, compileOptions.resolve); + return { script, componentPlugins }; +} - let items: JsxItem[] = []; - let collectionItem: JsxItem | undefined; - let currentItemName: string | undefined; - let currentDepth = 0; - let css: string[] = []; - - walk(ast.css, { +function compileCss(style: Style, state: CodegenState) { + walk(style, { enter(node: TemplateNode) { if (node.type === 'Style') { - css.push(node.content.styles); // if multiple