summaryrefslogtreecommitdiff
path: root/src/compiler/codegen.ts
diff options
context:
space:
mode:
authorGravatar Drew Powers <1369770+drwpow@users.noreply.github.com> 2021-03-25 16:59:38 -0600
committerGravatar GitHub <noreply@github.com> 2021-03-25 16:59:38 -0600
commit04a443a8887257e86d824bb22686a527f84c0875 (patch)
treeb64a7cd8fa56e4c549a1b60afa63e611779eda53 /src/compiler/codegen.ts
parent3db595937719b89956c594e4a76ee68ae8de098a (diff)
downloadastro-04a443a8887257e86d824bb22686a527f84c0875.tar.gz
astro-04a443a8887257e86d824bb22686a527f84c0875.tar.zst
astro-04a443a8887257e86d824bb22686a527f84c0875.zip
Add React component SSR (#28)
* Add React component SSR * Add React component SSR
Diffstat (limited to 'src/compiler/codegen.ts')
-rw-r--r--src/compiler/codegen.ts29
1 files changed, 15 insertions, 14 deletions
diff --git a/src/compiler/codegen.ts b/src/compiler/codegen.ts
index 58c7a6c9d..c0d963703 100644
--- a/src/compiler/codegen.ts
+++ b/src/compiler/codegen.ts
@@ -10,7 +10,7 @@ import { walk } from 'estree-walker';
import babelParser from '@babel/parser';
import _babelGenerator from '@babel/generator';
import traverse from '@babel/traverse';
-import { ImportDeclaration,ExportNamedDeclaration, VariableDeclarator, Identifier, VariableDeclaration } from '@babel/types';
+import { ImportDeclaration, ExportNamedDeclaration, VariableDeclarator, Identifier, VariableDeclaration } from '@babel/types';
import { type } from 'node:os';
const babelGenerator: typeof _babelGenerator =
@@ -111,10 +111,7 @@ const defaultExtensions: Readonly<Record<string, ValidExtensionPlugins>> = {
'.svelte': 'svelte',
};
-type DynamicImportMap = Map<
- 'vue' | 'react' | 'react-dom' | 'preact',
- string
->;
+type DynamicImportMap = Map<'vue' | 'react' | 'react-dom' | 'preact', string>;
function getComponentWrapper(_name: string, { type, plugin, url }: ComponentInfo, dynamicImports: DynamicImportMap) {
const [name, kind] = _name.split(':');
@@ -136,7 +133,9 @@ function getComponentWrapper(_name: string, { type, plugin, url }: ComponentInfo
case 'preact': {
if (kind === 'dynamic') {
return {
- wrapper: `__preact_dynamic(${name}, new URL(${JSON.stringify(url.replace(/\.[^.]+$/, '.js'))}, \`http://TEST\${import.meta.url}\`).pathname, '${dynamicImports.get('preact')!}')`,
+ wrapper: `__preact_dynamic(${name}, new URL(${JSON.stringify(url.replace(/\.[^.]+$/, '.js'))}, \`http://TEST\${import.meta.url}\`).pathname, '${dynamicImports.get(
+ 'preact'
+ )!}')`,
wrapperImport: `import {__preact_dynamic} from '${internalImport('render/preact.js')}';`,
};
} else {
@@ -177,7 +176,9 @@ function getComponentWrapper(_name: string, { type, plugin, url }: ComponentInfo
case 'vue': {
if (kind === 'dynamic') {
return {
- wrapper: `__vue_dynamic(${name}, new URL(${JSON.stringify(url.replace(/\.[^.]+$/, '.vue.js'))}, \`http://TEST\${import.meta.url}\`).pathname, '${dynamicImports.get('vue')!}')`,
+ wrapper: `__vue_dynamic(${name}, new URL(${JSON.stringify(url.replace(/\.[^.]+$/, '.vue.js'))}, \`http://TEST\${import.meta.url}\`).pathname, '${dynamicImports.get(
+ 'vue'
+ )!}')`,
wrapperImport: `import {__vue_dynamic} from '${internalImport('render/vue.js')}';`,
};
} else {
@@ -207,8 +208,8 @@ function compileExpressionSafe(raw: string): string {
async function acquireDynamicComponentImports(plugins: Set<ValidExtensionPlugins>, resolve: (s: string) => Promise<string>): Promise<DynamicImportMap> {
const importMap: DynamicImportMap = new Map();
- for(let plugin of plugins) {
- switch(plugin) {
+ for (let plugin of plugins) {
+ switch (plugin) {
case 'vue': {
importMap.set('vue', await resolve('vue'));
break;
@@ -236,9 +237,9 @@ export async function codegen(ast: Ast, { compileOptions }: CodeGenOptions): Pro
const componentExports: ExportNamedDeclaration[] = [];
let script = '';
- let propsStatement: string = '';
+ let propsStatement = '';
const importExportStatements: Set<string> = new Set();
- const components: Record<string, { type: string; url: string, plugin: string | undefined }> = {};
+ const components: Record<string, { type: string; url: string; plugin: string | undefined }> = {};
const componentPlugins = new Set<ValidExtensionPlugins>();
if (ast.module) {
@@ -277,9 +278,9 @@ export async function codegen(ast: Ast, { compileOptions }: CodeGenOptions): Pro
components[componentName] = {
type: componentType,
plugin,
- url: importUrl
+ url: importUrl,
};
- if(plugin) {
+ if (plugin) {
componentPlugins.add(plugin);
}
importExportStatements.add(ast.module.content.slice(componentImport.start!, componentImport.end!));
@@ -293,7 +294,7 @@ export async function codegen(ast: Ast, { compileOptions }: CodeGenOptions): Pro
for (const componentExport of componentProps) {
propsStatement += `${(componentExport.id as Identifier).name}`;
if (componentExport.init) {
- propsStatement += `= ${babelGenerator(componentExport.init!).code }`;
+ propsStatement += `= ${babelGenerator(componentExport.init!).code}`;
}
propsStatement += `,`;
}