Skip to content

Commit 1397e09

Browse files
author
hubert
committed
feat: add global props option
1 parent 80e85d7 commit 1397e09

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

packages/module-loader/src/register.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,19 +106,29 @@ export function registerSubModules(config: RegistrableModule | RegistrableModule
106106

107107
formatModules(config).map((config) => {
108108
let activeRule: InnerRegisterSubModule['activeRule'] = undefined;
109-
if (!isFunction(config) && !isUndef(config.activeRule)) {
110-
const activeRules = isArray(config.activeRule) ? config.activeRule : [config.activeRule!];
111-
activeRule = activeRules.map((rule) => {
112-
if (isFunction(rule)) {
113-
return rule;
114-
} else {
115-
return (location: Location) => {
116-
const path = getLocation(location);
117-
const regex = compilePathRegex(rule, {});
118-
return !!path.match(regex);
119-
};
120-
}
121-
});
109+
110+
if (!isFunction(config)) {
111+
// merge global props
112+
if (!isUndef(loaderOptions.props)) {
113+
const globalProps = typeof loaderOptions.props === 'function' ? loaderOptions.props() : loaderOptions.props;
114+
config.props = { ...globalProps, ...config.props };
115+
}
116+
117+
// format activeRule
118+
if (!isUndef(config.activeRule)) {
119+
const activeRules = isArray(config.activeRule) ? config.activeRule : [config.activeRule!];
120+
activeRule = activeRules.map((rule) => {
121+
if (isFunction(rule)) {
122+
return rule;
123+
} else {
124+
return (location: Location) => {
125+
const path = getLocation(location);
126+
const regex = compilePathRegex(rule, {});
127+
return !!path.match(regex);
128+
};
129+
}
130+
});
131+
}
122132
}
123133
subModuleConfigs.push({ config, lifecycles: innerLifecycles, activeRule, activated: false });
124134
});
@@ -141,7 +151,10 @@ export function registerSubModules(config: RegistrableModule | RegistrableModule
141151
loader = activeLoader!;
142152

143153
return {
144-
start: (options?: ModuleLoaderOptions & { router?: Router }) =>
154+
/**
155+
* Start module loader
156+
*/
157+
start: (options?: Pick<ModuleLoaderOptions, 'loading' | 'register'> & { router?: Router }) =>
145158
execute(loader!._moduleLoader, subModuleConfigs, Object.assign({}, loaderOptions, options), loader!._a),
146159
};
147160

packages/module-loader/src/types.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import type { App, Vue2, Component } from 'vue-demi';
22

3-
export interface ModuleLoader<Context = any> {
3+
export interface ModuleLoader<Props extends Record<string, any> = {}, Context = any> {
44
/**
55
* Alias of export setModuleLoaderOptions
66
*/
7-
setOptions: (options: ModuleLoaderOptions) => ModuleLoader;
7+
setOptions: (options: ModuleLoaderOptions<Props>) => ModuleLoader;
88
/**
99
* Alias of export addErrorHandler
1010
*/
@@ -57,12 +57,17 @@ export interface RegisterProperties {
5757
/**
5858
* Main module loader typings
5959
*/
60-
export type ModuleLoaderOptions = {
60+
export type ModuleLoaderOptions<Props extends Record<string, any> = {}> = {
6161
/**
6262
* Use sync mode to load submodules
6363
* @default false
6464
*/
6565
sync?: boolean;
66+
/**
67+
* Global properties to pass to submodules,
68+
* set it before registerSubModules, otherwise it will not apply to submodules
69+
*/
70+
props?: Props | (() => Props);
6671
/**
6772
* Loading shown,
6873
* return a function to hide loading
@@ -78,7 +83,7 @@ export type ModuleLoaderOptions = {
7883
/**
7984
* Remote module
8085
*/
81-
export type ModuleRemoteConfig = {
86+
export type ModuleRemoteConfig<Props extends Record<string, any> = {}> = {
8287
/**
8388
* Module name(Then name of submodule export to global)
8489
*/
@@ -102,7 +107,7 @@ export type ModuleRemoteConfig = {
102107
/**
103108
* Props,pass to submodule "mount" and "unmount" lifecycle
104109
*/
105-
props?: Record<string, any>;
110+
props?: Props;
106111
};
107112

108113
/**

0 commit comments

Comments
 (0)