( urlOrAlias: string, options?: ModuleConfig, )
| 29 | }; |
| 30 | |
| 31 | export 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]; |
nothing calls this directly
no test coverage detected