Skip to content

Commit dbe4c7d

Browse files
author
hubert
committed
perf: context global variables set method change
1 parent 1a2e301 commit dbe4c7d

File tree

5 files changed

+71
-68
lines changed

5 files changed

+71
-68
lines changed

packages/module-loader/src/createLoader.ts

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,30 @@ import * as VueDemi from 'vue-demi';
22
import { createModuleLoader } from './core/moduleLoader';
33
import { createComponentLoader } from './core/componentLoader';
44
import { ModuleLoaderSymbol, setActiveLoader, setOptions, addErrorHandler, removeErrorHandler } from './register';
5-
import { getUmdResolver } from './resolvers/umd';
5+
import { createUmdResolver } from './resolvers/umd';
66

77
// Types
8-
import type { ModuleLoader, GetResolver } from './types';
8+
import type { ModuleLoader, Resolver, ResolverCreatorOptions } from './types';
9+
10+
type Without<T, U> = { [P in Exclude<keyof T, keyof U>]?: never };
11+
type XOR<T, U> = T | U extends object ? (Without<T, U> & U) | (Without<U, T> & T) : T | U;
912

1013
/**
1114
* create module loader
1215
*/
1316
export function createLoader<Props extends Record<string, any> = any, Context = any>(
14-
options: {
15-
/**
16-
* remote module resolver, default to umd resolver
17-
*/
18-
resolver?: GetResolver<Context>;
19-
/**
20-
* container to append script, default is append to body in client side
21-
*/
22-
container?: string | ((proxy: Context) => Element);
23-
/**
24-
* global variables to expose to remote module
25-
*/
26-
globals?: Record<string, any>;
27-
} = {},
17+
options: XOR<
18+
{
19+
/**
20+
* remote module resolver, default to umd resolver
21+
*/
22+
resolver?: Resolver<Context>;
23+
},
24+
ResolverCreatorOptions<WindowProxy>
25+
> = {},
2826
) {
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);
38-
}
27+
const { resolver, ...resolverOptions } = options;
28+
const _resolver = resolver ?? (createUmdResolver(resolverOptions) as Resolver<any>);
3929

