Skip to content

Commit f975dd5

Browse files
committed
fix: merge load app options
1 parent 8c25e3f commit f975dd5

File tree

3 files changed

+66
-79
lines changed

3 files changed

+66
-79
lines changed

packages/runtime/core/src/config.ts

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
import { deepMerge, error, hasOwn, warn } from '@garfish/utils';
1+
import {
2+
warn,
3+
error,
4+
assert,
5+
hasOwn,
6+
deepMerge,
7+
getRenderNode,
8+
} from '@garfish/utils';
9+
import { AppInfo } from './module/app';
210
import { interfaces } from './interface';
311

412
// Because of the addition of nested scenes, the configuration merging is too complicated
@@ -19,15 +27,6 @@ export const lifecycle: Array<Exclude<
1927
'errorUnmountApp',
2028
];
2129

22-
const globalConfigAttrs = [
23-
'apps',
24-
'plugins',
25-
'autoRefreshApp',
26-
'onNotMatchRouter',
27-
'disableStatistics',
28-
'disablePreloadApp',
29-
];
30-
3130
const invalidNestedAttrs = [
3231
'sandbox',
3332
'autoRefreshApp',
@@ -40,23 +39,13 @@ export const filterNestedConfig = (config: interfaces.Options) => {
4039
invalidNestedAttrs.forEach((key) => {
4140
if (key in config) {
4241
delete config[key];
43-
__DEV__ &&
44-
warn(`Nested scene does not support the configuration ${key}`);
42+
warn(`Nested scene does not support the configuration ${key}`);
4543
}
4644
});
4745
}
4846
return config;
4947
};
5048

51-
export const filterGlobalConfig = (config: interfaces.AppInfo) => {
52-
for (const key in config) {
53-
if (globalConfigAttrs.includes(key)) {
54-
delete config[key];
55-
}
56-
}
57-
return config;
58-
};
59-
6049
// Merge `oldConfig` and `newConfig`
6150
export const deepMergeConfig = (o, n) => {
6251
let tempO = o;
@@ -96,7 +85,7 @@ export const createDefaultOptions = (nested = false) => {
9685
strictIsolation: false,
9786
},
9887
// Load hooks
99-
beforeLoad: async () => {},
88+
beforeLoad: () => {},
10089
afterLoad: () => {},
10190
// Code eval hooks
10291
beforeEval: () => {},
@@ -121,3 +110,38 @@ export const createDefaultOptions = (nested = false) => {
121110
}
122111
return config;
123112
};
113+
114+
export const generateAppOptions = async (
115+
appName: string,
116+
garfish: interfaces.Garfish,
117+
appOpts: Partial<interfaces.LoadAppOptions> | string = {},
118+
) => {
119+
let appInfo = garfish.appInfos[appName];
120+
121+
// `Garfish.loadApp('appName', 'https://xx.html');`
122+
if (typeof appOpts === 'string') {
123+
appOpts = {
124+
name: appName,
125+
entry: appOpts,
126+
basename: '/',
127+
} as interfaces.LoadAppOptions;
128+
}
129+
130+
appInfo = appInfo
131+
? deepMergeConfig(appInfo, appOpts)
132+
: deepMergeConfig(garfish.options, appOpts);
133+
134+
// Does not support does not have remote resources application
135+
assert(
136+
appInfo.entry,
137+
`Can't load unexpected child app "${appName}", ` +
138+
'Please provide the entry parameters or registered in advance of the app.',
139+
);
140+
141+
appInfo.name = appName;
142+
// Initialize the mount point, support domGetter as promise, is advantageous for the compatibility
143+
if (appInfo.domGetter) {
144+
appInfo.domGetter = await getRenderNode(appInfo.domGetter);
145+
}
146+
return appInfo as AppInfo;
147+
};

packages/runtime/core/src/garfish.ts

Lines changed: 17 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { Loader, TemplateManager, JavaScriptManager } from '@garfish/loader';
1212
import {
1313
deepMergeConfig,
1414
filterNestedConfig,
15-
filterGlobalConfig,
15+
generateAppOptions,
1616
createDefaultOptions,
1717
} from './config';
1818
import { interfaces } from './interface';
@@ -52,51 +52,6 @@ export class Garfish implements interfaces.Garfish {
5252
options?.plugins?.forEach((plugin) => this.usePlugin(plugin));
5353
}
5454

