web-extend-plugin-vue2 0.3.3 → 0.3.5

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/README.md CHANGED
@@ -5,7 +5,7 @@ Vue 2 host runtime for web plugin bootstrap, route registration, host API inject
5
5
  ## Install
6
6
 
7
7
  ```bash
8
- npm i web-extend-plugin-vue2
8
+ npm i web-extend-plugin-vue2@0.3.5
9
9
  ```
10
10
 
11
11
  Peer dependencies:
@@ -13,6 +13,10 @@ Peer dependencies:
13
13
  - `vue >= 2.6.0 < 3`
14
14
  - `vue-router >= 3.5.0 < 4`
15
15
 
16
+ Published package:
17
+
18
+ - `web-extend-plugin-vue2@0.3.5`
19
+
16
20
  ## Public API
17
21
 
18
22
  The package now exposes named exports only.
@@ -33,6 +37,9 @@ Utilities:
33
37
 
34
38
  - `createRequestBridge`
35
39
  - `createVueCliAxiosInstallOptions`
40
+ - `registerHostComponents`
41
+ - `registerVueGlobalComponents`
42
+ - `registerHostModules`
36
43
  - `composeManifestFetch`
37
44
  - `manifestFetchCacheMiddleware`
38
45
  - `wrapManifestFetchWithCache`
@@ -90,6 +97,37 @@ installWebExtendPluginVue2(
90
97
  ).catch(console.warn)
