File tree Expand file tree Collapse file tree 6 files changed +145
-12
lines changed
Expand file tree Collapse file tree 6 files changed +145
-12
lines changed Original file line number Diff line number Diff line change 11{
22 "name" : " @athenna/vite" ,
3- "version" : " 5.9 .0" ,
3+ "version" : " 5.10 .0" ,
44 "description" : " Vite plugin for Athenna Framework." ,
55 "license" : " MIT" ,
66 "author" : " João Lenon <lenon@athenna.io>" ,
5959 "devDependencies" : {
6060 "@athenna/common" : " ^5.5.0" ,
6161 "@athenna/config" : " ^5.1.0" ,
62+ "@athenna/ioc" : " ^5.0.0" ,
6263 "@athenna/test" : " ^5.2.0" ,
6364 "@athenna/tsconfig" : " ^5.0.0" ,
6465 "@athenna/view" : " ^5.1.0" ,
66+ "@types/react-dom" : " ^19.0.3" ,
6567 "@typescript-eslint/eslint-plugin" : " ^7.18.0" ,
6668 "@typescript-eslint/parser" : " ^7.18.0" ,
6769 "commitizen" : " ^4.3.1" ,
7678 "husky" : " ^3.1.0" ,
7779 "lint-staged" : " ^12.5.0" ,
7880 "prettier" : " ^2.8.8" ,
81+ "react-dom" : " ^19.0.0" ,
7982 "vite" : " ^6.0.6"
8083 },
8184 "c8" : {
Original file line number Diff line number Diff line change 99
1010export * from '#src/types'
1111export * from '#src/vite/Vite'
12+ export * from '#src/renders/React'
1213export * from '#src/fastify/FastifyVite'
Original file line number Diff line number Diff line change 1+ /**
2+ * @athenna /vite
3+ *
4+ * (c) João Lenon <lenon@athenna.io>
5+ *
6+ * For the full copyright and license information, please view the LICENSE
7+ * file that was distributed with this source code.
8+ */
9+
10+ import type { Vite } from '#src/vite/Vite'
11+
12+ export class React {
13+ /**
14+ * Automatically compile a React component using Vite
15+ * dev server and import it. In production the server
16+ * manifest.json file will be read instead.
17+ *
18+ * @example
19+ * ```ts
20+ * const { createApp } = await React.loadComponent('src/resources/app/app.tsx')
21+ * ```
22+ */
23+ public static async loadComponent < T = any > ( path : string ) : Promise < T > {
24+ const vite : Vite = ioc
25+ . safeUse ( 'Athenna/Core/Server' )
26+ . getVitePlugin ( )
27+ . getVite ( )
28+
29+ return vite . ssrLoadModule ( path )
30+ }
31+
32+ /**
33+ * Render a React component to an HTML string.
34+ *
35+ * @example
36+ * ```ts
37+ * const { createApp } = await React.loadComponent('src/resources/app/app.tsx')
38+ *
39+ * const htmlElement = await React.renderComponent(createApp(request.baseUrl))
40+ * ```
41+ */
42+ public static async renderComponent ( component : any ) {
43+ const reactDom = await import ( 'react-dom/server' )
44+
45+ if ( ! reactDom ) {
46+ throw new Error (
47+ 'ReactDOM is not installed to process rendering, please run "npm install react react-dom".'
48+ )
49+ }
50+
51+ return reactDom . renderToString ( component )
52+ }
53+ }
Original file line number Diff line number Diff line change @@ -76,14 +76,6 @@ export type FastifyViteOptions = {
7676 */
7777 ssrBuildDirectory ?: string
7878
79- /**
80- * Path to the SSR manifest file relative from the root of
81- * the application.
82- *
83- * @default Path.public('assets/server/.vite/manifest.json')
84- */
85- ssrManifestFile ?: string
86-
8779 /**
8880 * A custom set of attributes to apply on all
8981 * script tags injected by edge `@vite()` tag.
Original file line number Diff line number Diff line change @@ -490,7 +490,9 @@ export class Vite {
490490 }
491491
492492 if ( ! this . ssrManifestCache ) {
493- this . ssrManifestCache = this . readFileAsJSON ( this . options . ssrManifestFile )
493+ this . ssrManifestCache = this . readFileAsJSON (
494+ `${ this . options . ssrBuildDirectory } ${ path . sep } .vite${ path . sep } manifest.json`
495+ )
494496 }
495497
496498 return this . ssrManifestCache !
You can’t perform that action at this time.
0 commit comments