mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
ui-next: add hydroPlugins to dynamically load route plugins
This commit is contained in:
2 files changed
+29
-6
No files matched your search
@@ -622,7 +622,7 @@ export interface ProblemSearchOptions {
|
|||||||
|
|
||||||
export type ProblemSearch = (domainId: string, q: string, options?: ProblemSearchOptions) => Promise<ProblemSearchResponse>;
|
export type ProblemSearch = (domainId: string, q: string, options?: ProblemSearchOptions) => Promise<ProblemSearchResponse>;
|
||||||
|
|
||||||
export type UIInjectableFields = 'ProblemAdd' | 'Notification' | 'Nav' | 'UserDropdown' | 'DomainManage' | 'ControlPanel';
|
export type UIInjectableFields = 'ProblemAdd' | 'Notification' | 'Nav' | 'UserDropdown' | 'DomainManage' | 'ControlPanel' | 'Route';
|
||||||
export interface UI {
|
export interface UI {
|
||||||
nodes: Record<UIInjectableFields, any[]>;
|
nodes: Record<UIInjectableFields, any[]>;
|
||||||
getNodes: typeof import('./lib/ui').getNodes;
|
getNodes: typeof import('./lib/ui').getNodes;
|
||||||
|
|||||||
@@ -1,14 +1,37 @@
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import importMetaUrlPlugin from '@codingame/esbuild-import-meta-url-plugin';
|
import importMetaUrlPlugin from '@chialab/esbuild-plugin-meta-url';
|
||||||
import react from '@vitejs/plugin-react-swc';
|
import react from '@vitejs/plugin-react';
|
||||||
import c2k from 'koa2-connect/ts';
|
import c2k from 'koa2-connect/ts';
|
||||||
import { createServer } from 'vite';
|
import { createServer, type Plugin } from 'vite';
|
||||||
import { } from '@hydrooj/framework';
|
import { } from '@hydrooj/framework';
|
||||||
import { Context } from 'hydrooj';
|
import { Context, getNodes } from 'hydrooj';
|
||||||
|
|
||||||
const INJECT_MARKER = '<!-- __HYDRO_INJECTION__DO_NOT_REMOVE_THIS__ -->';
|
const INJECT_MARKER = '<!-- __HYDRO_INJECTION__DO_NOT_REMOVE_THIS__ -->';
|
||||||
|
|
||||||
|
function hydroPlugins(): Plugin {
|
||||||
|
const virtualModuleId = 'virtual:hydro-plugins';
|
||||||
|
const resolvedVirtualModuleId = `\0${virtualModuleId}`;
|
||||||
|
|
||||||
|
return {
|
||||||
|
name: 'hydro-plugins',
|
||||||
|
resolveId(id) {
|
||||||
|
if (id === virtualModuleId) {
|
||||||
|
return resolvedVirtualModuleId;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
load(id) {
|
||||||
|
if (id === resolvedVirtualModuleId) {
|
||||||
|
const plugins = getNodes('Route');
|
||||||
|
if (!plugins) return 'export default [];';
|
||||||
|
const imports = plugins.map((p, i) => `import plugin${i} from '${p.args.entry}';`).join('\n');
|
||||||
|
const exports = `export default [${plugins.map((p, i) => `plugin${i}`).join(', ')}];`;
|
||||||
|
return `${imports}\n${exports}`;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
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({
|
||||||
@@ -26,7 +49,7 @@ export async function apply(ctx: Context) {
|
|||||||
appType: 'custom',
|
appType: 'custom',
|
||||||
root: __dirname,
|
root: __dirname,
|
||||||
base: '/',
|
base: '/',
|
||||||
plugins: [react()],
|
plugins: [react(), hydroPlugins()],
|
||||||
optimizeDeps: {
|
optimizeDeps: {
|
||||||
esbuildOptions: {
|
esbuildOptions: {
|
||||||
plugins: [
|
plugins: [
|
||||||
|
|||||||
Reference in new issue
Block a user