55-
private registerDefaultPlugin(options?: interfaces.Options) {
56-
this.usePlugin(GarfishHMRPlugin());
57-
this.usePlugin(GarfishOptionsLife(options));
58-
this.usePlugin(GarfishPerformance());
59-
if (!options.disablePreloadApp) {
60-
this.usePlugin(GarfishPreloadPlugin());
61-
}
62-
}
63-
64-
private async mergeAppOptions(
65-
appName: string,
66-
options?: Partial<interfaces.LoadAppOptions> | string,
67-
) {
68-
options = options || {};
69-
// `Garfish.loadApp('appName', 'https://xx.html');`
70-
if (typeof options === 'string') {
71-
options = {
72-
name: appName,
73-
entry: options,
74-
basename: '/',
75-
} as interfaces.AppInfo;
76-
}
77-
78-
let appInfo = this.appInfos[appName];
79-
if (appInfo) {
80-
appInfo = deepMergeConfig(appInfo, options);
81-
} else {
82-
appInfo = deepMergeConfig(this.options, options);
83-
filterGlobalConfig(appInfo);
84-
}
85-
86-
// Does not support does not have remote resources application
87-
assert(
88-
appInfo.entry,
89-
`Can't load unexpected child app "${appName}", ` +
90-
'Please provide the entry parameters or registered in advance of the app.',
91-
);
92-
appInfo.name = appName;
93-
// Initialize the mount point, support domGetter as promise, is advantageous for the compatibility
94-
if (appInfo.domGetter) {
95-
appInfo.domGetter = await getRenderNode(appInfo.domGetter);
96-
}
97-
return appInfo as AppInfo;
98-
}
99-
10055
private setOptions(options: Partial<interfaces.Options>) {
10156
assert(!this.running, 'Garfish is running, can`t set options');
10257
if (isPlainObject(options)) {
@@ -134,23 +89,24 @@ export class Garfish implements interfaces.Garfish {
13489
if (this.running) {
13590
// Nested scene can be repeated registration application
13691
if (options.nested) {
137-
const hooks = globalLifecycle();
92+
const nestedHooks = globalLifecycle();
13893
const mainOptions = createDefaultOptions(true);
139-
options = filterNestedConfig(deepMergeConfig(mainOptions, options));
94+
options = deepMergeConfig(mainOptions, options);
95+
options = filterNestedConfig(options);
14096

14197
// Register plugins
14298
options.plugins?.forEach((plugin) => this.usePlugin(plugin));
99+
143100
// Nested applications have independent life cycles
144-
this.usePlugin(GarfishOptionsLife(options));
101+
nestedHooks.usePlugin(GarfishOptionsLife(options));
145102

146103
if (options.apps) {
147-
// usage:
104+
// Usage:
148105
// `Garfish.run({ apps: [{ props: Garfish.props }] })`
149106
this.registerApp(
150107
options.apps.map((app) => {
151108
const appConf = deepMergeConfig(options, app);
152-
filterGlobalConfig(appConf);
153-
appConf.hooks = hooks;
109+
appConf.hooks = nestedHooks;
154110
appConf.sandbox = this.options.sandbox;
155111
return appConf;
156112
}),
@@ -165,10 +121,15 @@ export class Garfish implements interfaces.Garfish {
165121
this.setOptions(options);
166122

167123
// Register plugins
168-
this.registerDefaultPlugin(this.options);
124+
this.usePlugin(GarfishHMRPlugin());
125+
this.usePlugin(GarfishOptionsLife(this.options));
126+
this.usePlugin(GarfishPerformance());
127+
if (!options.disablePreloadApp) {
128+
this.usePlugin(GarfishPreloadPlugin());
129+
}
169130
options?.plugins?.forEach((plugin) => this.usePlugin(plugin, this));
170131

171-
// Emit hooks
132+
// Emit hooks and register apps
172133
this.hooks.lifecycle.beforeBootstrap.emit(this.options);
173134
this.registerApp(options.apps || []);
174135
this.running = true;
@@ -190,7 +151,6 @@ export class Garfish implements interfaces.Garfish {
190151
// Deep merge this.options
191152
if (!info.nested) {
192153
info = deepMergeConfig(this.options, info);
193-
filterGlobalConfig(info);
194154
}
195155
currentAdds[info.name] = info;
196156
this.appInfos[info.name] = info;
@@ -220,10 +180,10 @@ export class Garfish implements interfaces.Garfish {
220180

221181
async loadApp(
222182
appName: string,
223-
options?: Partial<interfaces.LoadAppOptions> | string,
183+
options?: interfaces.LoadAppOptions | string,
224184
): Promise<interfaces.App | null> {
225185
assert(appName, 'Miss appName.');
226-
const appInfo = await this.mergeAppOptions(appName, options);
186+
const appInfo = await generateAppOptions(appName, this, options);
227187

228188
const asyncLoadProcess = async () => {
229189
// Return not undefined type data directly to end loading

packages/runtime/core/src/module/app.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ export class App {
105105
this.cjsModules.module = this.cjsModules;
106106
this.customLoader = customLoader;
107107

108+
// Register hooks
109+
this.hooks.usePlugin(appInfo);
110+
108111
// Save all the resources to address
109112
const nodes = entryManager.getNodesByTagName(...sourceListTags);
110113
for (const key in nodes) {

0 commit comments

Comments
 (0)