mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-17 09:36:08 +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 UIInjectableFields = 'ProblemAdd' | 'Notification' | 'Nav' | 'UserDropdown' | 'DomainManage' | 'ControlPanel';
|
||||
export type UIInjectableFields = 'ProblemAdd' | 'Notification' | 'Nav' | 'UserDropdown' | 'DomainManage' | 'ControlPanel' | 'Route';
|
||||
export interface UI {
|
||||
nodes: Record<UIInjectableFields, any[]>;
|
||||
getNodes: typeof import('./lib/ui').getNodes;
|
||||
|
||||
@@ -1,14 +1,37 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import importMetaUrlPlugin from '@codingame/esbuild-import-meta-url-plugin';
|
||||
import react from '@vitejs/plugin-react-swc';
|
||||
import importMetaUrlPlugin from '@chialab/esbuild-plugin-meta-url';
|
||||
import react from '@vitejs/plugin-react';
|
||||
import c2k from 'koa2-connect/ts';
|
||||
import { createServer } from 'vite';
|
||||
import { createServer, type Plugin } from 'vite';
|
||||
import { } from '@hydrooj/framework';
|
||||
import { Context } from 'hydrooj';
|
||||
import { Context, getNodes } from 'hydrooj';
|
||||
|
||||
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) {
|
||||
if (process.env.HYDRO_CLI) return;
|
||||
const vite = await createServer({
|
||||
@@ -26,7 +49,7 @@ export async function apply(ctx: Context) {
|
||||
appType: 'custom',
|
||||
root: __dirname,
|
||||
base: '/',
|
||||
plugins: [react()],
|
||||
plugins: [react(), hydroPlugins()],
|
||||
optimizeDeps: {
|
||||
esbuildOptions: {
|
||||
plugins: [
|
||||
|
||||
Reference in new issue
Block a user