diff --git a/game/game.js b/game/game.js index 48aae274a..5dc94eeb4 100644 --- a/game/game.js +++ b/game/game.js @@ -123,7 +123,9 @@ new Promise(resolve => { const result = ts.transpile(code, { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2019, - inlineSourceMap: true + inlineSourceMap: true, + resolveJsonModule: true, + esModuleInterop: true, }, fileName); // 使用默认的js编译函数获取返回值 return _compile.call(this, result, fileName); diff --git a/service-worker.js b/service-worker.js index d0898e185..54cb6ce84 100644 --- a/service-worker.js +++ b/service-worker.js @@ -10,7 +10,13 @@ if (typeof ts != 'undefined') { console.log(`ts undefined`); } -console.log('version 1'); +console.log('version 2.1'); + +self.addEventListener("install", (event) => { + // The promise that skipWaiting() returns can be safely ignored. + // @ts-ignore + self.skipWaiting(); +}); self.addEventListener('message', event => { console.log(event.data); @@ -20,7 +26,12 @@ 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; + if (!['.ts', '.json'].some(ext => request.url.endsWith(ext))) return; + if (request.url.endsWith('.d.ts')) return; + if (request.url.endsWith('.json')) { + // @ts-ignore + if (!event.request.headers.get('origin')) return; + } // 请求ts文件 const res = fetch(request.url, { method: request.method, @@ -35,11 +46,18 @@ self.addEventListener('fetch', event => { if (res.status != 200) return res; console.log('正在编译', request.url); return res.text().then(text => { - const js = ts.transpile(text, { - module: ts.ModuleKind.ES2015, - target: ts.ScriptTarget.ES2019, - inlineSourceMap: true - }, request.url); + let js; + if (request.url.endsWith('.json')) { + js = `export default ${text}`; + } else { + js = ts.transpile(text, { + module: ts.ModuleKind.ES2015, + target: ts.ScriptTarget.ES2019, + inlineSourceMap: true, + resolveJsonModule: true, + esModuleInterop: true, + }, request.url); + } const rep = new Response(new Blob([js], { type: "text/javascript" }), { status: 200, statusText: "OK", diff --git a/tsconfig.json b/tsconfig.json index ddfc45445..b988e2e3d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -39,7 +39,7 @@ ], /* Specify multiple folders that act like `./node_modules/@types`. */ // "types": [], /* Specify type package names to be included without being referenced in a source file. */ // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ - // "resolveJsonModule": true, /* Enable importing .json files */ + "resolveJsonModule": true, /* Enable importing .json files */ // "noResolve": true, /* Disallow `import`s, `require`s or ``s from expanding the number of files TypeScript should add to a project. */ /* JavaScript Support */ // "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */