Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(react): add react render #10

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 84 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/vite",
"version": "5.9.0",
"version": "5.10.0",
"description": "Vite plugin for Athenna Framework.",
"license": "MIT",
"author": "João Lenon <[email protected]>",
Expand Down Expand Up @@ -59,9 +59,11 @@
"devDependencies": {
"@athenna/common": "^5.5.0",
"@athenna/config": "^5.1.0",
"@athenna/ioc": "^5.0.0",
"@athenna/test": "^5.2.0",
"@athenna/tsconfig": "^5.0.0",
"@athenna/view": "^5.1.0",
"@types/react-dom": "^19.0.3",
"@typescript-eslint/eslint-plugin": "^7.18.0",
"@typescript-eslint/parser": "^7.18.0",
"commitizen": "^4.3.1",
Expand All @@ -76,6 +78,7 @@
"husky": "^3.1.0",
"lint-staged": "^12.5.0",
"prettier": "^2.8.8",
"react-dom": "^19.0.0",
"vite": "^6.0.6"
},
"c8": {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@

export * from '#src/types'
export * from '#src/vite/Vite'
export * from '#src/renders/React'
export * from '#src/fastify/FastifyVite'
53 changes: 53 additions & 0 deletions src/renders/React.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* @athenna/vite
*
* (c) João Lenon <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import type { Vite } from '#src/vite/Vite'

export class React {
/**
* Automatically compile a React component using Vite
* dev server and import it. In production the server
* manifest.json file will be read instead.
*
* @example
* ```ts
* const { createApp } = await React.loadComponent('src/resources/app/app.tsx')
* ```
*/
public static async loadComponent<T = any>(path: string): Promise<T> {
const vite: Vite = ioc
.safeUse('Athenna/Core/Server')
.getVitePlugin()
.getVite()

return vite.ssrLoadModule(path)
}

/**
* Render a React component to an HTML string.
*
* @example
* ```ts
* const { createApp } = await React.loadComponent('src/resources/app/app.tsx')
*
* const htmlElement = await React.renderComponent(createApp(request.baseUrl))
* ```
*/
public static async renderComponent(component: any) {
const reactDom = await import('react-dom/server')

if (!reactDom) {
throw new Error(
'ReactDOM is not installed to process rendering, please run "npm install react react-dom".'
)
}

return reactDom.renderToString(component)
}
}
8 changes: 0 additions & 8 deletions src/types/fastify/FastifyViteOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,6 @@ export type FastifyViteOptions = {
*/
ssrBuildDirectory?: string

/**
* Path to the SSR manifest file relative from the root of
* the application.
*
* @default Path.public('assets/server/.vite/manifest.json')
*/
ssrManifestFile?: string

/**
* A custom set of attributes to apply on all
* script tags injected by edge `@vite()` tag.
Expand Down
4 changes: 3 additions & 1 deletion src/vite/Vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,9 @@ export class Vite {
}

if (!this.ssrManifestCache) {
this.ssrManifestCache = this.readFileAsJSON(this.options.ssrManifestFile)
this.ssrManifestCache = this.readFileAsJSON(
`${this.options.ssrBuildDirectory}${path.sep}.vite${path.sep}manifest.json`
)
}

return this.ssrManifestCache!
Expand Down
Loading