( urlOrAlias: string, options?: ModuleConfig, )
| 13 | import { processAlias, getValueInObject } from './setModuleConfig'; |
| 14 | |
| 15 | export async function loadModule( |
| 16 | urlOrAlias: string, |
| 17 | options?: ModuleConfig, |
| 18 | ): Promise<Record<string, any> | null> { |
| 19 | const data = await hooks.lifecycle.asyncBeforeLoadModule.emit({ |
| 20 | options, |
| 21 | url: urlOrAlias, |
| 22 | }); |
| 23 | if (data === false) { |
| 24 | return null; |
| 25 | } |
| 26 | |
| 27 | urlOrAlias = data.url; |
| 28 | options = data.options; |
| 29 | |
| 30 | assert(urlOrAlias, 'Missing url for loading remote module.'); |
| 31 | assert( |
| 32 | typeof urlOrAlias === 'string', |
| 33 | 'The type of URL needs to be a string.', |
| 34 | ); |
| 35 | const [url, segments] = processAlias(urlOrAlias); |
| 36 | assert( |
| 37 | isAbsolute(url) || url.startsWith('//'), |
| 38 | `The loading of the remote module must be an absolute path. "${url}"`, |
| 39 | ); |
| 40 | |
| 41 | const info = purifyOptions(url, options); |
| 42 | const { cache, version, externals, error, adapter } = info; |
| 43 | const urlWithVersion = `${version || 'latest'}@${url}`; // `latest@https://xx.js` |
| 44 | |
| 45 | const asyncLoadProcess = async () => { |
| 46 | let result: Record<string, any> | null = null; |
| 47 | let module = cacheModules[urlWithVersion]; |
| 48 | |
| 49 | if (cache && module) { |
| 50 | if (isPromise(module)) { |
| 51 | module = await module; |
| 52 | } |
| 53 | result = getValueInObject(module, segments); |
| 54 | } else { |
| 55 | try { |
| 56 | const data = await loader.loadModule(url); |
| 57 | if (data.resourceManager) { |
| 58 | const actuator = new Actuator(data.resourceManager, externals); |
| 59 | cacheModules[urlWithVersion] = actuator.env.exports; |
| 60 | let exports = actuator.execScript().exports; |
| 61 | |
| 62 | if (typeof adapter === 'function') { |
| 63 | exports = adapter(exports); |
| 64 | } |
| 65 | const hookResult = await hooks.lifecycle.asyncAfterLoadModule.emit({ |
| 66 | url, |
| 67 | exports, |
| 68 | code: data.resourceManager.moduleCode, |
| 69 | }); |
| 70 | if (hookResult === false) { |
| 71 | return null; |
| 72 | } |
nothing calls this directly
no test coverage detected