ui-next: fix indent for server-side files

This commit is contained in:
Baoshuo committed 2026-05-10 01:12:47 +08:00
1 parent 979cda7c1a
commit b66bdfac77
3 files changed
+81 -90

No files matched your search

+1 -1
View File
@@ -68,7 +68,7 @@ const config = react({
'**/{public,frontend}/**/*.{ts,tsx,page.js}', '**/{public,frontend}/**/*.{ts,tsx,page.js}',
'**/plugins/**/*.page.{ts,js,tsx,jsx}', '**/plugins/**/*.page.{ts,js,tsx,jsx}',
'packages/ui-default/**/*.{ts,tsx,js,jsx}', 'packages/ui-default/**/*.{ts,tsx,js,jsx}',
'packages/ui-next/**/*.{ts,tsx,js,jsx}', 'packages/ui-next/src/**/*.{ts,tsx,js,jsx}',
], ],
languageOptions: { languageOptions: {
+79 -88
View File
@@ -1,105 +1,96 @@
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
import { serializer } from '@hydrooj/framework';
import { Context } from 'hydrooj';
import importMetaUrlPlugin from '@chialab/esbuild-plugin-meta-url';
import react from '@vitejs/plugin-react'; import react from '@vitejs/plugin-react';
import c2k from 'koa2-connect/ts'; import c2k from 'koa2-connect/ts';
import { createServer, type Plugin } from 'vite'; import { createServer, type Plugin } from 'vite';
import { serializer } from '@hydrooj/framework';
import type { Context } from 'hydrooj';
import type { PageData } from './src/context/page-data'; import type { PageData } from './src/context/page-data';
const INJECT_MARKER = '<!-- __HYDRO_INJECTION__DO_NOT_REMOVE_THIS__ -->'; const INJECT_MARKER = '<!-- __HYDRO_INJECTION__DO_NOT_REMOVE_THIS__ -->';
const buildInject = (str: string) => `<script id="__HYDRO_INJECTION__" type="application/json">${str}</script>`; const buildInject = (str: string) => `<script id="__HYDRO_INJECTION__" type="application/json">${str}</script>`;
function hydroPlugins(): Plugin { function hydroPlugins(): Plugin {
const virtualModuleId = 'virtual:hydro-plugins'; const virtualModuleId = 'virtual:hydro-plugins';
const resolvedVirtualModuleId = `\0${virtualModuleId}`; const resolvedVirtualModuleId = `\0${virtualModuleId}`;
return { return {
name: 'hydro-plugins', name: 'hydro-plugins',
resolveId(id) { resolveId(id) {
if (id === virtualModuleId) { if (id === virtualModuleId) {
return resolvedVirtualModuleId; return resolvedVirtualModuleId;
} }
return undefined; return undefined;
}, },
load(id) { load(id) {
if (id === resolvedVirtualModuleId) { if (id === resolvedVirtualModuleId) {
const entries: string[] = []; const entries: string[] = [];
for (const addon of Object.values(global.addons)) { for (const addon of Object.values(global.addons)) {
const uiEntry = path.resolve(addon, 'ui', 'index.ts'); const uiEntry = path.resolve(addon, 'ui', 'index.ts');
if (fs.existsSync(uiEntry)) entries.push(uiEntry); if (fs.existsSync(uiEntry)) entries.push(uiEntry);
} }
if (!entries.length) return 'export default [];'; if (!entries.length) return 'export default [];';
const imports = entries.map((e, i) => `import * as plugin${i} from '${e}';`).join('\n'); const imports = entries.map((e, i) => `import * as plugin${i} from '${e}';`).join('\n');
const exports = `export default [${entries.map((_, i) => `plugin${i}`).join(', ')}];`; const exports = `export default [${entries.map((_, i) => `plugin${i}`).join(', ')}];`;
return `${imports}\n${exports}`; return `${imports}\n${exports}`;
} }
return undefined; return undefined;
}, },
}; };
} }
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({
clearScreen: false, clearScreen: false,
server: { server: {
middlewareMode: true, middlewareMode: true,
hmr: { hmr: {
port: 3010, port: 3010,
}, },
headers: { headers: {
'Cross-Origin-Opener-Policy': 'same-origin', 'Cross-Origin-Opener-Policy': 'same-origin',
'Cross-Origin-Embedder-Policy': 'require-corp', 'Cross-Origin-Embedder-Policy': 'require-corp',
}, },
// allowedHosts: ['beta.hydro.ac'], // allowedHosts: ['beta.hydro.ac'],
}, },
appType: 'custom', appType: 'custom',
root: __dirname, root: __dirname,
base: '/', base: '/',
plugins: [react(), hydroPlugins()], plugins: [react(), hydroPlugins()],
optimizeDeps: { worker: {
esbuildOptions: { format: 'es',
plugins: [ },
// @ts-ignore });
importMetaUrlPlugin(), const middleware = c2k(vite.middlewares);
], const capture = ['/@vite/', '/src/', '/node_modules/', '/@react-refresh', '/@fs', '/@id/'];
}, for (const route of capture) {
}, ctx.server.addCaptureRoute(route, middleware);
worker: { }
format: 'es', const html = fs.readFileSync(path.join(__dirname, 'index.html'), 'utf-8');
}, ctx.server.registerRenderer('next', {
}); name: 'next',
const middleware = c2k(vite.middlewares); accept: [],
const capture = ['/@vite/', '/src/', '/node_modules/', '/@react-refresh', '/@fs', '/@id/']; output: 'html',
for (const route of capture) { asFallback: true,
ctx.server.addCaptureRoute(route, middleware); priority: 100,
} async render(name, args, context) {
const html = fs.readFileSync(path.join(__dirname, 'index.html'), 'utf-8'); const data: PageData = {
ctx.server.registerRenderer('next', { HYDRO_INJECTED: true,
name: 'next', name: context.handler.context._matchedRouteName,
accept: [], args,
output: 'html', url: context.handler.context.req.url!,
asFallback: true, routeMap: ctx.server.routeMap,
priority: 100, };
async render(name, args, context) { const serialized = JSON.stringify(data, serializer(false, context.handler));
const data: PageData = { const htmlToRender = html.replace(INJECT_MARKER, buildInject(serialized));
HYDRO_INJECTED: true, return await vite.transformIndexHtml(context.handler.context.req.url!, htmlToRender);
name: context.handler.context._matchedRouteName, },
args, });
url: context.handler.context.req.url!,
routeMap: ctx.server.routeMap,
};
const serialized = JSON.stringify(data, serializer(false, context.handler));
const htmlToRender = html.replace(INJECT_MARKER, buildInject(serialized));
return await vite.transformIndexHtml(context.handler.context.req.url!, htmlToRender);
},
});
// eslint-disable-next-line consistent-return // eslint-disable-next-line consistent-return
return async () => { return async () => {
await vite.close().catch((e) => console.error(e)); await vite.close().catch((e) => console.error(e));
}; };
} }
+1 -1
View File
@@ -10,7 +10,6 @@
"preview": "vite preview" "preview": "vite preview"
}, },
"dependencies": { "dependencies": {
"koa2-connect": "^1.0.2",
"path-to-regexp": "^8.4.2", "path-to-regexp": "^8.4.2",
"react": "^19.2.5", "react": "^19.2.5",
"react-dom": "^19.2.5" "react-dom": "^19.2.5"
@@ -19,6 +18,7 @@
"@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": "^6.0.1", "@vitejs/plugin-react": "^6.0.1",
"koa2-connect": "^1.0.2",
"vite": "^8.0.10" "vite": "^8.0.10"
} }
} }