TeaWeb/loader/app/maifest.ts

90 lines
2.7 KiB
TypeScript
Raw Permalink Normal View History

import * as loader from "./loader/loader";
import {config, loaderPerformance} from "./loader/loader";
2021-03-18 17:59:06 +00:00
import {loadStyles} from "./loader/StyleLoader";
import {loadScripts} from "./loader/ScriptLoader";
2021-03-18 17:59:06 +00:00
export interface ApplicationManifest {
version: number;
chunks: {
[key: string]: {
files: {
hash: string,
file: string
}[],
css_files: {
hash: string,
file: string
}[],
modules: {
id: string,
context: string,
resource: string
}[]
}
};
}
2021-03-18 17:59:06 +00:00
let manifest: ApplicationManifest;
export async function loadManifest() : Promise<ApplicationManifest> {
if(manifest) {
return manifest;
}
const requestResource = loaderPerformance.logResourceRequest("json", "manifest.json");
try {
requestResource.markExecuting();
const response = await fetch(config.baseUrl + "/manifest.json?_date=" + Date.now());
2021-03-18 17:59:06 +00:00
if(!response.ok) {
requestResource.markExecuted({ status: "unknown-error", message: response.status + " " + response.statusText });
2021-03-18 17:59:06 +00:00
throw response.status + " " + response.statusText;
}
manifest = await response.json();
requestResource.markExecuted({ status: "success" });
} catch(error) {
requestResource.markExecuted({ status: "error-event" });
console.error("Failed to load javascript manifest: %o", error);
loader.critical_error("Failed to load manifest.json", error);
throw "failed to load manifest.json";
}
2021-03-18 17:59:06 +00:00
if(manifest.version !== 2) {
throw "invalid manifest version";
2021-03-18 17:59:06 +00:00
}
return manifest;
}
export async function loadManifestTarget(chunkName: string, taskId: number) {
if(typeof manifest.chunks[chunkName] !== "object") {
loader.critical_error("Missing entry chunk in manifest.json", "Chunk " + chunkName + " is missing.");
throw "missing entry chunk";
}
2021-03-18 17:59:06 +00:00
loader.module_mapping().push({
application: chunkName,
modules: manifest.chunks[chunkName].modules
});
const kMaxRequests = 4;
2021-03-18 17:59:06 +00:00
await loadStyles(manifest.chunks[chunkName].css_files.map(e => e.file), {
maxParallelRequests: kMaxRequests
}, (entry, state) => {
2021-03-18 17:59:06 +00:00
if (state !== "loading") {
return;
}
2021-03-18 17:59:06 +00:00
loader.setCurrentTaskName(taskId, entry);
});
2021-03-18 17:59:06 +00:00
await loadScripts(manifest.chunks[chunkName].files.map(e => e.file), {
maxParallelRequests: kMaxRequests
}, (script, state) => {
if(state !== "loading") {
return;
}
2021-03-18 17:59:06 +00:00
loader.setCurrentTaskName(taskId, script);
});
}