MCPcopy
hub / github.com/web-infra-dev/garfish / loadModuleSync

Function loadModuleSync

packages/remote-module/src/apis/loadModuleSync.ts:31–97  ·  view source on GitHub ↗
(
  urlOrAlias: string,
  options?: ModuleConfig,
)

Source from the content-addressed store, hash-verified

29};
30
31export function loadModuleSync(
32 urlOrAlias: string,
33 options?: ModuleConfig,
34): Record<string, any> | null {
35 const data = hooks.lifecycle.beforeLoadModule.emit({
36 options,
37 url: urlOrAlias,
38 });
39 urlOrAlias = data.url;
40 options = data.options;
41
42 assert(urlOrAlias, 'Missing url for loading remote module.');
43 assert(
44 typeof urlOrAlias === 'string',
45 'The type of URL needs to be a string.',
46 );
47 const [url, segments] = processAlias(urlOrAlias);
48 assert(
49 isAbsolute(url) || url.startsWith('//'),
50 `The loading of the remote module must be an absolute path. "${url}"`,
51 );
52
53 let result: Record<string, any> | null = null;
54 const info = purifyOptions(url, options);
55 const { cache, version, externals, error, adapter } = info;
56 const urlWithVersion = `${version || 'latest'}@${url}`;
57 const module = cacheModules[urlWithVersion];
58 const alias = segments ? segments[0] : '';
59
60 if (cache && module) {
61 isPromise(module) && throwWarn(alias, url);
62 result = getValueInObject(module, segments);
63 } else {
64 const manager = getModuleManager(url);
65 assert(
66 manager,
67 `Synchronously load module must load resources in advance. "${url}"`,
68 );
69
70 try {
71 const actuator = new Actuator(manager, externals);
72 cacheModules[urlWithVersion] = actuator.env.exports;
73 let exports = actuator.execScript().exports;
74
75 if (typeof adapter === 'function') {
76 exports = adapter(exports);
77 }
78 exports = hooks.lifecycle.afterLoadModule.emit({
79 url,
80 exports,
81 code: manager.moduleCode,
82 }).exports;
83
84 isPromise(exports) && throwWarn(alias, url);
85 cacheModules[urlWithVersion] = exports;
86 result = getValueInObject(exports, segments);
87 } catch (e) {
88 delete cacheModules[urlWithVersion];

Callers

nothing calls this directly

Calls 12

execScriptMethod · 0.95
assertFunction · 0.90
processAliasFunction · 0.90
isAbsoluteFunction · 0.90
purifyOptionsFunction · 0.90
isPromiseFunction · 0.90
getValueInObjectFunction · 0.90
getModuleManagerFunction · 0.90
errorFunction · 0.90
prettifyErrorFunction · 0.90
throwWarnFunction · 0.85
emitMethod · 0.45

Tested by

no test coverage detected