|
| 1 | +/// <reference path="./runtime-base.ts" /> |
| 2 | +/// <reference path="./dummy.ts" /> |
| 3 | + |
| 4 | +declare var augmentContext: ((context: unknown) => unknown); |
| 5 | + |
| 6 | +const moduleCache: ModuleCache<Module> = {}; |
| 7 | + |
| 8 | +/** |
| 9 | + * Gets or instantiates a runtime module. |
| 10 | + */ |
| 11 | +// @ts-ignore |
| 12 | +// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 13 | +function getOrInstantiateRuntimeModule( |
| 14 | + moduleId: ModuleId, |
| 15 | + chunkPath: ChunkPath, |
| 16 | +): Module { |
| 17 | + const module = moduleCache[moduleId]; |
| 18 | + if (module) { |
| 19 | + if (module.error) { |
| 20 | + throw module.error; |
| 21 | + } |
| 22 | + return module; |
| 23 | + } |
| 24 | + |
| 25 | + return instantiateModule(moduleId, { type: SourceType.Runtime, chunkPath }); |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Retrieves a module from the cache, or instantiate it if it is not cached. |
| 30 | + */ |
| 31 | +// Used by the backend |
| 32 | +// @ts-ignore |
| 33 | +// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 34 | +const getOrInstantiateModuleFromParent: GetOrInstantiateModuleFromParent<Module> = ( |
| 35 | + id, |
| 36 | + sourceModule |
| 37 | +) => { |
| 38 | + const module = moduleCache[id]; |
| 39 | + |
| 40 | + if (module) { |
| 41 | + return module; |
| 42 | + } |
| 43 | + |
| 44 | + return instantiateModule(id, { |
| 45 | + type: SourceType.Parent, |
| 46 | + parentId: sourceModule.id, |
| 47 | + }); |
| 48 | +}; |
| 49 | + |
| 50 | +function instantiateModule(id: ModuleId, source: SourceInfo): Module { |
| 51 | + const moduleFactory = moduleFactories[id]; |
| 52 | + if (typeof moduleFactory !== "function") { |
| 53 | + // This can happen if modules incorrectly handle HMR disposes/updates, |
| 54 | + // e.g. when they keep a `setTimeout` around which still executes old code |
| 55 | + // and contains e.g. a `require("something")` call. |
| 56 | + let instantiationReason; |
| 57 | + switch (source.type) { |
| 58 | + case SourceType.Runtime: |
| 59 | + instantiationReason = `as a runtime entry of chunk ${source.chunkPath}`; |
| 60 | + break; |
| 61 | + case SourceType.Parent: |
| 62 | + instantiationReason = `because it was required from module ${source.parentId}`; |
| 63 | + break; |
| 64 | + case SourceType.Update: |
| 65 | + instantiationReason = "because of an HMR update"; |
| 66 | + break; |
| 67 | + default: |
| 68 | + invariant(source, (source) => `Unknown source type: ${source?.type}`); |
| 69 | + } |
| 70 | + throw new Error( |
| 71 | + `Module ${id} was instantiated ${instantiationReason}, but the module factory is not available. It might have been deleted in an HMR update.` |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + switch (source.type) { |
| 76 | + case SourceType.Runtime: |
| 77 | + runtimeModules.add(id); |
| 78 | + break; |
| 79 | + case SourceType.Parent: |
| 80 | + // No need to add this module as a child of the parent module here, this |
| 81 | + // has already been taken care of in `getOrInstantiateModuleFromParent`. |
| 82 | + break; |
| 83 | + case SourceType.Update: |
| 84 | + throw new Error('Unexpected') |
| 85 | + default: |
| 86 | + invariant(source, (source) => `Unknown source type: ${source?.type}`); |
| 87 | + } |
| 88 | + |
| 89 | + const module: Module = { |
| 90 | + exports: {}, |
| 91 | + error: undefined, |
| 92 | + loaded: false, |
| 93 | + id, |
| 94 | + namespaceObject: undefined, |
| 95 | + }; |
| 96 | + |
| 97 | + moduleCache[id] = module; |
| 98 | + |
| 99 | + // NOTE(alexkirsz) This can fail when the module encounters a runtime error. |
| 100 | + try { |
| 101 | + const sourceInfo: SourceInfo = { type: SourceType.Parent, parentId: id }; |
| 102 | + |
| 103 | + const r = commonJsRequire.bind(null, module); |
| 104 | + moduleFactory.call( |
| 105 | + module.exports, |
| 106 | + augmentContext({ |
| 107 | + a: asyncModule.bind(null, module), |
| 108 | + e: module.exports, |
| 109 | + r: commonJsRequire.bind(null, module), |
| 110 | + t: runtimeRequire, |
| 111 | + f: moduleContext, |
| 112 | + i: esmImport.bind(null, module), |
| 113 | + s: esmExport.bind(null, module, module.exports), |
| 114 | + j: dynamicExport.bind(null, module, module.exports), |
| 115 | + v: exportValue.bind(null, module), |
| 116 | + n: exportNamespace.bind(null, module), |
| 117 | + m: module, |
| 118 | + c: moduleCache, |
| 119 | + M: moduleFactories, |
| 120 | + l: loadChunk.bind(null, sourceInfo), |
| 121 | + w: loadWebAssembly.bind(null, sourceInfo), |
| 122 | + u: loadWebAssemblyModule.bind(null, sourceInfo), |
| 123 | + g: globalThis, |
| 124 | + P: resolveAbsolutePath, |
| 125 | + U: relativeURL, |
| 126 | + R: createResolvePathFromModule(r), |
| 127 | + b: getWorkerBlobURL, |
| 128 | + __dirname: typeof module.id === "string" ? module.id.replace(/(^|\/)\/+$/, "") : module.id |
| 129 | + }) |
| 130 | + ); |
| 131 | + } catch (error) { |
| 132 | + module.error = error as any; |
| 133 | + throw error; |
| 134 | + } |
| 135 | + |
| 136 | + module.loaded = true; |
| 137 | + if (module.namespaceObject && module.exports !== module.namespaceObject) { |
| 138 | + // in case of a circular dependency: cjs1 -> esm2 -> cjs1 |
| 139 | + interopEsm(module.exports, module.namespaceObject); |
| 140 | + } |
| 141 | + |
| 142 | + return module; |
| 143 | +} |
| 144 | + |
0 commit comments