diff --git a/.gitignore b/.gitignore index 966499c9c..71a62c1a0 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,4 @@ noname.ico package-lock.json package.json Thumbs.db +game/typescript.js diff --git a/game/game.js b/game/game.js index 11484dfec..bb2b6e40e 100644 --- a/game/game.js +++ b/game/game.js @@ -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`; diff --git a/noname-server.exe b/noname-server.exe index b2c2b1f2a..ee02bee87 100644 Binary files a/noname-server.exe and b/noname-server.exe differ diff --git a/noname/init/import.js b/noname/init/import.js index 1668a2812..0375e3b32 100644 --- a/noname/init/import.js +++ b/noname/init/import.js @@ -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(); diff --git a/service-worker.js b/service-worker.js new file mode 100644 index 000000000..68ca420f4 --- /dev/null +++ b/service-worker.js @@ -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; + }) + ); +}); \ No newline at end of file