in packages/core/loader/src/requireEnsure.ts [112:167]
export async function requireEnsure<T>(bundle: IBundleOption) {
const transform = bundle.transform || ((source: string) => source);
if (isModuleResolved(bundle)) {
// if loader contains the context(window, location)
// then get the new export using new context
if (bundle.context) {
return globalModule.requireIsolateWithContext(bundle.id, bundle.context);
}
// return the cached module
return globalModule.require(bundle.id);
}
const promises: Array<Promise<T>> = [];
let chunkRecord: Record<T> = Module.record.get(bundle.id);
let pending = false;
if (!chunkRecord || !chunkRecord.loaded) {
if (chunkRecord) {
promises.push(chunkRecord.promise);
pending = true;
} else {
const promise = new Promise<T>((resolve, reject) => {
chunkRecord = new Record();
chunkRecord.resolve = resolve;
chunkRecord.reject = reject;
chunkRecord.context = bundle.context;
chunkRecord.deps = bundle.deps;
chunkRecord.uuid = uuidv4();
Module.record.set(bundle.id, chunkRecord);
});
chunkRecord.promise = promise;
promises.push(promise);
if (bundle.xmlrequest) {
xmlRequire(bundle.id, bundle.url, transform);
} else {
jsonpRequire(bundle.id, bundle.url, chunkRecord.uuid);
}
}
}
await Promise.all(promises);
// pending 的 chunk 需要重新执行
if (isModuleResolved(bundle) && pending) {
// if loader contains the context(window, location)
// then get the new export using new context
if (bundle.context) {
return globalModule.requireIsolateWithContext(bundle.id, bundle.context);
}
}
return globalModule.require(bundle.id);
}