web-extend-plugin-vue2 0.3.0 → 0.3.2

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/index.d.ts CHANGED
@@ -1,54 +1,41 @@
1
- /**
2
- * 公共 API 的 TypeScript 声明;IDE 补全以本文件为准。
3
- */
4
-
5
- /** 清单侧路由声明(PRD),需配合 `adaptRouteDeclarations` */
6
1
  export type RouteDeclaration = {
7
2
  path: string
8
3
  name?: string
9
4
  title?: string
10
5
  meta?: Record<string, unknown>
11
6
  children?: RouteDeclaration[]
12
- /** 由宿主在 `adaptRouteDeclarations` 中解析为 Vue 组件 */
13
7
  componentRef: string
14
8
  }
15
9
 
16
- /** `bootstrapPlugins` 调用 `activator` 时的第二参数 */
17
- export type PluginActivateContext = {
18
- /** 清单条目的浅拷贝且已 `Object.freeze`,请勿就地修改嵌套对象 */
19
- pluginRecord: Readonly<Record<string, unknown>>
20
- }
21
-
22
- /**
23
- * 与 `vue-router` RouteConfig 兼容的宽松对象形态(未安装 vue 类型时仍可用)。
24
- * 安装 `vue` / `vue-router` 类型后可在业务代码中收窄。
25
- */
26
10
  export type VueRouteConfig = Record<string, unknown>
27
11
 
28
- /** 在合成 `name` / `meta.pluginId` 之前转换插件提交的 `RouteConfig` */
12
+ export type PluginRouteSnapshot = {
13
+ path?: string
14
+ name?: string | symbol
15
+ meta?: Record<string, unknown>
16
+ children?: PluginRouteSnapshot[]
17
+ [key: string]: unknown
18
+ }
19
+
29
20
  export type TransformRoutesFn = (ctx: {
30
21
  pluginId: string
31
22
  router: unknown
32
23
  routes: ReadonlyArray<VueRouteConfig>
33
24
  }) => VueRouteConfig[]
34
25
 
35
- /** 接管路由注册;由宿主决定是否调用 `applyInternalRegister` */
36
26
  export type InterceptRegisterRoutesFn = (ctx: {
37
27
  pluginId: string
38
28
  router: unknown
39
29
  routes: ReadonlyArray<VueRouteConfig>
40
- /** 与默认 `registerRoutes` 相同的包装 + `router.addRoute` */
41
30
  applyInternalRegister: (routes: VueRouteConfig[]) => void
42
31
  }) => void
43
32
 
44
- /** PRD → `RouteConfig` */
45
33
  export type AdaptRouteDeclarationsFn = (ctx: {
46
34
  pluginId: string
47
35
  router: unknown
48
36
  declarations: ReadonlyArray<RouteDeclaration>
49
37
  }) => VueRouteConfig[]
50
38
 
51
- /** 宿主在 `resolveRuntimeOptions({ hostContext })` 中注入;经浅冻结后由 `hostApi.hostContext` 只读暴露 */
52
39
  export type HostContext = Readonly<Record<string, unknown>>
53
40
 