4030
const loader: ModuleLoader<Props, Context> = VueDemi.markRaw({
4131
install(app) {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { GetResolver } from './types';
1+
import type { Resolver } from './types';
22

3-
export function defineResolver<Context>(resolver: GetResolver<Context>): GetResolver<Context> {
3+
export function defineResolver<Context>(resolver: Resolver<Context>): Resolver<Context> {
44
return resolver;
55
}

packages/module-loader/src/resolvers/cjs.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import warning from 'warning';
22
import { debug } from '../env';
3-
import { defineResolver } from '../resolver';
43

54
// Types
65
import { Context as vmContext } from 'vm';
76
import { ClientRequest, IncomingMessage } from 'http';
7+
import { ResolverCreatorOptions, Resolver } from '../types';
88

9-
function createSandbox() {
9+
function createSandbox(globalVariables: Record<string, any> = {}) {
1010
const sandbox: Record<string, any> = {
11+
...globalVariables,
1112
Buffer: Buffer,
1213
require: require,
1314
console: console,
@@ -43,8 +44,8 @@ function tryGetCwd(path: any) {
4344
return cwd;
4445
}
4546

46-
export const getCjsResolver = defineResolver<vmContext>((container) => {
47-
const proxy = createSandbox();
47+
export function createCjsResolver({ globalVariables }: ResolverCreatorOptions): Resolver<vmContext> {
48+
const proxy = createSandbox(globalVariables);
4849
return {
4950
context: proxy,
5051
execScript(entry) {
@@ -120,4 +121,4 @@ export const getCjsResolver = defineResolver<vmContext>((container) => {
120121
return Promise.resolve();
121122
},
122123
};
123-
});
124+
}

packages/module-loader/src/resolvers/umd.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
*/
55
import warning from 'warning';
66
import { debug } from '../env';
7-
import { defineResolver } from '../resolver';
7+
8+
// Types
9+
import { ResolverCreatorOptions, Resolver } from '../types';
810

911
const isIE11 = typeof navigator !== 'undefined' && navigator.userAgent.indexOf('Trident') !== -1;
1012

@@ -70,14 +72,18 @@ function noteGlobalProps(global: WindowProxy) {
7072
const scriptsCache = new Map<string, any>();
7173
const stylesCache = new Map<string, HTMLLinkElement>();
7274

73-
function createSandbox() {
75+
function createSandbox(globalVariables: Record<string, any> = {}) {
7476
// TODO: load in sandbox
7577
const global: WindowProxy = window;
78+
Object.assign(global, globalVariables);
7679
return global;
7780
}
7881

79-
export const getUmdResolver = defineResolver<WindowProxy>((container = (proxy) => proxy.document.body) => {
80-
const proxy = createSandbox();
82+
export function createUmdResolver({
83+
container = (proxy) => proxy.document.body,
84+
globalVariables,
85+
}: ResolverCreatorOptions): Resolver<WindowProxy> {
86+
const proxy = createSandbox(globalVariables);
8187
return {
8288
context: proxy,
8389
execScript(entry) {
@@ -147,4 +153,4 @@ export const getUmdResolver = defineResolver<WindowProxy>((container = (proxy) =
147153
}
148154
},
149155
};
150-
});
156+
}

packages/module-loader/src/types.ts

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ export interface ModuleLoader<Props extends Record<string, any> = any, Context =
1616
/**
1717
* Resolver
1818
*/
19-
resolver: ReturnType<GetResolver<Context>> & {
20-
context: Context;
21-
};
19+
resolver: Resolver<Context>;
2220
/**
2321
* App linked to this ModuleLoader instance
2422
* @internal
@@ -210,32 +208,40 @@ export type Lifecycles = {
210208
export type ErrorHandler = (error: Error, module: InnerRegistrableModule) => void;
211209

212210
/**
213-
* Loader resolver
211+
* Resolver
214212
*/
215-
export interface GetResolver<Context = any> {
216-
/**
217-
* Get resolver
218-
* @param container container to append script, default is append to body in client side
219-
*/
220-
(container?: string | ((proxy: Context) => Element)): {
221-
/**
222-
* execution context
223-
*/
224-
context: Context;
225-
/**
226-
* execute script
227-
* @param entry remote script src
228-
*/
229-
execScript<R = unknown>(entry: string): R | Promise<R>;
230-
/**
231-
* add styles
232-
* @param styles style href
233-
*/
234-
addStyles(styles: string[]): void | Promise<void>;
235-
/**
236-
* remove styles
237-
* @param styles style href
238-
*/
239-
removeStyles(styles: string[]): void | Promise<void>;
240-
};
213+
export interface Resolver<Context = any> {
214+
/**
215+
* execution context
216+
*/
217+
context: Context;
218+
/**
219+
* execute script
220+
* @param entry remote script src
221+
*/
222+
execScript<R = unknown>(entry: string): R | Promise<R>;
223+
/**
224+
* add styles
225+
* @param styles style href
226+
*/
227+
addStyles(styles: string[]): void | Promise<void>;
228+
/**
229+
* remove styles
230+
* @param styles style href
231+
*/
232+
removeStyles(styles: string[]): void | Promise<void>;
233+
}
234+
235+
/**
236+
* create resolver options
237+
*/
238+
export interface ResolverCreatorOptions<Context = any> {
239+
/**
240+
* container container to append script, default is append to body in client side
241+
*/
242+
container?: string | ((proxy: Context) => Element);
243+
/**
244+
* global variables to extend to resolver context
245+
*/
246+
globalVariables?: Record<string, any>;
241247
}

0 commit comments

Comments
 (0)