From aefc70ccb30230325eead8c369958c0c5d5f3746 Mon Sep 17 00:00:00 2001 From: nonameShijian <2954700422@qq.com> Date: Sat, 3 Feb 2024 19:39:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E8=A6=81=E5=AF=BC=E5=85=A5=E7=9A=84ts?= =?UTF-8?q?=E7=BC=96=E8=AF=91=E5=88=B0ES2019=EF=BC=8C=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E4=BD=BF=E7=94=A8require=E5=AF=BC=E5=85=A5=E4=B8=80=E4=B8=AAts?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game/game.js | 55 ++++++++++++++++++++++++++++++++++++----------- service-worker.js | 1 + 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/game/game.js b/game/game.js index bb2b6e40e..48aae274a 100644 --- a/game/game.js +++ b/game/game.js @@ -86,21 +86,50 @@ new Promise(resolve => { exit() } else { - // 在http环境下修改__dirname - if (location.protocol.startsWith('http') && - typeof window.require == 'function' && + // node环境下 + if (typeof window.require == 'function' && typeof window.process == 'object' && - typeof window.__dirname == 'string' && - window.__dirname.endsWith('electron.asar\\renderer')) { - const path = require('path'); - window.__dirname = path.join(path.resolve(), 'resources/app'); - // @ts-ignore - window.require = function (moduleId) { - try { - return module.require(moduleId); - } catch { - return module.require(path.join(window.__dirname, moduleId)); + typeof window.__dirname == 'string') { + // 在http环境下修改__dirname和require的逻辑 + if (location.protocol.startsWith('http') && + window.__dirname.endsWith('electron.asar\\renderer')) { + const path = require('path'); + window.__dirname = path.join(path.resolve(), 'resources/app'); + const oldData = Object.entries(window.require); + // @ts-ignore + window.require = function (moduleId) { + try { + return module.require(moduleId); + } catch { + return module.require(path.join(window.__dirname, moduleId)); + } + }; + oldData.forEach(([key, value]) => { + window.require[key] = value; + }); + } + // 增加导入ts的逻辑 + window.require.extensions['.ts'] = function (module, filename) { + // @ts-ignore + const _compile = module._compile; + // @ts-ignore + module._compile = function (code, fileName) { + /** + * @type { import('typescript') } + */ + // @ts-ignore + const ts = require('./game/typescript.js'); + // 使用ts compiler对ts文件进行编译 + const result = ts.transpile(code, { + module: ts.ModuleKind.CommonJS, + target: ts.ScriptTarget.ES2019, + inlineSourceMap: true + }, fileName); + // 使用默认的js编译函数获取返回值 + return _compile.call(this, result, fileName); } + // @ts-ignore + module._compile(require('fs').readFileSync(filename, 'utf8'), filename); }; } if (location.protocol.startsWith('http') && 'serviceWorker' in navigator) { diff --git a/service-worker.js b/service-worker.js index 321d69548..d0898e185 100644 --- a/service-worker.js +++ b/service-worker.js @@ -37,6 +37,7 @@ self.addEventListener('fetch', event => { return res.text().then(text => { const js = ts.transpile(text, { module: ts.ModuleKind.ES2015, + target: ts.ScriptTarget.ES2019, inlineSourceMap: true }, request.url); const rep = new Response(new Blob([js], { type: "text/javascript" }), {