Merge pull request #891 from nonameShijian/PR-Branch
添加解析ts的逻辑, exe使未找到的文件不返回html
This commit is contained in:
commit
540fad041e
|
@ -28,3 +28,4 @@ noname.ico
|
|||
package-lock.json
|
||||
package.json
|
||||
Thumbs.db
|
||||
game/typescript.js
|
||||
|
|
17
game/game.js
17
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`;
|
||||
|
|
Binary file not shown.
|
@ -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();
|
||||
|
|
|
@ -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;
|
||||
})
|
||||
);
|
||||
});
|
Loading…
Reference in New Issue