Merge pull request #891 from nonameShijian/PR-Branch

添加解析ts的逻辑, exe使未找到的文件不返回html
This commit is contained in:
Spmario233 2024-02-02 00:27:22 +08:00 committed by GitHub
commit 540fad041e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 95 additions and 8 deletions

1
.gitignore vendored
View File

@ -28,3 +28,4 @@ noname.ico
package-lock.json
package.json
Thumbs.db
game/typescript.js

View File

@ -100,9 +100,24 @@ new Promise(resolve => {
return module.require(moduleId);
} catch {
return module.require(path.join(window.__dirname, moduleId));
}
}
};
}
if (location.protocol.startsWith('http') && 'serviceWorker' in navigator) {
let scope = window.location.protocol + '//' + window.location.host + '/';
navigator.serviceWorker.register(`${scope}service-worker.js`, {
updateViaCache: "all",
scope,
}).then(registration => {
navigator.serviceWorker.addEventListener('message', e => {
console.log(e);
});
registration.update();
// console.log(`set scope: ${scope}, service worker instance:`, registration);
}).catch(e => {
console.log('serviceWorker加载失败: ', e);
});
}
const script = document.createElement('script')
script.type = "module";
script.src = `${assetURL}game/entry.js`;

Binary file not shown.

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();

57
service-worker.js Normal file
View File

@ -0,0 +1,57 @@
/**
* @type { import('typescript') }
*/
var ts;
importScripts('./game/typescript.js');
// @ts-ignore
if (typeof ts != 'undefined') {
console.log(ts);
} else {
console.log(`ts undefined`);
}
console.log('version 1');
self.addEventListener('message', event => {
console.log(event.data);
});
self.addEventListener('fetch', event => {
// @ts-ignore
const request = event.request;
if (typeof request.url != 'string') return console.log(request);
if (!request.url.endsWith('.ts') || request.url.endsWith('.d.ts')) return;
// 请求ts文件
const res = fetch(request.url, {
method: request.method,
mode: "no-cors",
headers: new Headers({
"Content-Type": "text/plain"
}),
});
// @ts-ignore
event.respondWith(
res.then(res => {
if (res.status != 200) {
return res;
} else {
console.log('正在编译', request.url);
return res.text().then(text => {
const js = ts.transpile(text, { module: ts.ModuleKind.ES2015 });
const rep = new Response(new Blob([js], { type: "text/javascript" }), {
status: 200,
statusText: "OK",
headers: new Headers({
"Content-Type": "text/javascript"
}),
});
return rep;
})
}
})
.catch(e => {
console.log(e);
throw e;
})
);
});