mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
ui-next: inject args into vite html
This commit is contained in:
3 files changed
+15
-4
No files matched your search
@@ -5,6 +5,7 @@
|
|||||||
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
|
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
<title>Hydro</title>
|
<title>Hydro</title>
|
||||||
|
<!-- __HYDRO_INJECTION__DO_NOT_REMOVE_THIS__ -->
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div id="root"></div>
|
<div id="root"></div>
|
||||||
|
|||||||
@@ -2,11 +2,13 @@ import fs from 'fs';
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
import importMetaUrlPlugin from '@codingame/esbuild-import-meta-url-plugin';
|
import importMetaUrlPlugin from '@codingame/esbuild-import-meta-url-plugin';
|
||||||
import react from '@vitejs/plugin-react-swc';
|
import react from '@vitejs/plugin-react-swc';
|
||||||
import c2k from 'koa2-connect';
|
import c2k from 'koa2-connect/ts';
|
||||||
import { createServer } from 'vite';
|
import { createServer } from 'vite';
|
||||||
import { } from '@hydrooj/framework';
|
import { } from '@hydrooj/framework';
|
||||||
import { Context } from 'hydrooj';
|
import { Context } from 'hydrooj';
|
||||||
|
|
||||||
|
const INJECT_MARKER = '<!-- __HYDRO_INJECTION__DO_NOT_REMOVE_THIS__ -->';
|
||||||
|
|
||||||
export async function apply(ctx: Context) {
|
export async function apply(ctx: Context) {
|
||||||
if (process.env.HYDRO_CLI) return;
|
if (process.env.HYDRO_CLI) return;
|
||||||
const vite = await createServer({
|
const vite = await createServer({
|
||||||
@@ -49,7 +51,15 @@ export async function apply(ctx: Context) {
|
|||||||
output: 'html',
|
output: 'html',
|
||||||
asFallback: false,
|
asFallback: false,
|
||||||
priority: 100,
|
priority: 100,
|
||||||
render: async (name, args, context) => await vite.transformIndexHtml(context.handler.context.req.url, html),
|
async render(name, args, context) {
|
||||||
|
const htmlToRender = html.replace(INJECT_MARKER, `<script id="__HYDRO_INJECTION__" type="application/json">${JSON.stringify({
|
||||||
|
HYDRO_INJECTED: true,
|
||||||
|
name,
|
||||||
|
args,
|
||||||
|
url: context.handler.context.req.url,
|
||||||
|
})}</script>`);
|
||||||
|
return await vite.transformIndexHtml(context.handler.context.req.url!, htmlToRender);
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// eslint-disable-next-line consistent-return
|
// eslint-disable-next-line consistent-return
|
||||||
|
|||||||
@@ -8,14 +8,14 @@
|
|||||||
"preview": "vite preview"
|
"preview": "vite preview"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@vitejs/plugin-react": "^6.0.1",
|
"koa2-connect": "^1.0.2",
|
||||||
"react": "^19.2.5",
|
"react": "^19.2.5",
|
||||||
"react-dom": "^19.2.5"
|
"react-dom": "^19.2.5"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/react": "^19.2.14",
|
"@types/react": "^19.2.14",
|
||||||
"@types/react-dom": "^19.2.3",
|
"@types/react-dom": "^19.2.3",
|
||||||
"@vitejs/plugin-react-swc": "^4.3.0",
|
"@vitejs/plugin-react": "^6.0.1",
|
||||||
"vite": "^8.0.10"
|
"vite": "^8.0.10"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user