Skip to content

Commit 4638183

Browse files
authored
Merge pull request #10 from AthennaIO/develop
feat(react): add react render
2 parents 52ea5b4 + cca9531 commit 4638183

File tree

6 files changed

+145
-12
lines changed

6 files changed

+145
-12
lines changed

package-lock.json

Lines changed: 84 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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>",
@@ -59,9 +59,11 @@
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",
@@ -76,6 +78,7 @@
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": {

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@
99

1010
export * from '#src/types'
1111
export * from '#src/vite/Vite'
12+
export * from '#src/renders/React'
1213
export * from '#src/fastify/FastifyVite'

src/renders/React.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
}

src/types/fastify/FastifyViteOptions.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff 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.

src/vite/Vite.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff 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!

0 commit comments

Comments
 (0)