|
1 | | -import { JSX_IMPORTS } from '../constants.mjs'; |
| 1 | +import { resolve } from 'node:path/posix'; |
| 2 | + |
| 3 | +import { JSX_IMPORTS, ROOT } from '../constants.mjs'; |
2 | 4 |
|
3 | 5 | /** |
4 | 6 | * Creates an ES Module `import` statement as a string, based on parameters. |
@@ -54,43 +56,35 @@ export default () => { |
54 | 56 | // Import client-side CSS styles. |
55 | 57 | // This ensures that styles used in the rendered app are loaded on the client. |
56 | 58 | // The use of `new URL(...).pathname` resolves the absolute path for `entrypoint.jsx`. |
57 | | - createImportDeclaration( |
58 | | - null, |
59 | | - new URL('../ui/index.css', import.meta.url).pathname |
60 | | - ), |
| 59 | + createImportDeclaration(null, resolve(ROOT, './ui/index.css')), |
61 | 60 |
|
62 | 61 | // Import `hydrate()` from Preact — needed to attach to server-rendered HTML. |
63 | 62 | // This is a named import (not default), hence `false` as the third argument. |
64 | 63 | createImportDeclaration('hydrate', 'preact', false), |
65 | 64 |
|
66 | | - '', |
67 | | - |
68 | 65 | // Hydration call: binds the component to an element with ID "root" |
69 | 66 | // This assumes SSR has placed matching HTML there, which, it has. |
70 | 67 | `hydrate(${componentCode}, document.getElementById("root"));`, |
71 | | - ].join('\n'); |
| 68 | + ].join(''); |
72 | 69 | }; |
73 | 70 |
|
74 | 71 | /** |
75 | 72 | * Builds a server-side rendering (SSR) program. |
76 | 73 | * |
77 | 74 | * @param {string} componentCode - Code expression representing a JSX component |
| 75 | + * @param {string} variable - The variable to output it to |
78 | 76 | */ |
79 | | - const buildServerProgram = componentCode => { |
| 77 | + const buildServerProgram = (componentCode, variable) => { |
80 | 78 | return [ |
81 | 79 | // JSX component imports |
82 | 80 | ...baseImports, |
83 | 81 |
|
84 | | - // Import `renderToStringAsync()` from Preact's SSR module |
85 | | - createImportDeclaration( |
86 | | - 'renderToStringAsync', |
87 | | - 'preact-render-to-string', |
88 | | - false |
89 | | - ), |
| 82 | + // Import Preact's SSR module |
| 83 | + createImportDeclaration('render', 'preact-render-to-string', false), |
90 | 84 |
|
91 | 85 | // Render the component to an HTML string |
92 | 86 | // The output can be embedded directly into the server's HTML template |
93 | | - `const code = renderToStringAsync(${componentCode});`, |
| 87 | + `const ${variable} = render(${componentCode});`, |
94 | 88 | ].join('\n'); |
95 | 89 | }; |
96 | 90 |
|
|
0 commit comments