From b0c0f70e577c1f5ec737fab2e209bf708cee498b Mon Sep 17 00:00:00 2001 From: nonameShijian <2954700422@qq.com> Date: Thu, 1 Feb 2024 19:22:37 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=A7=A3=E6=9E=90ts?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + game/game.js | 17 +++++++++++++- service-worker.js | 57 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 service-worker.js 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/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 From cb1b81b772f21e4e71847dd20ddf7284b84c0ad8 Mon Sep 17 00:00:00 2001 From: nonameShijian <2954700422@qq.com> Date: Thu, 1 Feb 2024 19:40:27 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0js=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E5=90=8E=E8=87=AA=E5=8A=A8=E8=AF=B7=E6=B1=82?= =?UTF-8?q?ts=E7=9A=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- noname/init/import.js | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) 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(); From ec622150a0be37943c1400595ef70e2b0e0b9ce3 Mon Sep 17 00:00:00 2001 From: nonameShijian <2954700422@qq.com> Date: Thu, 1 Feb 2024 20:04:57 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BD=BF=E6=9C=AA=E6=89=BE=E5=88=B0?= =?UTF-8?q?=E7=9A=84=E6=96=87=E4=BB=B6=E4=B8=8D=E8=BF=94=E5=9B=9Ehtml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- noname-server.exe | Bin 70686720 -> 70686720 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/noname-server.exe b/noname-server.exe index b2c2b1f2abed449eb95592f023fbd96bbf2e71b7..ee02bee87e4b0a6ab59d4bbd0d28de7b1475c2dd 100644 GIT binary patch delta 3220 zcmXZUX_&|b7l!fcJm+uU_njH*jAiUo3PotuLMfq)UDnJ{NKHC1aSLgWrdZ2le=hFYVNrU3x!7#1)^XSio#JOibk<09wnk=l#0?( zCdx*+C?6G~VpNLCQ6;KIwWuC7qGr^Jlt_)*Q77s~y{I1zA}!LRVKj=y(IhgWX*7%G z(IQ$#t7sjWabBDsZQ_EsFxp1DXdf5F#nB-;#wF1yI!BkdG%kzFTj~BkqjxF(D?#T`?){j(Z|E?v2TDUqsv=Q(|i5#RD-drUx-29*mhW zD`v+-F()35N8-_VE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