@ubean/modules 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +127 -0
- package/dist/index.js +408 -0
- package/package.json +4 -4
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { createHooks } from "hookable";
|
|
2
|
+
import { Plugin } from "vite";
|
|
3
|
+
import { ModuleDefinition, ResolvedConfig, ResolvedModule } from "@ubean/config";
|
|
4
|
+
import { MiddlewareHandler } from "hono";
|
|
5
|
+
import { UbeanApp } from "@ubean/app";
|
|
6
|
+
import { UbeanEnv } from "@ubean/types";
|
|
7
|
+
//#region src/kit.d.ts
|
|
8
|
+
interface DevToolsCustomTab {
|
|
9
|
+
id: string;
|
|
10
|
+
title: string;
|
|
11
|
+
icon?: string;
|
|
12
|
+
component?: any;
|
|
13
|
+
}
|
|
14
|
+
interface ModuleHooks {
|
|
15
|
+
'app:created': (app: UbeanApp) => void | Promise<void>;
|
|
16
|
+
'app:before:register': (app: UbeanApp) => void | Promise<void>;
|
|
17
|
+
'app:after:register': (app: UbeanApp) => void | Promise<void>;
|
|
18
|
+
'app:ready': (app: UbeanApp) => void | Promise<void>;
|
|
19
|
+
'build:before': () => void | Promise<void>;
|
|
20
|
+
'build:after': () => void | Promise<void>;
|
|
21
|
+
'dev:setup': () => void | Promise<void>;
|
|
22
|
+
'dev:listen': (info: {
|
|
23
|
+
port: number;
|
|
24
|
+
host: string;
|
|
25
|
+
url: string;
|
|
26
|
+
}) => void | Promise<void>;
|
|
27
|
+
'request:start': (c: any) => void | Promise<void>;
|
|
28
|
+
'request:end': (c: any, res: Response) => void | Promise<void>;
|
|
29
|
+
}
|
|
30
|
+
interface ModuleKitContext {
|
|
31
|
+
moduleName: string;
|
|
32
|
+
options: Record<string, unknown>;
|
|
33
|
+
hooks: {
|
|
34
|
+
hook: <T extends keyof ModuleHooks>(name: T, fn: ModuleHooks[T]) => void;
|
|
35
|
+
callHook: <T extends keyof ModuleHooks>(name: T, ...args: Parameters<ModuleHooks[T]>) => Promise<void>;
|
|
36
|
+
};
|
|
37
|
+
addServerHandler: (handler: ServerHandlerRegistration) => void;
|
|
38
|
+
addDevServerHandler: (handler: DevServerHandlerRegistration) => void;
|
|
39
|
+
addVitePlugin: (plugin: Plugin | Plugin[]) => void;
|
|
40
|
+
addVirtualImports: (imports: VirtualImportRegistration) => void;
|
|
41
|
+
addComponentsDir: (dir: ComponentsDirRegistration) => void;
|
|
42
|
+
addAutoImport: (imports: AutoImportRegistration) => void;
|
|
43
|
+
addDevToolsTab: (tab: DevToolsCustomTab) => void;
|
|
44
|
+
_vitePlugins: Plugin[];
|
|
45
|
+
_serverHandlers: ServerHandlerRegistration[];
|
|
46
|
+
_devServerHandlers: DevServerHandlerRegistration[];
|
|
47
|
+
_virtualImports: VirtualImportRegistration[];
|
|
48
|
+
_componentsDirs: ComponentsDirRegistration[];
|
|
49
|
+
_autoImports: AutoImportRegistration[];
|
|
50
|
+
_devToolsTabs: DevToolsCustomTab[];
|
|
51
|
+
}
|
|
52
|
+
interface ServerHandlerRegistration {
|
|
53
|
+
route: string;
|
|
54
|
+
handler: MiddlewareHandler<UbeanEnv> | ((...args: any[]) => any);
|
|
55
|
+
method?: string | string[];
|
|
56
|
+
middleware?: boolean;
|
|
57
|
+
}
|
|
58
|
+
interface DevServerHandlerRegistration {
|
|
59
|
+
route: string;
|
|
60
|
+
handler: MiddlewareHandler<UbeanEnv> | ((...args: any[]) => any);
|
|
61
|
+
method?: string | string[];
|
|
62
|
+
}
|
|
63
|
+
interface VirtualImportRegistration {
|
|
64
|
+
imports: Array<{
|
|
65
|
+
name: string;
|
|
66
|
+
as?: string;
|
|
67
|
+
from: string;
|
|
68
|
+
}>;
|
|
69
|
+
from?: string;
|
|
70
|
+
}
|
|
71
|
+
interface ComponentsDirRegistration {
|
|
72
|
+
path: string;
|
|
73
|
+
pattern?: string;
|
|
74
|
+
pathPrefix?: boolean;
|
|
75
|
+
global?: boolean;
|
|
76
|
+
}
|
|
77
|
+
interface AutoImportRegistration {
|
|
78
|
+
imports: Array<string | {
|
|
79
|
+
name: string;
|
|
80
|
+
as?: string;
|
|
81
|
+
from: string;
|
|
82
|
+
}>;
|
|
83
|
+
from?: string;
|
|
84
|
+
}
|
|
85
|
+
declare function createModuleKitContext(moduleName: string, options: Record<string, unknown>): ModuleKitContext;
|
|
86
|
+
declare function topologicalSort<T extends {
|
|
87
|
+
key: string;
|
|
88
|
+
name: string;
|
|
89
|
+
dependsOn: string[];
|
|
90
|
+
}>(modules: T[], keyToIndex: Map<string, number>): T[];
|
|
91
|
+
//#endregion
|
|
92
|
+
//#region src/builtins.d.ts
|
|
93
|
+
interface BuiltinModuleDefinition {
|
|
94
|
+
key: 'icon' | 'pwa' | 'auth' | 'image' | 'fonts' | 'electron' | 'ui';
|
|
95
|
+
modulePath: string;
|
|
96
|
+
factoryExport?: string;
|
|
97
|
+
pluginName: string;
|
|
98
|
+
}
|
|
99
|
+
declare const BUILTIN_MODULES: BuiltinModuleDefinition[];
|
|
100
|
+
declare function getBuiltinModuleByKey(key: string): BuiltinModuleDefinition | undefined;
|
|
101
|
+
declare function isBuiltinModuleConfig(value: unknown): value is {
|
|
102
|
+
disabled?: boolean;
|
|
103
|
+
} & Record<string, unknown>;
|
|
104
|
+
declare function extractBuiltinOptions(value: unknown): Record<string, unknown>;
|
|
105
|
+
declare function isBuiltinDisabled(value: unknown): boolean;
|
|
106
|
+
//#endregion
|
|
107
|
+
//#region src/index.d.ts
|
|
108
|
+
interface ResolveModulesOptions {
|
|
109
|
+
cwd: string;
|
|
110
|
+
config: ResolvedConfig;
|
|
111
|
+
builtinPlugins: Plugin[];
|
|
112
|
+
}
|
|
113
|
+
interface ResolveModulesResult {
|
|
114
|
+
plugins: Plugin[];
|
|
115
|
+
modules: ResolvedModule[];
|
|
116
|
+
setupFns: Array<(kit?: ModuleKitContext) => void | Promise<void>>;
|
|
117
|
+
hooks: ReturnType<typeof createHooks<ModuleHooks>>;
|
|
118
|
+
serverHandlers: ServerHandlerRegistration[];
|
|
119
|
+
devServerHandlers: DevServerHandlerRegistration[];
|
|
120
|
+
devToolsTabs: DevToolsCustomTab[];
|
|
121
|
+
}
|
|
122
|
+
declare function resolveModules(options: ResolveModulesOptions): Promise<ResolveModulesResult>;
|
|
123
|
+
declare function defineModule<TOptions = unknown>(def: ModuleDefinition & {
|
|
124
|
+
setup?: (options: TOptions, kit: ModuleKitContext) => void | Promise<void>;
|
|
125
|
+
}): ModuleDefinition;
|
|
126
|
+
//#endregion
|
|
127
|
+
export { type AutoImportRegistration, BUILTIN_MODULES, type BuiltinModuleDefinition, type ComponentsDirRegistration, type DevServerHandlerRegistration, type DevToolsCustomTab, type ModuleHooks, type ModuleKitContext, ResolveModulesOptions, ResolveModulesResult, type ServerHandlerRegistration, type VirtualImportRegistration, createModuleKitContext, defineModule, extractBuiltinOptions, getBuiltinModuleByKey, isBuiltinDisabled, isBuiltinModuleConfig, resolveModules, topologicalSort };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
import { createHooks } from "hookable";
|
|
2
|
+
//#region src/builtins.ts
|
|
3
|
+
const BUILTIN_MODULES = [
|
|
4
|
+
{
|
|
5
|
+
key: "icon",
|
|
6
|
+
modulePath: "@ubean/icon/vite",
|
|
7
|
+
factoryExport: "ubeanIconPlugin",
|
|
8
|
+
pluginName: "ubean:icon"
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
key: "pwa",
|
|
12
|
+
modulePath: "@ubean/pwa/vite",
|
|
13
|
+
factoryExport: "ubeanPwaPlugin",
|
|
14
|
+
pluginName: "ubean:pwa"
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
key: "auth",
|
|
18
|
+
modulePath: "@ubean/auth/vite",
|
|
19
|
+
factoryExport: "ubeanAuthPlugin",
|
|
20
|
+
pluginName: "ubean:auth"
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
key: "image",
|
|
24
|
+
modulePath: "@ubean/image/vite",
|
|
25
|
+
factoryExport: "ubeanImagePlugin",
|
|
26
|
+
pluginName: "ubean:image"
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
key: "fonts",
|
|
30
|
+
modulePath: "@ubean/fonts/vite",
|
|
31
|
+
factoryExport: "ubeanFontsPlugin",
|
|
32
|
+
pluginName: "ubean:fonts"
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
key: "electron",
|
|
36
|
+
modulePath: "@ubean/electron/vite",
|
|
37
|
+
factoryExport: "ubeanElectronPlugin",
|
|
38
|
+
pluginName: "ubean:electron"
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
key: "ui",
|
|
42
|
+
modulePath: "@ubean/ui/vite",
|
|
43
|
+
factoryExport: "ubeanUiPlugin",
|
|
44
|
+
pluginName: "ubean:ui"
|
|
45
|
+
}
|
|
46
|
+
];
|
|
47
|
+
function getBuiltinModuleByKey(key) {
|
|
48
|
+
return BUILTIN_MODULES.find((m) => m.key === key);
|
|
49
|
+
}
|
|
50
|
+
function isBuiltinModuleConfig(value) {
|
|
51
|
+
return value === true || typeof value === "object" && value !== null;
|
|
52
|
+
}
|
|
53
|
+
function extractBuiltinOptions(value) {
|
|
54
|
+
if (value === true) return {};
|
|
55
|
+
if (typeof value === "object" && value !== null) {
|
|
56
|
+
const { disabled: _disabled, ...options } = value;
|
|
57
|
+
return options;
|
|
58
|
+
}
|
|
59
|
+
return {};
|
|
60
|
+
}
|
|
61
|
+
function isBuiltinDisabled(value) {
|
|
62
|
+
if (value === false) return true;
|
|
63
|
+
if (value === true) return false;
|
|
64
|
+
if (typeof value === "object" && value !== null) return value.disabled === true;
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
//#endregion
|
|
68
|
+
//#region src/kit.ts
|
|
69
|
+
function createModuleKitContext(moduleName, options) {
|
|
70
|
+
const ctx = {
|
|
71
|
+
moduleName,
|
|
72
|
+
options,
|
|
73
|
+
hooks: {
|
|
74
|
+
hook: () => {},
|
|
75
|
+
callHook: async () => {}
|
|
76
|
+
},
|
|
77
|
+
addServerHandler: (handler) => {
|
|
78
|
+
ctx._serverHandlers.push(handler);
|
|
79
|
+
},
|
|
80
|
+
addDevServerHandler: (handler) => {
|
|
81
|
+
ctx._devServerHandlers.push(handler);
|
|
82
|
+
},
|
|
83
|
+
addVitePlugin: (plugin) => {
|
|
84
|
+
const plugins = Array.isArray(plugin) ? plugin : [plugin];
|
|
85
|
+
ctx._vitePlugins.push(...plugins);
|
|
86
|
+
},
|
|
87
|
+
addVirtualImports: (imports) => {
|
|
88
|
+
ctx._virtualImports.push(imports);
|
|
89
|
+
},
|
|
90
|
+
addComponentsDir: (dir) => {
|
|
91
|
+
ctx._componentsDirs.push(dir);
|
|
92
|
+
},
|
|
93
|
+
addAutoImport: (imports) => {
|
|
94
|
+
ctx._autoImports.push(imports);
|
|
95
|
+
},
|
|
96
|
+
addDevToolsTab: (tab) => {
|
|
97
|
+
ctx._devToolsTabs.push(tab);
|
|
98
|
+
},
|
|
99
|
+
_vitePlugins: [],
|
|
100
|
+
_serverHandlers: [],
|
|
101
|
+
_devServerHandlers: [],
|
|
102
|
+
_virtualImports: [],
|
|
103
|
+
_componentsDirs: [],
|
|
104
|
+
_autoImports: [],
|
|
105
|
+
_devToolsTabs: []
|
|
106
|
+
};
|
|
107
|
+
return ctx;
|
|
108
|
+
}
|
|
109
|
+
function topologicalSort(modules, keyToIndex) {
|
|
110
|
+
const inDegree = /* @__PURE__ */ new Map();
|
|
111
|
+
const graph = /* @__PURE__ */ new Map();
|
|
112
|
+
const sorted = [];
|
|
113
|
+
for (const mod of modules) {
|
|
114
|
+
inDegree.set(mod.key, 0);
|
|
115
|
+
graph.set(mod.key, []);
|
|
116
|
+
}
|
|
117
|
+
for (const mod of modules) for (const dep of mod.dependsOn) {
|
|
118
|
+
const depKey = dep;
|
|
119
|
+
if (keyToIndex.has(depKey)) {
|
|
120
|
+
graph.get(depKey).push(mod.key);
|
|
121
|
+
inDegree.set(mod.key, (inDegree.get(mod.key) || 0) + 1);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
const queue = [];
|
|
125
|
+
for (const mod of modules) if ((inDegree.get(mod.key) || 0) === 0) queue.push(mod.key);
|
|
126
|
+
while (queue.length > 0) {
|
|
127
|
+
const key = queue.shift();
|
|
128
|
+
const idx = keyToIndex.get(key);
|
|
129
|
+
if (idx !== void 0) sorted.push(modules[idx]);
|
|
130
|
+
for (const dependent of graph.get(key) || []) {
|
|
131
|
+
inDegree.set(dependent, (inDegree.get(dependent) || 0) - 1);
|
|
132
|
+
if ((inDegree.get(dependent) || 0) === 0) queue.push(dependent);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
if (sorted.length < modules.length) {
|
|
136
|
+
const sortedKeys = new Set(sorted.map((m) => m.key));
|
|
137
|
+
for (const mod of modules) if (!sortedKeys.has(mod.key)) sorted.push(mod);
|
|
138
|
+
}
|
|
139
|
+
return sorted;
|
|
140
|
+
}
|
|
141
|
+
//#endregion
|
|
142
|
+
//#region src/index.ts
|
|
143
|
+
const BUILTIN_CORE_KEYS = /* @__PURE__ */ new Set([
|
|
144
|
+
"__ubean_core__",
|
|
145
|
+
"__ubean_vue__",
|
|
146
|
+
"__ubean_islands__"
|
|
147
|
+
]);
|
|
148
|
+
const MODULE_DEF_KEYS = /* @__PURE__ */ new Set([
|
|
149
|
+
"vitePlugin",
|
|
150
|
+
"setup",
|
|
151
|
+
"hooks",
|
|
152
|
+
"dependsOn"
|
|
153
|
+
]);
|
|
154
|
+
function isModuleDefinition(obj) {
|
|
155
|
+
if (!obj || typeof obj !== "object" || Array.isArray(obj)) return false;
|
|
156
|
+
return Object.keys(obj).some((k) => MODULE_DEF_KEYS.has(k));
|
|
157
|
+
}
|
|
158
|
+
function isVitePlugin(obj) {
|
|
159
|
+
if (!obj || typeof obj !== "object" || Array.isArray(obj)) return false;
|
|
160
|
+
return typeof obj.name === "string" && !isModuleDefinition(obj);
|
|
161
|
+
}
|
|
162
|
+
function getModuleKey(mod, index) {
|
|
163
|
+
if (typeof mod === "string") return mod;
|
|
164
|
+
if (Array.isArray(mod)) {
|
|
165
|
+
const [factory] = mod;
|
|
166
|
+
if (typeof factory === "function") return factory.name || `__factory_${index}__`;
|
|
167
|
+
return `__tuple_${index}__`;
|
|
168
|
+
}
|
|
169
|
+
if (mod && typeof mod === "object") {
|
|
170
|
+
if (isModuleDefinition(mod)) return mod.name || `__def_${index}__`;
|
|
171
|
+
const plugin = mod;
|
|
172
|
+
if (plugin.name) return plugin.name;
|
|
173
|
+
}
|
|
174
|
+
return `__module_${index}__`;
|
|
175
|
+
}
|
|
176
|
+
function getModuleName(mod, key) {
|
|
177
|
+
if (typeof mod === "string") {
|
|
178
|
+
const parts = mod.split("/");
|
|
179
|
+
return parts[parts.length - 1] || mod;
|
|
180
|
+
}
|
|
181
|
+
if (Array.isArray(mod)) {
|
|
182
|
+
const [factory] = mod;
|
|
183
|
+
if (typeof factory === "function") return factory.name || key;
|
|
184
|
+
return key;
|
|
185
|
+
}
|
|
186
|
+
if (mod && typeof mod === "object") {
|
|
187
|
+
if (isModuleDefinition(mod) && mod.name) return mod.name;
|
|
188
|
+
const plugin = mod;
|
|
189
|
+
if (plugin.name) return plugin.name;
|
|
190
|
+
}
|
|
191
|
+
return key;
|
|
192
|
+
}
|
|
193
|
+
function extractPlugins(mod) {
|
|
194
|
+
if (!mod) return [];
|
|
195
|
+
if (Array.isArray(mod)) return mod.filter(isVitePlugin);
|
|
196
|
+
if (typeof mod === "object") {
|
|
197
|
+
if (isModuleDefinition(mod)) {
|
|
198
|
+
if (mod.vitePlugin) return Array.isArray(mod.vitePlugin) ? mod.vitePlugin.filter(isVitePlugin) : [mod.vitePlugin].filter(isVitePlugin);
|
|
199
|
+
return [];
|
|
200
|
+
}
|
|
201
|
+
if (isVitePlugin(mod)) return [mod];
|
|
202
|
+
}
|
|
203
|
+
return [];
|
|
204
|
+
}
|
|
205
|
+
function extractSetup(mod) {
|
|
206
|
+
if (isModuleDefinition(mod)) return mod.setup;
|
|
207
|
+
}
|
|
208
|
+
function extractHooks(mod) {
|
|
209
|
+
if (isModuleDefinition(mod)) return mod.hooks;
|
|
210
|
+
}
|
|
211
|
+
function extractDependsOn(mod) {
|
|
212
|
+
if (isModuleDefinition(mod)) return mod.dependsOn || [];
|
|
213
|
+
return [];
|
|
214
|
+
}
|
|
215
|
+
function userModulesHasBuiltin(userModules, builtin) {
|
|
216
|
+
for (const mod of userModules) if (typeof mod === "string") {
|
|
217
|
+
if (mod === builtin.modulePath || mod.endsWith(builtin.modulePath)) return true;
|
|
218
|
+
} else if (Array.isArray(mod)) {
|
|
219
|
+
const [factory] = mod;
|
|
220
|
+
if (typeof factory === "function") {
|
|
221
|
+
if (factory.name === `${builtin.pluginName.replace(":", "")}Plugin`) return true;
|
|
222
|
+
}
|
|
223
|
+
} else if (mod && typeof mod === "object") {
|
|
224
|
+
if (isModuleDefinition(mod) && mod.name === builtin.pluginName) return true;
|
|
225
|
+
if (mod.name === builtin.pluginName) return true;
|
|
226
|
+
}
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
async function loadBuiltinModule(builtin, configValue) {
|
|
230
|
+
const options = extractBuiltinOptions(configValue);
|
|
231
|
+
const kit = createModuleKitContext(builtin.pluginName, options);
|
|
232
|
+
try {
|
|
233
|
+
const mod = await import(
|
|
234
|
+
/* @vite-ignore */
|
|
235
|
+
builtin.modulePath
|
|
236
|
+
);
|
|
237
|
+
const factory = builtin.factoryExport ? mod[builtin.factoryExport] : mod.default || mod;
|
|
238
|
+
if (typeof factory === "function") {
|
|
239
|
+
const result = await factory(options, kit);
|
|
240
|
+
if (result) {
|
|
241
|
+
const plugins = extractPlugins(result);
|
|
242
|
+
const setup = extractSetup(result);
|
|
243
|
+
const hooks = extractHooks(result);
|
|
244
|
+
const dependsOn = extractDependsOn(result);
|
|
245
|
+
plugins.push(...kit._vitePlugins);
|
|
246
|
+
return {
|
|
247
|
+
key: builtin.modulePath,
|
|
248
|
+
name: builtin.pluginName,
|
|
249
|
+
plugins,
|
|
250
|
+
setup,
|
|
251
|
+
hooks,
|
|
252
|
+
dependsOn,
|
|
253
|
+
options,
|
|
254
|
+
kit
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
} catch {}
|
|
259
|
+
return null;
|
|
260
|
+
}
|
|
261
|
+
async function parseUserModule(mod, index) {
|
|
262
|
+
const key = getModuleKey(mod, index);
|
|
263
|
+
const name = getModuleName(mod, key);
|
|
264
|
+
const options = {};
|
|
265
|
+
const kit = createModuleKitContext(name, options);
|
|
266
|
+
let resolvedPlugins = [];
|
|
267
|
+
let setupFn;
|
|
268
|
+
let hooks;
|
|
269
|
+
let dependsOn = [];
|
|
270
|
+
if (typeof mod === "string") try {
|
|
271
|
+
const factoryMod = await import(
|
|
272
|
+
/* @vite-ignore */
|
|
273
|
+
mod
|
|
274
|
+
);
|
|
275
|
+
const factoryFn = factoryMod.default || factoryMod;
|
|
276
|
+
if (typeof factoryFn === "function") {
|
|
277
|
+
const result = await factoryFn(options, kit);
|
|
278
|
+
if (result) if (Array.isArray(result)) resolvedPlugins = extractPlugins(result);
|
|
279
|
+
else {
|
|
280
|
+
resolvedPlugins = extractPlugins(result);
|
|
281
|
+
setupFn = extractSetup(result);
|
|
282
|
+
hooks = extractHooks(result);
|
|
283
|
+
dependsOn = extractDependsOn(result);
|
|
284
|
+
if (isModuleDefinition(result) && result.name) kit.moduleName = result.name;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
} catch {
|
|
288
|
+
return null;
|
|
289
|
+
}
|
|
290
|
+
else if (Array.isArray(mod)) {
|
|
291
|
+
const [factory, factoryOptions] = mod;
|
|
292
|
+
Object.assign(options, factoryOptions || {});
|
|
293
|
+
if (typeof factory === "function") try {
|
|
294
|
+
const result = await factory(factoryOptions, kit);
|
|
295
|
+
if (result) if (Array.isArray(result)) resolvedPlugins = extractPlugins(result);
|
|
296
|
+
else {
|
|
297
|
+
resolvedPlugins = extractPlugins(result);
|
|
298
|
+
setupFn = extractSetup(result);
|
|
299
|
+
hooks = extractHooks(result);
|
|
300
|
+
dependsOn = extractDependsOn(result);
|
|
301
|
+
if (isModuleDefinition(result) && result.name) kit.moduleName = result.name;
|
|
302
|
+
}
|
|
303
|
+
} catch {
|
|
304
|
+
return null;
|
|
305
|
+
}
|
|
306
|
+
} else if (mod && typeof mod === "object") {
|
|
307
|
+
resolvedPlugins = extractPlugins(mod);
|
|
308
|
+
setupFn = extractSetup(mod);
|
|
309
|
+
hooks = extractHooks(mod);
|
|
310
|
+
dependsOn = extractDependsOn(mod);
|
|
311
|
+
if (isModuleDefinition(mod) && mod.name) kit.moduleName = mod.name;
|
|
312
|
+
} else return null;
|
|
313
|
+
resolvedPlugins.push(...kit._vitePlugins);
|
|
314
|
+
return {
|
|
315
|
+
key,
|
|
316
|
+
name: kit.moduleName || name,
|
|
317
|
+
plugins: resolvedPlugins,
|
|
318
|
+
setup: setupFn,
|
|
319
|
+
hooks,
|
|
320
|
+
dependsOn,
|
|
321
|
+
options,
|
|
322
|
+
kit
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
async function resolveModules(options) {
|
|
326
|
+
const { config, builtinPlugins } = options;
|
|
327
|
+
const { modules: userModules = [] } = config;
|
|
328
|
+
const moduleHooks = createHooks();
|
|
329
|
+
const parsed = [];
|
|
330
|
+
const parsedKeys = /* @__PURE__ */ new Set();
|
|
331
|
+
const plugins = [...builtinPlugins];
|
|
332
|
+
const serverHandlers = [];
|
|
333
|
+
const devServerHandlers = [];
|
|
334
|
+
const devToolsTabs = [];
|
|
335
|
+
for (const builtin of BUILTIN_MODULES) {
|
|
336
|
+
const configValue = config[builtin.key];
|
|
337
|
+
if (isBuiltinDisabled(configValue)) continue;
|
|
338
|
+
if (userModulesHasBuiltin(userModules, builtin)) continue;
|
|
339
|
+
const loaded = await loadBuiltinModule(builtin, configValue);
|
|
340
|
+
if (loaded && !BUILTIN_CORE_KEYS.has(loaded.key) && !parsedKeys.has(loaded.key)) {
|
|
341
|
+
parsedKeys.add(loaded.key);
|
|
342
|
+
parsed.push(loaded);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
for (let i = 0; i < userModules.length; i++) {
|
|
346
|
+
const mod = userModules[i];
|
|
347
|
+
const key = getModuleKey(mod, i);
|
|
348
|
+
if (BUILTIN_CORE_KEYS.has(key) || parsedKeys.has(key)) continue;
|
|
349
|
+
const parsedMod = await parseUserModule(mod, i);
|
|
350
|
+
if (parsedMod) {
|
|
351
|
+
parsedKeys.add(parsedMod.key);
|
|
352
|
+
parsed.push(parsedMod);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
const keyToIndex = /* @__PURE__ */ new Map();
|
|
356
|
+
for (let i = 0; i < parsed.length; i++) keyToIndex.set(parsed[i].key, i);
|
|
357
|
+
const sorted = topologicalSort(parsed, keyToIndex);
|
|
358
|
+
const resolvedModules = [];
|
|
359
|
+
const coreModule = {
|
|
360
|
+
name: "ubean-core",
|
|
361
|
+
key: "__ubean_core__",
|
|
362
|
+
plugins: builtinPlugins,
|
|
363
|
+
dependsOn: []
|
|
364
|
+
};
|
|
365
|
+
resolvedModules.push(coreModule);
|
|
366
|
+
for (const mod of sorted) {
|
|
367
|
+
if (mod.setup) {
|
|
368
|
+
const setup = mod.setup;
|
|
369
|
+
const kit = mod.kit;
|
|
370
|
+
kit.hooks = {
|
|
371
|
+
hook: ((name, fn) => moduleHooks.hook(name, fn)),
|
|
372
|
+
callHook: async (name, ...args) => {
|
|
373
|
+
await moduleHooks.callHook(name, ...args);
|
|
374
|
+
}
|
|
375
|
+
};
|
|
376
|
+
await setup(mod.options, kit);
|
|
377
|
+
}
|
|
378
|
+
if (mod.hooks) for (const [hookName, hookFn] of Object.entries(mod.hooks)) moduleHooks.hook(hookName, hookFn);
|
|
379
|
+
plugins.push(...mod.plugins);
|
|
380
|
+
plugins.push(...mod.kit._vitePlugins);
|
|
381
|
+
serverHandlers.push(...mod.kit._serverHandlers);
|
|
382
|
+
devServerHandlers.push(...mod.kit._devServerHandlers);
|
|
383
|
+
devToolsTabs.push(...mod.kit._devToolsTabs);
|
|
384
|
+
resolvedModules.push({
|
|
385
|
+
name: mod.name,
|
|
386
|
+
key: mod.key,
|
|
387
|
+
plugins: [...mod.plugins, ...mod.kit._vitePlugins],
|
|
388
|
+
options: mod.options,
|
|
389
|
+
setup: mod.setup,
|
|
390
|
+
hooks: mod.hooks,
|
|
391
|
+
dependsOn: mod.dependsOn
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
return {
|
|
395
|
+
plugins,
|
|
396
|
+
modules: resolvedModules,
|
|
397
|
+
setupFns: [],
|
|
398
|
+
hooks: moduleHooks,
|
|
399
|
+
serverHandlers,
|
|
400
|
+
devServerHandlers,
|
|
401
|
+
devToolsTabs
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
function defineModule(def) {
|
|
405
|
+
return def;
|
|
406
|
+
}
|
|
407
|
+
//#endregion
|
|
408
|
+
export { BUILTIN_MODULES, createModuleKitContext, defineModule, extractBuiltinOptions, getBuiltinModuleByKey, isBuiltinDisabled, isBuiltinModuleConfig, resolveModules, topologicalSort };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ubean/modules",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Module system for ubean (resolveModules, defineModule, BUILTIN_MODULES)",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
@@ -17,9 +17,9 @@
|
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"hookable": "^6.1.1",
|
|
20
|
-
"@ubean/
|
|
21
|
-
"@ubean/
|
|
22
|
-
"@ubean/types": "0.1.
|
|
20
|
+
"@ubean/app": "0.1.3",
|
|
21
|
+
"@ubean/config": "0.1.3",
|
|
22
|
+
"@ubean/types": "0.1.3"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
25
|
"@types/node": "^26.1.1",
|