From b66bdfac77f3b06fd8a4ad600986296a52139b67 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Tue, 28 Apr 2026 09:05:26 +0800 Subject: [PATCH] ui-next: fix indent for server-side files --- eslint.config.mjs | 2 +- packages/ui-next/index.ts | 167 ++++++++++++++++------------------ packages/ui-next/package.json | 2 +- 3 files changed, 81 insertions(+), 90 deletions(-) diff --git a/eslint.config.mjs b/eslint.config.mjs index 91bc1b7d2..87b138e4d 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -68,7 +68,7 @@ const config = react({ '**/{public,frontend}/**/*.{ts,tsx,page.js}', '**/plugins/**/*.page.{ts,js,tsx,jsx}', 'packages/ui-default/**/*.{ts,tsx,js,jsx}', - 'packages/ui-next/**/*.{ts,tsx,js,jsx}', + 'packages/ui-next/src/**/*.{ts,tsx,js,jsx}', ], languageOptions: { diff --git a/packages/ui-next/index.ts b/packages/ui-next/index.ts index 303e506ea..04db9e7fe 100644 --- a/packages/ui-next/index.ts +++ b/packages/ui-next/index.ts @@ -1,105 +1,96 @@ import fs from 'fs'; 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 c2k from 'koa2-connect/ts'; import { createServer, type Plugin } from 'vite'; +import { serializer } from '@hydrooj/framework'; +import type { Context } from 'hydrooj'; import type { PageData } from './src/context/page-data'; const INJECT_MARKER = ''; const buildInject = (str: string) => ``; function hydroPlugins(): Plugin { - const virtualModuleId = 'virtual:hydro-plugins'; - const resolvedVirtualModuleId = `\0${virtualModuleId}`; + const virtualModuleId = 'virtual:hydro-plugins'; + const resolvedVirtualModuleId = `\0${virtualModuleId}`; - return { - name: 'hydro-plugins', - resolveId(id) { - if (id === virtualModuleId) { - return resolvedVirtualModuleId; - } - return undefined; - }, - load(id) { - if (id === resolvedVirtualModuleId) { - const entries: string[] = []; - for (const addon of Object.values(global.addons)) { - const uiEntry = path.resolve(addon, 'ui', 'index.ts'); - if (fs.existsSync(uiEntry)) entries.push(uiEntry); - } - if (!entries.length) return 'export default [];'; - const imports = entries.map((e, i) => `import * as plugin${i} from '${e}';`).join('\n'); - const exports = `export default [${entries.map((_, i) => `plugin${i}`).join(', ')}];`; - return `${imports}\n${exports}`; - } - return undefined; - }, - }; + return { + name: 'hydro-plugins', + resolveId(id) { + if (id === virtualModuleId) { + return resolvedVirtualModuleId; + } + return undefined; + }, + load(id) { + if (id === resolvedVirtualModuleId) { + const entries: string[] = []; + for (const addon of Object.values(global.addons)) { + const uiEntry = path.resolve(addon, 'ui', 'index.ts'); + if (fs.existsSync(uiEntry)) entries.push(uiEntry); + } + if (!entries.length) return 'export default [];'; + const imports = entries.map((e, i) => `import * as plugin${i} from '${e}';`).join('\n'); + const exports = `export default [${entries.map((_, i) => `plugin${i}`).join(', ')}];`; + return `${imports}\n${exports}`; + } + return undefined; + }, + }; } export async function apply(ctx: Context) { - if (process.env.HYDRO_CLI) return; - // 现在只是开发环境的实现,生产环境的实现还未完成 - const vite = await createServer({ - clearScreen: false, - server: { - middlewareMode: true, - hmr: { - port: 3010, - }, - headers: { - 'Cross-Origin-Opener-Policy': 'same-origin', - 'Cross-Origin-Embedder-Policy': 'require-corp', - }, - // allowedHosts: ['beta.hydro.ac'], - }, - appType: 'custom', - root: __dirname, - base: '/', - plugins: [react(), hydroPlugins()], - optimizeDeps: { - esbuildOptions: { - plugins: [ - // @ts-ignore - importMetaUrlPlugin(), - ], - }, - }, - worker: { - format: 'es', - }, - }); - 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); - } - const html = fs.readFileSync(path.join(__dirname, 'index.html'), 'utf-8'); - ctx.server.registerRenderer('next', { - name: 'next', - accept: [], - output: 'html', - asFallback: true, - priority: 100, - async render(name, args, context) { - const data: PageData = { - HYDRO_INJECTED: true, - 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); - }, - }); + if (process.env.HYDRO_CLI) return; + // 现在只是开发环境的实现,生产环境的实现还未完成 + const vite = await createServer({ + clearScreen: false, + server: { + middlewareMode: true, + hmr: { + port: 3010, + }, + headers: { + 'Cross-Origin-Opener-Policy': 'same-origin', + 'Cross-Origin-Embedder-Policy': 'require-corp', + }, + // allowedHosts: ['beta.hydro.ac'], + }, + appType: 'custom', + root: __dirname, + base: '/', + plugins: [react(), hydroPlugins()], + worker: { + format: 'es', + }, + }); + 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); + } + const html = fs.readFileSync(path.join(__dirname, 'index.html'), 'utf-8'); + ctx.server.registerRenderer('next', { + name: 'next', + accept: [], + output: 'html', + asFallback: true, + priority: 100, + async render(name, args, context) { + const data: PageData = { + HYDRO_INJECTED: true, + 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 - return async () => { - await vite.close().catch((e) => console.error(e)); - }; + // eslint-disable-next-line consistent-return + return async () => { + await vite.close().catch((e) => console.error(e)); + }; } diff --git a/packages/ui-next/package.json b/packages/ui-next/package.json index 834cc6715..a1d9cc923 100644 --- a/packages/ui-next/package.json +++ b/packages/ui-next/package.json @@ -10,7 +10,6 @@ "preview": "vite preview" }, "dependencies": { - "koa2-connect": "^1.0.2", "path-to-regexp": "^8.4.2", "react": "^19.2.5", "react-dom": "^19.2.5" @@ -19,6 +18,7 @@ "@types/react": "^19.2.14", "@types/react-dom": "^19.2.3", "@vitejs/plugin-react": "^6.0.1", + "koa2-connect": "^1.0.2", "vite": "^8.0.10" } }