Skip to content

Commit 1a2e301

Browse files
author
hubert
committed
feat: add global variables option
1 parent a897212 commit 1a2e301

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

packages/module-loader/src/createLoader.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,34 @@ import type { ModuleLoader, GetResolver } from './types';
99

1010
/**
1111
* create module loader
12-
* @param options options
13-
* @param options.resolver remote module resolver, default to umd resolver
14-
* @param options.container container to append script, default is append to body in client side
1512
*/
1613
export function createLoader<Props extends Record<string, any> = any, Context = any>(
1714
options: {
15+
/**
16+
* remote module resolver, default to umd resolver
17+
*/
1818
resolver?: GetResolver<Context>;
19+
/**
20+
* container to append script, default is append to body in client side
21+
*/
1922
container?: string | ((proxy: Context) => Element);
23+
/**
24+
* global variables to expose to remote module
25+
*/
26+
globals?: Record<string, any>;
2027
} = {},
2128
) {
22-
// Set global VueDemi
23-
if (window && !window.VueDemi) {
24-
window.VueDemi = VueDemi;
29+
const _resolver = options.resolver?.(options.container) ?? (getUmdResolver(options.container as any) as any);
30+
31+
// Set global variables
32+
const proxy = _resolver.context;
33+
if (!proxy.VueDemi) {
34+
proxy.VueDemi = VueDemi;
35+
}
36+
if (options.globals) {
37+
Object.assign(proxy, options.globals);
2538
}
2639

27-
const _resolver = options.resolver?.(options.container) ?? (getUmdResolver(options.container as any) as any);
2840
const loader: ModuleLoader<Props, Context> = VueDemi.markRaw({
2941
install(app) {
3042
// this allows calling registerSubModules() outside of a component setup after

packages/module-loader/src/sub/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ export type Bootstrap = (App: App | typeof Vue2) => void;
1818
*/
1919
export type Mount = <Props extends ModuleLoaderCustomProperties = ModuleLoaderCustomProperties>(
2020
App: App | typeof Vue2,
21-
props?: Props,
21+
props: Props,
2222
) => void | RegisterProperties | Promise<void | RegisterProperties>;
2323

2424
/**
2525
* Unmount根据 activeRule 在每一次被销毁时被调用
2626
*/
2727
export type Unmount = <Props extends ModuleLoaderCustomProperties = ModuleLoaderCustomProperties>(
2828
App: App | typeof Vue2,
29-
props?: Props,
29+
props: Props,
3030
) => void;
3131

3232
export interface ModuleLoaderCustomProperties {

0 commit comments

Comments
 (0)