91
98
  ```
92
99
 
100
+ ## Host exposes
101
+
102
+ Preferred host integration uses concrete component/module exposes instead of relying on `hostCapabilities`.
103
+
104
+ ```js
105
+ import {
106
+ installWebExtendPluginVue2,
107
+ createVueCliAxiosInstallOptions,
108
+ registerHostComponents,
109
+ registerVueGlobalComponents,
110
+ registerHostModules
111
+ } from 'web-extend-plugin-vue2'
112
+
113
+ registerVueGlobalComponents(Vue, {
114
+ include: (name) => /^El[A-Z]/.test(name),
115
+ mapName: (name) => name.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase()
116
+ })
117
+
118
+ registerHostComponents({
119
+ 'app.pagination': Pagination,
120
+ 'app.dict-tag': DictTag
121
+ })
122
+
123
+ registerHostModules({
124
+ request,
125
+ download
126
+ })
127
+ ```
128
+
129
+ Plugins should depend on stable expose names such as `el-button`, `app.pagination`, or `request`.
130
+
93
131
  ## Important runtime options
94
132
 
95
133
  - `manifestBase`: API prefix, default `/fp-api`
@@ -105,6 +143,7 @@ installWebExtendPluginVue2(
105
143
  - `pluginRoutesParentName`: explicit parent route name for child plugin routes
106
144
  - `ensurePluginHostRoute`: when `true`, auto-registers the shell route
107
145
  - `hostContext`: readonly host dependencies injected into `hostApi.hostContext`
146
+ - `hostCapabilities`: optional legacy metadata; prefer `registerHostComponents` and `registerHostModules`
108
147
 
109
148
  ## Notes
110
149
 
package/dist/index.cjs CHANGED
@@ -81,7 +81,7 @@ function getEnvObject() {
81
81
  return raw.__WEP_ENV__;
82
82
  }
83
83
  }
84
- catch {
84
+ catch (_a) {
85
85
  /* ignore */
86
86
  }
87
87
  return null;
@@ -117,7 +117,7 @@ function readProcessEnv(key) {
117
117
  }
118
118
  }
119
119
  }
120
- catch {
120
+ catch (_a) {
121
121
  /* ignore */
122
122
  }
123
123
  return undefined;
@@ -143,7 +143,7 @@ function resolveBundledIsDev() {
143
143
  return true;
144
144
  }
145
145
  }
146
- catch {
146
+ catch (_a) {
147
147
  /* ignore */
148
148
  }
149
149
  try {
@@ -151,7 +151,7 @@ function resolveBundledIsDev() {
151
151
  return true;
152
152
  }
153
153
  }
154
- catch {
154
+ catch (_b) {
155
155
  /* ignore */
156
156
  }
157
157
  return false;
@@ -226,7 +226,7 @@ function isScriptHostAllowed(url, hostSet) {
226
226
  const h = normalizeHost(u.hostname);
227
227
  return hostSet.has(h);
228
228
  }
229
- catch {
229
+ catch (_a) {
230
230
  return false;
231
231
  }
232
232
  }
@@ -401,6 +401,12 @@ function resolveRuntimeOptions$1(user = {}) {
401
401
  !Array.isArray(user.hostContext)
402
402
  ? { hostContext: user.hostContext }
403
403
  : {}),
404
+ ...(user.hostCapabilities !== undefined &&
405
+ user.hostCapabilities !== null &&
406
+ typeof user.hostCapabilities === 'object' &&
407
+ !Array.isArray(user.hostCapabilities)
408
+ ? { hostCapabilities: user.hostCapabilities }
409
+ : {}),
404
410
  ...(typeof user.onBeforePluginActivate === 'function'
405
411
  ? { onBeforePluginActivate: user.onBeforePluginActivate }
406
412
  : {}),
@@ -3432,7 +3438,7 @@ function parseWebPluginDevMapExplicit(opts) {
3432
3438
  const map = JSON.parse(String(raw));
3433
3439
  return map && typeof map === 'object' ? map : null;
3434
3440
  }
3435
- catch {
3441
+ catch (_a) {
3436
3442
  console.warn('[wep] invalid webPluginDevMapJson / VITE_WEB_PLUGIN_DEV_MAP');
3437
3443
  return null;
3438
3444
  }
@@ -3484,7 +3490,7 @@ async function buildImplicitWebPluginDevMap(opts, hostSet) {
3484
3490
  return {};
3485
3491
  }
3486
3492
  }
3487
- catch {
3493
+ catch (_a) {
3488
3494
  return {};
3489
3495
  }
3490
3496
  const pathPart = opts.webPluginDevEntryPath;
@@ -3508,7 +3514,7 @@ function closeAllPluginDevEventSources() {
3508
3514
  try {
3509
3515
  es.close();
3510
3516
  }
3511
- catch {
3517
+ catch (_a) {
3512
3518
  /* ignore */
3513
3519
  }
3514
3520
  }
@@ -3526,7 +3532,7 @@ function isDevOriginAllowedForSse(origin, hostSet) {
3526
3532
  const u = new URL(origin);
3527
3533
  return hostSet.has(normalizeHost(u.hostname));
3528
3534
  }
3529
- catch {
3535
+ catch (_a) {
3530
3536
  return false;
3531
3537
  }
3532
3538
  }
@@ -3570,7 +3576,7 @@ function startPluginDevSseForMap(devMap, isDev, hostSet, ssePath) {
3570
3576
  try {
3571
3577
  origins.add(new URL(t, window.location.href).origin);
3572
3578
  }
3573
- catch {
3579
+ catch (_a) {
3574
3580
  /* skip */
3575
3581
  }
3576
3582
  }
@@ -3692,15 +3698,254 @@ function ensurePluginHostRoute$1(router, opts) {
3692
3698
  });
3693
3699
  }
3694
3700
 
3695
- /**
3696
- * 宿主通过 `resolveRuntimeOptions({ hostContext })` 注入的只读上下文,
3697
- * 经浅拷贝 + 浅冻结后挂到 `hostApi.hostContext`,供插件访问 store/i18n 等而不污染 HostApi 顶层命名。
3698
- */
3699
- function freezeShallowHostContext(input) {
3700
- if (input == null || typeof input !== 'object' || Array.isArray(input)) {
3701
- return Object.freeze({});
3701
+ const hostComponentsRegistry = Object.create(null);
3702
+ const hostModulesRegistry = Object.create(null);
3703
+ function normalizeExposeMeta(raw) {
3704
+ const meta = {};
3705
+ if (typeof raw.title === 'string' && raw.title.trim()) {
3706
+ meta.title = raw.title.trim();
3707
+ }
3708
+ if (typeof raw.description === 'string' && raw.description.trim()) {
3709
+ meta.description = raw.description.trim();
3710
+ }
3711
+ return meta;
3712
+ }
3713
+ function normalizeHostComponentEntry(entry) {
3714
+ if (!entry) {
3715
+ return null;
3716
+ }
3717
+ if (typeof entry === 'object' && !Array.isArray(entry)) {
3718
+ const record = entry;
3719
+ if ('component' in record) {
3720
+ return {
3721
+ component: record.component,
3722
+ ...normalizeExposeMeta(record)
3723
+ };
3724
+ }
3725
+ }
3726
+ return { component: entry };
3727
+ }
3728
+ function normalizeHostModuleEntry(entry) {
3729
+ if (!entry) {
3730
+ return null;
3731
+ }
3732
+ if (typeof entry === 'object' && !Array.isArray(entry)) {
3733
+ const record = entry;
3734
+ if ('module' in record) {
3735
+ return {
3736
+ module: record.module,
3737
+ ...normalizeExposeMeta(record)
3738
+ };
3739
+ }
3740
+ }
3741
+ return { module: entry };
3742
+ }
3743
+ function normalizeNamedEntries(input, normalizeEntry) {
3744
+ if (Array.isArray(input)) {
3745
+ return input
3746
+ .map((item) => {
3747
+ const name = typeof item.name === 'string' ? item.name.trim() : '';
3748
+ const entry = normalizeEntry(item);
3749
+ if (!name || !entry) {
3750
+ return null;
3751
+ }
3752
+ return { name, entry };
3753
+ })
3754
+ .filter(Boolean);
3755
+ }
3756
+ return Object.entries(input || {})
3757
+ .map(([rawName, rawEntry]) => {
3758
+ const name = String(rawName).trim();
3759
+ const entry = normalizeEntry(rawEntry);
3760
+ if (!name || !entry) {
3761
+ return null;
3762
+ }
3763
+ return { name, entry };
3764
+ })
3765
+ .filter(Boolean);
3766
+ }
3767
+ function cloneMetaMap(registry) {
3768
+ const out = {};
3769
+ for (const [name, entry] of Object.entries(registry)) {
3770
+ out[name] = { ...entry.meta };
3771
+ }
3772
+ return out;
3773
+ }
3774
+ function resolveVueGlobalComponentSource(VueRuntime) {
3775
+ if (!VueRuntime || typeof VueRuntime !== 'object' || Array.isArray(VueRuntime)) {
3776
+ return undefined;
3777
+ }
3778
+ const maybeOptions = VueRuntime.options;
3779
+ if (!maybeOptions || typeof maybeOptions !== 'object' || Array.isArray(maybeOptions)) {
3780
+ return undefined;
3781
+ }
3782
+ const maybeComponents = maybeOptions.components;
3783
+ return maybeComponents && typeof maybeComponents === 'object' && !Array.isArray(maybeComponents)
3784
+ ? maybeComponents
3785
+ : undefined;
3786
+ }
3787
+ function resolveHostComponentMeta(metaInput, name, component) {
3788
+ const raw = typeof metaInput === 'function' ? metaInput(name, component) : metaInput;
3789
+ return raw && typeof raw === 'object' && !Array.isArray(raw)
3790
+ ? normalizeExposeMeta(raw)
3791
+ : {};
3792
+ }
3793
+ function registerHostComponents(input) {
3794
+ const entries = normalizeNamedEntries(input, normalizeHostComponentEntry);
3795
+ for (const { name, entry } of entries) {
3796
+ hostComponentsRegistry[name] = {
3797
+ value: entry.component,
3798
+ meta: normalizeExposeMeta(entry)
3799
+ };
3800
+ }
3801
+ return getAllHostComponentMeta();
3802
+ }
3803
+ function registerHostModules(input) {
3804
+ const entries = normalizeNamedEntries(input, normalizeHostModuleEntry);
3805
+ for (const { name, entry } of entries) {
3806
+ hostModulesRegistry[name] = {
3807
+ value: entry.module,
3808
+ meta: normalizeExposeMeta(entry)
3809
+ };
3810
+ }
3811
+ return getAllHostModuleMeta();
3812
+ }
3813
+ function registerVueGlobalComponents(VueRuntime, options = {}) {
3814
+ const source = options.source && typeof options.source === 'object' && !Array.isArray(options.source)
3815
+ ? options.source
3816
+ : resolveVueGlobalComponentSource(VueRuntime);
3817
+ if (!source) {
3818
+ return getAllHostComponentMeta();
3819
+ }
3820
+ const registry = {};
3821
+ for (const [rawName, component] of Object.entries(source)) {
3822
+ const sourceName = String(rawName).trim();
3823
+ if (!sourceName || component == null) {
3824
+ continue;
3825
+ }
3826
+ if (typeof options.include === 'function' && !options.include(sourceName, component)) {
3827
+ continue;
3828
+ }
3829
+ const mappedName = typeof options.mapName === 'function' ? options.mapName(sourceName, component) : sourceName;
3830
+ const exposeName = typeof mappedName === 'string' ? mappedName.trim() : '';
3831
+ if (!exposeName) {
3832
+ continue;
3833
+ }
3834
+ registry[exposeName] = {
3835
+ component,
3836
+ ...resolveHostComponentMeta(options.meta, sourceName, component)
3837
+ };
3838
+ }
3839
+ return registerHostComponents(registry);
3840
+ }
3841
+ function getHostComponent(name) {
3842
+ const key = typeof name === 'string' ? name.trim() : '';
3843
+ if (!key) {
3844
+ return undefined;
3845
+ }
3846
+ const record = hostComponentsRegistry[key];
3847
+ return record ? record.value : undefined;
3848
+ }
3849
+ function getHostModule(name) {
3850
+ const key = typeof name === 'string' ? name.trim() : '';
3851
+ if (!key) {
3852
+ return undefined;
3853
+ }
3854
+ const record = hostModulesRegistry[key];
3855
+ return record ? record.value : undefined;
3856
+ }
3857
+ function getHostComponentMeta(name) {
3858
+ const key = typeof name === 'string' ? name.trim() : '';
3859
+ const record = key ? hostComponentsRegistry[key] : undefined;
3860
+ const meta = record ? record.meta : undefined;
3861
+ return meta ? { ...meta } : undefined;
3862
+ }
3863
+ function getHostModuleMeta(name) {
3864
+ const key = typeof name === 'string' ? name.trim() : '';
3865
+ const record = key ? hostModulesRegistry[key] : undefined;
3866
+ const meta = record ? record.meta : undefined;
3867
+ return meta ? { ...meta } : undefined;
3868
+ }
3869
+ function getAllHostComponentMeta() {
3870
+ return cloneMetaMap(hostComponentsRegistry);
3871
+ }
3872
+ function getAllHostModuleMeta() {
3873
+ return cloneMetaMap(hostModulesRegistry);
3874
+ }
3875
+ function normalizeHostCapabilities(input) {
3876
+ const moduleMeta = getAllHostModuleMeta();
3877
+ const componentMeta = getAllHostComponentMeta();
3878
+ if (!input || typeof input !== 'object' || Array.isArray(input)) {
3879
+ const fallback = {};
3880
+ if (Object.keys(moduleMeta).length > 0) {
3881
+ fallback.modules = moduleMeta;
3882
+ }
3883
+ if (Object.keys(componentMeta).length > 0) {
3884
+ fallback.components = componentMeta;
3885
+ }
3886
+ return Object.keys(fallback).length > 0 ? Object.freeze(fallback) : undefined;
3887
+ }
3888
+ const raw = input;
3889
+ const normalized = { ...raw };
3890
+ if (raw.ui && typeof raw.ui === 'object' && !Array.isArray(raw.ui)) {
3891
+ const uiRaw = raw.ui;
3892
+ const ui = {};
3893
+ if (typeof uiRaw.framework === 'string' && uiRaw.framework.trim()) {
3894
+ ui.framework = uiRaw.framework.trim();
3895
+ }
3896
+ if (typeof uiRaw.componentLibrary === 'string' && uiRaw.componentLibrary.trim()) {
3897
+ ui.componentLibrary = uiRaw.componentLibrary.trim();
3898
+ }
3899
+ normalized.ui = ui;
3900
+ }
3901
+ const mergedModules = { ...moduleMeta };
3902
+ if (raw.modules && typeof raw.modules === 'object' && !Array.isArray(raw.modules)) {
3903
+ for (const [name, meta] of Object.entries(raw.modules)) {
3904
+ if (!name.trim()) {
3905
+ continue;
3906
+ }
3907
+ mergedModules[name.trim()] =
3908
+ meta && typeof meta === 'object' && !Array.isArray(meta)
3909
+ ? {
3910
+ ...(mergedModules[name.trim()] || {}),
3911
+ ...normalizeExposeMeta(meta)
3912
+ }
3913
+ : { ...(mergedModules[name.trim()] || {}) };
3914
+ }
3915
+ }
3916
+ const mergedComponents = { ...componentMeta };
3917
+ if (raw.components && typeof raw.components === 'object' && !Array.isArray(raw.components)) {
3918
+ for (const [name, meta] of Object.entries(raw.components)) {
3919
+ if (!name.trim()) {
3920
+ continue;
3921
+ }
3922
+ mergedComponents[name.trim()] =
3923
+ meta && typeof meta === 'object' && !Array.isArray(meta)
3924
+ ? {
3925
+ ...(mergedComponents[name.trim()] || {}),
3926
+ ...normalizeExposeMeta(meta)
3927
+ }
3928
+ : { ...(mergedComponents[name.trim()] || {}) };
3929
+ }
3930
+ }
3931
+ if (Object.keys(mergedModules).length > 0) {
3932
+ normalized.modules = mergedModules;
3933
+ }
3934
+ if (Object.keys(mergedComponents).length > 0) {
3935
+ normalized.components = mergedComponents;
3936
+ }
3937
+ return Object.freeze(normalized);
3938
+ }
3939
+
3940
+ function freezeShallowHostContext(input, explicitCapabilities) {
3941
+ const base = input != null && typeof input === 'object' && !Array.isArray(input)
3942
+ ? { ...input }
3943
+ : {};
3944
+ const mergedCapabilities = normalizeHostCapabilities(explicitCapabilities !== undefined ? explicitCapabilities : base.capabilities);
3945
+ if (mergedCapabilities) {
3946
+ base.capabilities = mergedCapabilities;
3702
3947
  }
3703
- return Object.freeze({ ...input });
3948
+ return Object.freeze(base);
3704
3949
  }
3705
3950
 
3706
3951
  /**
@@ -3722,7 +3967,7 @@ function resolveStaticManifestUrlForFetch(url, origin) {
3722
3967
  const pathPart = u.startsWith('/') ? u : `/${u}`;
3723
3968
  return new URL(pathPart, o).href;
3724
3969
  }
3725
- catch {
3970
+ catch (_a) {
3726
3971
  return u;
3727
3972
  }
3728
3973
  }
@@ -3908,7 +4153,7 @@ createHostApiFactory, runtimeOptions) {
3908
4153
  }
3909
4154
  const devMap = mergeDevMaps(implicit, explicit);
3910
4155
  startPluginDevSseForMap(devMap, opts.isDev, hostSet, opts.devReloadSsePath);
3911
- const frozenHostContext = freezeShallowHostContext(opts.hostContext !== undefined ? opts.hostContext : undefined);
4156
+ const frozenHostContext = freezeShallowHostContext(opts.hostContext !== undefined ? opts.hostContext : undefined, opts.hostCapabilities && typeof opts.hostCapabilities === 'object' ? opts.hostCapabilities : undefined);
3912
4157
  const hostKit = {
3913
4158
  hostContext: frozenHostContext,
3914
4159
  bridgeAllowedPathPrefixes: opts.bridgeAllowedPathPrefixes,
@@ -4108,13 +4353,13 @@ function manifestFetchCacheMiddleware$1(options = {}) {
4108
4353
  return structuredClone(r);
4109
4354
  }
4110
4355
  }
4111
- catch {
4356
+ catch (_a) {
4112
4357
  /* ignore */
4113
4358
  }
4114
4359
  try {
4115
4360
  return JSON.parse(JSON.stringify(r));
4116
4361
  }
4117
- catch {
4362
+ catch (_b) {
4118
4363
  return { ...r, data: r.data };
4119
4364
  }
4120
4365
  }
@@ -4152,7 +4397,7 @@ function manifestFetchCacheMiddleware$1(options = {}) {
4152
4397
  }
4153
4398
  return res;
4154
4399
  }
4155
- catch {
4400
+ catch (_a) {
4156
4401
  return null;
4157
4402
  }
4158
4403
  }
@@ -4160,7 +4405,7 @@ function manifestFetchCacheMiddleware$1(options = {}) {
4160
4405
  try {
4161
4406
  store.setItem(key, JSON.stringify({ expiresAt, result }));
4162
4407
  }
4163
- catch {
4408
+ catch (_a) {
4164
4409
  /* Quota / 不可序列化 */
4165
4410
  }
4166
4411
  }
@@ -4253,7 +4498,7 @@ function normalizeBridgePath(input) {
4253
4498
  try {
4254
4499
  return decodeURIComponent(normalized);
4255
4500
  }
4256
- catch {
4501
+ catch (_a) {
4257
4502
  return normalized;
4258
4503
  }
4259
4504
  })();
@@ -4580,6 +4825,10 @@ function createHostApi(pluginId, router, hostKitOptions = {}) {
4580
4825
  throw new Error('registerSanitizedHtmlSnippet is not enabled');
4581
4826
  },
4582
4827
  getContributedRoutes: () => getContributedRoutesForPlugin(pluginId),
4828
+ getHostModule,
4829
+ getHostModuleMeta,
4830
+ getHostComponent,
4831
+ getHostComponentMeta,
4583
4832
  getBridge: () => bridge,
4584
4833
  onTeardown(_pluginId, fn) {
4585
4834
  if (typeof fn === 'function') {
@@ -4750,11 +4999,20 @@ exports.defaultWebExtendPluginRuntime = defaultWebExtendPluginRuntime;
4750
4999
  exports.disposeWebPlugin = disposeWebPlugin;
4751
5000
  exports.ensurePluginHostRoute = ensurePluginHostRoute;
4752
5001
  exports.getActivatedPluginIds = getActivatedPluginIds;
5002
+ exports.getAllHostComponentMeta = getAllHostComponentMeta;
5003
+ exports.getAllHostModuleMeta = getAllHostModuleMeta;
4753
5004
  exports.getContributedRoutesForPlugin = getContributedRoutesForPlugin;
5005
+ exports.getHostComponent = getHostComponent;
5006
+ exports.getHostComponentMeta = getHostComponentMeta;
5007
+ exports.getHostModule = getHostModule;
5008
+ exports.getHostModuleMeta = getHostModuleMeta;
4754
5009
  exports.getRegisteredTopRouteNamesForPlugin = getRegisteredTopRouteNamesForPlugin;
4755
5010
  exports.installWebExtendPluginVue2 = installWebExtendPluginVue2;
4756
5011
  exports.manifestFetchCacheMiddleware = manifestFetchCacheMiddleware;
4757
5012
  exports.peerMinimumVersions = peerMinimumVersions;
5013
+ exports.registerHostComponents = registerHostComponents;
5014
+ exports.registerHostModules = registerHostModules;
5015
+ exports.registerVueGlobalComponents = registerVueGlobalComponents;
4758
5016
  exports.registries = registries;
4759
5017
  exports.resolveRuntimeOptions = resolveRuntimeOptions;
4760
5018
  exports.routeSynthNamePrefix = routeSynthNamePrefix;