MCPcopy Index your code
hub / github.com/Paciolan/remote-module-loader / createLoadRemoteModule

Function createLoadRemoteModule

src/lib/loadRemoteModule.ts:33–86  ·  view source on GitHub ↗
({
  requires,
  fetcher,
  type
} = {})

Source from the content-addressed store, hash-verified

31}
32
33export const createLoadRemoteModule: CreateLoadRemoteModule = ({
34 requires,
35 fetcher,
36 type
37} = {}) => {
38 const _requires = requires || defaultRequires;
39 const _fetcher = fetcher || defaultFetcher;
40
41 return memoize((url: string) =>
42 _fetcher(url).then(data => {
43 const exports = {};
44 const module = { exports };
45
46 // "cjs" and "umd" keep `define` out of scope entirely. A UMD wrapper
47 // then resolves through its CommonJS branch instead of registering via
48 // `define`, which also fixes CJS bundles that embed a UMD dependency
49 // (an injected `define` would otherwise hijack the dependency's exports;
50 // GitHub issue #39). "umd" is handled identically to "cjs"; it exists as
51 // a separate option only to read clearly at the call site.
52 if (type === "cjs" || type === "umd") {
53 const func = new Function("require", "module", "exports", data);
54 func(_requires, module, exports);
55 return module.exports;
56 }
57
58 const define: any = (...args: any[]) => {
59 let factory: Function;
60 let deps: string[];
61
62 if (typeof args[args.length - 1] === "function") {
63 factory = args.pop();
64 } else {
65 module.exports = args[args.length - 1];
66 return;
67 }
68
69 deps = Array.isArray(args[args.length - 1]) ? args.pop() : ["require", "exports", "module"];
70
71 const builtins: Record<string, any> = { exports, require: _requires, module };
72 const resolved = deps.map(dep => builtins[dep] || _requires(dep));
73
74 const result = factory(...resolved);
75 if (result !== undefined) {
76 module.exports = result;
77 }
78 };
79 define.amd = {};
80
81 const func = new Function("require", "module", "exports", "define", data);
82 func(_requires, module, exports, define);
83 return module.exports;
84 })
85 );
86};

Callers 1

Calls 1

memoizeFunction · 0.85

Tested by

no test coverage detected