添加js加载失败后自动请求ts的逻辑

This commit is contained in:
nonameShijian 2024-02-01 19:40:27 +08:00
parent b0c0f70e57
commit cb1b81b772
1 changed files with 21 additions and 7 deletions

View File

@ -38,15 +38,29 @@ function generateImportFunction(type, pathParser) {
await game.import(type,createEmptyExtension(name));
return;
}
const path = pathParser(name);
let path = pathParser(name);
// 通过浏览器自带的script标签导入可直接获取报错信息且不会影响JS运行
// 此时代码内容也将缓存在浏览器中故再次import后将不会重新执行代码内容测试下来如此
const [status, script] = await new Promise((resolve) => {
const script = document.createElement('script');
script.type = 'module';
script.src = `${lib.assetURL}noname/init/${path}`;
script.onerror = () => resolve(['error', script]);
script.onload = () => resolve(['ok', script]);
const [status, script] = await new Promise(resolve => {
const createScript = () => {
const script = document.createElement('script');
script.type = 'module';
script.src = `${lib.assetURL}noname/init/${path}`;
script.onload = () => resolve(['ok', script]);
return script;
};
let script = createScript();
script.onerror = () => {
if (path.endsWith('.js')) {
path = path.slice(0, -3) + '.ts';
script.remove();
let ts = createScript();
ts.onerror = () => resolve(['error', ts]);
document.head.appendChild(ts);
} else {
resolve(['error', script]);
}
};
document.head.appendChild(script);
});
script.remove();