1- import { generate } from '@babel/generator' ;
2- import * as t from '@babel/types' ;
3-
41import { JSX_IMPORTS } from '../constants.mjs' ;
52
63/**
7- * Creates an import declaration AST node
4+ * Creates an import statement as a string
85 * @param {string|null } importName - The import identifier name, or null for side-effect imports
96 * @param {string } source - The module path to import from
107 * @param {boolean } [useDefault=true] - Whether to use a default import or a named import
8+ * @returns {string } The import statement
119 */
1210export const createImportDeclaration = (
1311 importName ,
1412 source ,
1513 useDefault = true
1614) => {
17- const specifiers = importName
18- ? [
19- useDefault
20- ? t . importDefaultSpecifier ( t . identifier ( importName ) )
21- : t . importSpecifier (
22- t . identifier ( importName ) ,
23- t . identifier ( importName )
24- ) ,
25- ]
26- : [ ] ;
27-
28- return t . importDeclaration ( specifiers , t . stringLiteral ( source ) ) ;
15+ if ( ! importName ) {
16+ return `import "${ source } ";` ;
17+ }
18+ if ( useDefault ) {
19+ return `import ${ importName } from "${ source } ";` ;
20+ }
21+ return `import { ${ importName } } from "${ source } ";` ;
2922} ;
3023
3124/**
32- * Generates code from an AST
33- * @param {import('@babel/types').Node } ast - The AST node to generate code from
34- * @returns {string } The generated code
35- */
36- const generateCode = ast => generate ( ast ) . code ;
37-
38- /**
39- * Creates an AST builder with configuration for React SSR/hydration using Preact
25+ * Creates a code builder with configuration for React SSR/hydration using Preact
4026 */
4127export default ( ) => {
4228 // Create base imports from JSX_IMPORTS configuration
@@ -48,66 +34,35 @@ export default () => {
4834
4935 /**
5036 * Generates code that hydrates a server-rendered React component on the client
51- * @param {import('@babel/types').Expression } component - The React component AST node to hydrate
37+ * @param {string } componentCode - The React component code as a string to hydrate
5238 * @returns {string } The generated client-side hydration code
5339 */
54- const buildClientProgram = component => {
55- const program = t . program ( [
40+ const buildClientProgram = ( componentCode ) => {
41+ return [
5642 ...baseImports ,
57- createImportDeclaration (
58- null ,
59- // Relative to ESBUILD_RESOLVE_DIR
60- './index.css'
61- ) ,
43+ createImportDeclaration ( null , './index.css' ) ,
6244 createImportDeclaration ( 'hydrate' , 'preact' , false ) ,
63-
64- // hydrate(component, document.getElementById("root"));
65- t . expressionStatement (
66- t . callExpression ( t . identifier ( 'hydrate' ) , [
67- component ,
68- t . callExpression (
69- t . memberExpression (
70- t . identifier ( 'document' ) ,
71- t . identifier ( 'getElementById' )
72- ) ,
73- [ t . stringLiteral ( 'root' ) ]
74- ) ,
75- ] )
76- ) ,
77- ] ) ;
78-
79- return generateCode ( program ) ;
45+ '' ,
46+ `hydrate(${ componentCode } , document.getElementById("root"));` ,
47+ ] . join ( '\n' ) ;
8048 } ;
8149
8250 /**
8351 * Generates code that renders a React component to string on the server
84- * @param {import('@babel/types').Expression } component - The React component AST node to render
52+ * @param {string } componentCode - The React component code as a string to render
8553 * @returns {string } The generated server-side rendering code
8654 */
87- const buildServerProgram = component => {
88- const program = t . program ( [
55+ const buildServerProgram = ( componentCode ) => {
56+ return [
8957 ...baseImports ,
90- createImportDeclaration (
91- 'renderToStringAsync' ,
92- 'preact-render-to-string' ,
93- false
94- ) ,
95-
96- // code = renderToStringAsync(component);
97- t . expressionStatement (
98- t . assignmentExpression (
99- '=' ,
100- t . identifier ( 'code' ) ,
101- t . callExpression ( t . identifier ( 'renderToStringAsync' ) , [ component ] )
102- )
103- ) ,
104- ] ) ;
105-
106- return generateCode ( program ) ;
58+ createImportDeclaration ( 'renderToStringAsync' , 'preact-render-to-string' , false ) ,
59+ '' ,
60+ `const code = renderToStringAsync(${ componentCode } );` ,
61+ ] . join ( '\n' ) ;
10762 } ;
10863
10964 return {
11065 buildClientProgram,
11166 buildServerProgram,
11267 } ;
113- } ;
68+ } ;
0 commit comments