54
41
  export type OnBeforePluginActivateFn = (ctx: {
@@ -71,48 +58,35 @@ export type OnPluginActivateErrorFn = (ctx: {
71
58
  hostApi: unknown
72
59
  }) => void | Promise<void>
73
60
 
74
- /** 宿主传入 `createHostApi` `hostKit`(及 `resolveRuntimeOptions` 中同类字段) */
75
- /** {@link createVueCliAxiosQuickInstallOptions} / {@link installVueCliAxiosWebPlugins} 的宿主侧依赖 */
76
- export type VueCliAxiosQuickDeps = {
77
- request: (config: Record<string, unknown>) => Promise<unknown>
78
- hostLayoutComponent: unknown
79
- store?: unknown
80
- hostContext?: Record<string, unknown>
81
- }
61
+ export type OnPluginRoutesContributedFn = (ctx: {
62
+ pluginId: string
63
+ router: unknown
64
+ routes: ReadonlyArray<VueRouteConfig>
65
+ contributedRoutes: ReadonlyArray<PluginRouteSnapshot>
66
+ }) => void | Promise<void>
82
67
 
83
68
  export type HostKitOptions = {
84
- /** `getBridge().request` 允许的URL路径前缀,须以 `/` 开头 */
85
69
  bridgeAllowedPathPrefixes?: string[]
86
- /** 非空时子路由挂到该命名父路由下(需 vue-router ≥3.5 的 `addRoute`) */
87
70
  pluginRoutesParentName?: string
88
71
  transformRoutes?: TransformRoutesFn
89
72
  interceptRegisterRoutes?: InterceptRegisterRoutesFn
90
73
  adaptRouteDeclarations?: AdaptRouteDeclarationsFn
91
- /** 由引导流程浅冻结后传入各插件的 `hostApi.hostContext` */
74
+ onPluginRoutesContributed?: OnPluginRoutesContributedFn
92
75
  hostContext?: HostContext
93
76
  }
94
77
 
95
- /** `resolveRuntimeOptions` 全量运行时选项(在默认字段基础上的扩展;未列字段见 README) */
96
78
  export type WebExtendPluginRuntimeOptions = HostKitOptions &
97
79
  Record<string, unknown> & {
98
80
  manifestBase?: string
99
81
  manifestListPath?: string
100
- /** 默认 `api`;`static` 时用 `staticManifestUrl` 拉取聚合 JSON,不请求 Java 清单接口 */
101
82
  manifestMode?: 'api' | 'static'
102
- /** 静态清单 URL(绝对或站点内路径),`manifestMode: 'static'` 时必填(或配置环境变量) */
103
83
  staticManifestUrl?: string
104
- /** `manifestMode=api` 且开发环境:API 失败或 `plugins` 为空时是否尝试 `devFallbackStaticManifestUrl`(默认 true) */
105
84
  devManifestFallback?: boolean
106
- /** 开发回退静态清单路径,默认 `/web-plugins/plugins.manifest.json` */
107
85
  devFallbackStaticManifestUrl?: string
108
86
  manifestFetchCredentials?: RequestCredentials
109
- /** 宿主 Layout,传入后自动注册 `pluginMountPath` 壳路由 */
110
87
  hostLayoutComponent?: unknown
111
- /** 插件 URL 前缀,默认 `/plugin` */
112
88
  pluginMountPath?: string
113
- /** 壳路由 meta */
114
89
  pluginHostRouteMeta?: Record<string, unknown>
115
- /** 为 true 时且配置了 `pluginRoutesParentName` + `hostLayoutComponent` 才自动注册壳路由;默认 false */
116
90
  ensurePluginHostRoute?: boolean
117
91
  isDev?: boolean
118
92
  webPluginDevOrigin?: string
@@ -126,26 +100,15 @@ export type WebExtendPluginRuntimeOptions = HostKitOptions &
126
100
  allowedScriptHosts?: string[]
127
101
  bootstrapSummary?: boolean
128
102
  fetchManifest?: ManifestFetchFn
129
- /** 插件通过 `hostApi.hostContext` 只读访问(浅冻结拷贝);如 `{ store, i18n }` */
130
103
  hostContext?: Record<string, unknown>
131
- /** 调用 `activator` 之前;抛出异常将跳过该插件并计入失败 */
132
104
  onBeforePluginActivate?: OnBeforePluginActivateFn
133
- /** `activator` 成功返回后 */
134
105
  onAfterPluginActivate?: OnAfterPluginActivateFn
135
- /** `activator` 抛错后;用于埋点/降级,不应再抛错(再抛仅记录 warn) */
136
106
  onPluginActivateError?: OnPluginActivateErrorFn
137
107
  }
138
108
 
139
- /** 插件 `activator` 收到的宿主 API */
140
109
  export interface HostApi {
141
- /** 宿主实现的 Host API 协议版本,与清单 `hostPluginApiVersion` 对齐 */
142
110
  readonly hostPluginApiVersion: string
143
- /** 宿主注入上下文(`resolveRuntimeOptions.hostContext` 的浅冻结快照);无则 `{}` */
144
111
  readonly hostContext: HostContext
145
- /**
146
- * 动态注册路由。含 PRD 时使用 `componentRef` 树;否则传入 `RouteConfig` 数组。
147
- * 流水线:`adaptRouteDeclarations` → `transformRoutes` → `interceptRegisterRoutes` 或默认注册。
148
- */
149
112
  registerRoutes(routes: VueRouteConfig[] | RouteDeclaration[]): void
150
113
  registerSlotComponents(
151
114
  pointId: string,
@@ -154,6 +117,7 @@ export interface HostApi {
154
117
  registerStylesheetUrls(urls?: string[]): void
155
118
  registerScriptUrls(urls?: string[]): void
156
119
  registerSanitizedHtmlSnippet(): void
120
+ getContributedRoutes(): PluginRouteSnapshot[]
157
121
  getBridge(): {
158
122
  request(path: string, init?: RequestInit): Promise<Response>
159
123
  }
@@ -181,126 +145,32 @@ export type ManifestFetchCacheOptions = {
181
145
  now?: () => number
182
146
  }
183
147
 
184
- /** 从路由 meta 推导的菜单节点(供宿主侧栏;与 `buildMenuDescriptorsFromRoutes` 配套) */
185
- export type PluginMenuDescriptor = {
186
- path: string
187
- name?: string | symbol
188
- title: string
189
- order: number
190
- pluginId?: string
191
- icon?: unknown
192
- permission?: unknown
193
- hidden?: boolean
194
- external?: boolean
195
- children?: PluginMenuDescriptor[]
196
- }
197
-
198
- /** 由插件 `RouteConfig` 树生成菜单描述(不含 component) */
199
- export function buildMenuDescriptorsFromRoutes(
200
- routes: VueRouteConfig[],
201
- pluginId?: string
202
- ): PluginMenuDescriptor[]
203
-
204
- /** 调试:返回 `registerRoutes` 为该插件登记的顶层 route name */
205
148
  export function getRegisteredTopRouteNamesForPlugin(pluginId: string): string[]
206
-
207
- /** 命名空间聚合对象,按领域分组 */
208
- export const WebExtendPluginVue2: Readonly<{
209
- install: (Vue: unknown, router: unknown, options?: WebExtendPluginRuntimeOptions) => Promise<void>
210
- runtime: Readonly<{
211
- /** 拉取清单并依次 `activator(hostApi, { pluginRecord })` */
212
- bootstrapPlugins: (
213
- router: unknown,
214
- createHostApiFactory: (id: string, r: unknown, kit?: HostKitOptions) => HostApi,
215
- runtimeOptions?: WebExtendPluginRuntimeOptions
216
- ) => Promise<void>
217
- resolveRuntimeOptions: (user?: WebExtendPluginRuntimeOptions) => WebExtendPluginRuntimeOptions
218
- ensurePluginHostRoute: (router: unknown, opts: WebExtendPluginRuntimeOptions) => void
219
- defaultFetchWebPluginManifest: (ctx: {
220
- manifestUrl: string
221
- credentials: RequestCredentials
222
- }) => Promise<{ ok: boolean; status?: number; data?: unknown; error?: unknown }>
223
- composeManifestFetch: (
224
- inner: ManifestFetchFn,
225
- ...middlewares: Array<(next: ManifestFetchFn) => ManifestFetchFn>
226
- ) => ManifestFetchFn
227
- manifestFetchCacheMiddleware: (options?: ManifestFetchCacheOptions) => (next: ManifestFetchFn) => ManifestFetchFn
228
- wrapManifestFetchWithCache: (inner: ManifestFetchFn, options?: ManifestFetchCacheOptions) => ManifestFetchFn
229
- }>
230
- host: Readonly<{
231
- createHostApi: (pluginId: string, router: unknown, hostKit?: HostKitOptions) => HostApi
232
- disposeWebPlugin: (pluginId: string) => void
233
- createRequestBridge: (config?: { allowedPathPrefixes?: string[] }) => {
234
- request: (path: string, init?: RequestInit) => Promise<Response>
235
- }
236
- registries: unknown
237
- buildMenuDescriptorsFromRoutes: typeof buildMenuDescriptorsFromRoutes
238
- getRegisteredTopRouteNamesForPlugin: typeof getRegisteredTopRouteNamesForPlugin
239
- }>
240
- config: Readonly<{
241
- defaultWebExtendPluginRuntime: Record<string, unknown>
242
- setWebExtendPluginEnv: (env: Record<string, unknown> | null | undefined) => void
243
- webExtendPluginEnvKeys: Readonly<Record<string, string>>
244
- defaultManifestFetchCache: Readonly<{ storageKeyPrefix: string; maxEntries: number }>
245
- defaultManifestMode: string
246
- routeSynthNamePrefix: string
247
- peerMinimumVersions: Readonly<{ vue: string; vueRouter: string }>
248
- }>
249
- constants: Readonly<{ HOST_PLUGIN_API_VERSION: string; RUNTIME_CONSOLE_LABEL: string }>
250
- components: Readonly<{ ExtensionPoint: unknown }>
251
- presets: Readonly<{
252
- vueCliAxios: Readonly<{
253
- id: string
254
- description: string
255
- createInstallOptions: (
256
- deps: { request: (config: Record<string, unknown>) => Promise<unknown> },
257
- extra?: Record<string, unknown>
258
- ) => Record<string, unknown>
259
- createQuickInstallOptions: (
260
- router: unknown,
261
- deps: VueCliAxiosQuickDeps,
262
- extra?: Record<string, unknown>
263
- ) => Record<string, unknown>
264
- defaultJavaManifestListPath: string
265
- manifestPathForApiBase: (manifestUrl: string, apiBase?: string) => string
266
- unwrapManifestBody: (body: unknown) => object | null
267
- }>
268
- }>
269
- }>
149
+ export function getContributedRoutesForPlugin(pluginId: string): PluginRouteSnapshot[]
270
150
 
271
151
  export const defaultWebExtendPluginRuntime: Record<string, unknown>
272
-
273
- /** 与 `resolveRuntimeOptions` / `build-env` 读取逻辑一致的环境变量键(文档与宿主检索用) */
274
152
  export const webExtendPluginEnvKeys: Readonly<Record<string, string>>
275
-
276
153
  export const defaultManifestFetchCache: Readonly<{ storageKeyPrefix: string; maxEntries: number }>
277
-
278
154
  export const defaultManifestMode: 'api'
279
-
280
- /** 无 name 的路由由 `createHostApi` 合成的名称前缀 */
281
155
  export const routeSynthNamePrefix: string
282
-
283
- /** 与 package.json peer 下限一致,供 CI / `test:peer-min` 对齐 */
284
156
  export const peerMinimumVersions: Readonly<{ vue: string; vueRouter: string }>
285
157
 
286
- /** 拉取清单并依次激活插件;浏览器环境执行 */
287
158
  export function bootstrapPlugins(
288
159
  router: unknown,
289
160
  createHostApiFactory: (id: string, r: unknown, kit?: HostKitOptions) => HostApi,
290
161
  runtimeOptions?: WebExtendPluginRuntimeOptions
291
162
  ): Promise<void>
292
163
 
293
- /** 合并默认配置、环境变量与显式传入字段 */
294
164
  export function resolveRuntimeOptions(user?: WebExtendPluginRuntimeOptions): WebExtendPluginRuntimeOptions
295
165
 
296
166
  export function ensurePluginHostRoute(router: unknown, opts: WebExtendPluginRuntimeOptions): void
167
+ export function getActivatedPluginIds(): string[]
297
168
 
298
169
  export function defaultFetchWebPluginManifest(ctx: {
299
170
  manifestUrl: string
300
171
  credentials: RequestCredentials
301
172
  }): Promise<{ ok: boolean; status?: number; data?: unknown; error?: unknown }>
302
173
 
303
- /** 构造单个插件在宿主侧的 `hostApi`,供 `activator(hostApi, context)` 使用 */
304
174
  export function createHostApi(pluginId: string, router: unknown, hostKit?: HostKitOptions): HostApi
305
175
 
306
176
  export function disposeWebPlugin(pluginId: string): void
@@ -311,53 +181,21 @@ export function createRequestBridge(config?: {
311
181
  }): { request: (path: string, init?: RequestInit) => Promise<Response> }
312
182
 
313
183
  export const HOST_PLUGIN_API_VERSION: string
314
- /** 控制台日志前缀用的短名称 */
315
184
  export const RUNTIME_CONSOLE_LABEL: string
316
185
  export function setWebExtendPluginEnv(env: Record<string, unknown> | null | undefined): void
317
186
 
318
- /** 注册全局 `ExtensionPoint` 并异步 `bootstrapPlugins` */
319
187
  export function installWebExtendPluginVue2(
320
188
  Vue: unknown,
321
189
  router: unknown,
322
190
  options?: WebExtendPluginRuntimeOptions
323
191
  ): Promise<void>
324
192
 
325
- /** Vue CLI + axios 一键安装;默认 `exposeGlobalVue: true` 写入 `window.Vue` */
326
- export function installVueCliAxiosWebPlugins(
327
- Vue: unknown,
328
- router: unknown,
329
- deps: VueCliAxiosQuickDeps,
330
- extra?: WebExtendPluginRuntimeOptions & { exposeGlobalVue?: boolean }
331
- ): Promise<void>
332
-
333
193
  export const ExtensionPoint: unknown
334
194
 
335
- /** 常见 Java 清单路径段,与 `manifestBase` 拼接 */
336
- export const defaultVueCliJavaManifestListPath: string
337
-
338
- export function createVueCliAxiosQuickInstallOptions(
339
- router: unknown,
340
- deps: VueCliAxiosQuickDeps,
341
- extra?: Record<string, unknown>
342
- ): Record<string, unknown>
343
-
344
195
  export function createVueCliAxiosInstallOptions(
345
196
  deps: { request: (config: Record<string, unknown>) => Promise<unknown> },
346
197
  extra?: Record<string, unknown>
347
198
  ): Record<string, unknown>
348
- /** 将完整 manifest URL 转为相对 axios `baseURL` 的请求路径 */
349
- export function resolveManifestPathUnderApiBase(manifestUrl: string, apiBase?: string): string
350
- /** 解包裸清单或与 `{ data: { plugins } }` 包装的响应体 */
351
- export function unwrapNestedManifestBody(body: unknown): object | null
352
- export const presetVueCliAxios: Readonly<{
353
- id: string
354
- description: string
355
- createInstallOptions: typeof createVueCliAxiosInstallOptions
356
- createQuickInstallOptions: typeof createVueCliAxiosQuickInstallOptions
357
- defaultJavaManifestListPath: typeof defaultVueCliJavaManifestListPath
358
- manifestPathForApiBase: typeof resolveManifestPathUnderApiBase
359
- unwrapManifestBody: typeof unwrapNestedManifestBody
360
- }>
361
199
 
362
200
  export function composeManifestFetch(
363
201
  inner: ManifestFetchFn,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "web-extend-plugin-vue2",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -21,7 +21,7 @@
21
21
  "index.d.ts"
22
22
  ],
23
23
  "peerDependencies": {
24
- "vue": ">=2.6.14 <3.0.0",
24
+ "vue": ">=2.6.0 <3.0.0",
25
25
  "vue-router": ">=3.5.0 <4.0.0"
26
26
  },
27
27
  "dependencies": {
@@ -35,15 +35,12 @@
35
35
  "rollup": "^4.28.0",
36
36
  "tslib": "^2.8.1",
37
37
  "typescript": "^5.7.2",
38
- "vitest": "^2.1.9",
39
38
  "vue": "^2.7.16",
40
39
  "vue-router": "^3.6.5"
41
40
  },
42
41
  "scripts": {
43
42
  "build": "rollup -c rollup.config.mjs",
44
- "test": "vitest run",
45
- "test:peer-min": "npm install --no-save vue@2.6.14 vue-router@3.5.4 && npm run build && npm test && npm install",
46
- "prepublishOnly": "npm run build && npm run test"
43
+ "prepublishOnly": "npm run build"
47
44
  },
48
45
  "license": "Apache-2.0",
49
46
  "repository": {