summaryrefslogtreecommitdiff
path: root/src/compiler/index.ts
diff options
context:
space:
mode:
authorGravatar Matthew Phillips <matthew@matthewphillips.info> 2021-04-06 16:14:42 -0400
committerGravatar GitHub <noreply@github.com> 2021-04-06 16:14:42 -0400
commit72d9ece6db9cd57c865108b5fad43cbd5728e204 (patch)
treefe9f3ada071ece00c642dba162779796b0be1da3 /src/compiler/index.ts
parent7240f0d67737da3c003455d4ebed6faa5be8c118 (diff)
downloadastro-72d9ece6db9cd57c865108b5fad43cbd5728e204.tar.gz
astro-72d9ece6db9cd57c865108b5fad43cbd5728e204.tar.zst
astro-72d9ece6db9cd57c865108b5fad43cbd5728e204.zip
Compiler cleanup (#64)
* Compiler cleanup This is general compiler cleanup, especially around the codegen part. Goals here were too: 1. Make it possible to compile HTML recursively (needed for future astro-in-expressions work) by moving that work into its own function. 1. Get rid of collectionItems and have compiling the HTML return just a source string. Also not planned, this change gets rid of the different between components and pages. All Astro components compile to the same JavaScript. * Remove unused node types
Diffstat (limited to 'src/compiler/index.ts')
-rw-r--r--src/compiler/index.ts20
1 files changed, 6 insertions, 14 deletions
diff --git a/src/compiler/index.ts b/src/compiler/index.ts
index 112b7881e..d33527b9b 100644
--- a/src/compiler/index.ts
+++ b/src/compiler/index.ts
@@ -115,27 +115,23 @@ export async function compileComponent(
source: string,
{ compileOptions, filename, projectRoot }: { compileOptions: CompileOptions; filename: string; projectRoot: string }
): Promise<CompileResult> {
- const sourceJsx = await transformFromSource(source, { compileOptions, filename, projectRoot });
- const isPage = path.extname(filename) === '.md' || sourceJsx.items.some((item) => item.name === 'html');
+ const result = await transformFromSource(source, { compileOptions, filename, projectRoot });
// return template
let modJsx = `
import fetch from 'node-fetch';
// <script astro></script>
-${sourceJsx.imports.join('\n')}
+${result.imports.join('\n')}
// \`__render()\`: Render the contents of the Astro module.
import { h, Fragment } from '${internalImport('h.js')}';
async function __render(props, ...children) {
- ${sourceJsx.script}
- return h(Fragment, null, ${sourceJsx.items.map(({ jsx }) => jsx).join(',')});
+ ${result.script}
+ return h(Fragment, null, ${result.html});
}
export default __render;
-`;
- if (isPage) {
- modJsx += `
// \`__renderPage()\`: Render the contents of the Astro module as a page. This is a special flow,
// triggered by loading a component directly by URL.
export async function __renderPage({request, children, props}) {
@@ -159,14 +155,10 @@ export async function __renderPage({request, children, props}) {
return childBodyResult;
};\n`;
- } else {
- modJsx += `
-export async function __renderPage() { throw new Error("No <html> page element found!"); }\n`;
- }
return {
- result: sourceJsx,
+ result,
contents: modJsx,
- css: sourceJsx.css,
+ css: result.css,
};
}