将要导入的ts编译到ES2019,支持使用require导入一个ts文件
This commit is contained in:
parent
d9022bb482
commit
aefc70ccb3
37
game/game.js
37
game/game.js
|
@ -86,14 +86,16 @@ new Promise(resolve => {
|
||||||
exit()
|
exit()
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// 在http环境下修改__dirname
|
// node环境下
|
||||||
if (location.protocol.startsWith('http') &&
|
if (typeof window.require == 'function' &&
|
||||||
typeof window.require == 'function' &&
|
|
||||||
typeof window.process == 'object' &&
|
typeof window.process == 'object' &&
|
||||||
typeof window.__dirname == 'string' &&
|
typeof window.__dirname == 'string') {
|
||||||
|
// 在http环境下修改__dirname和require的逻辑
|
||||||
|
if (location.protocol.startsWith('http') &&
|
||||||
window.__dirname.endsWith('electron.asar\\renderer')) {
|
window.__dirname.endsWith('electron.asar\\renderer')) {
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
window.__dirname = path.join(path.resolve(), 'resources/app');
|
window.__dirname = path.join(path.resolve(), 'resources/app');
|
||||||
|
const oldData = Object.entries(window.require);
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
window.require = function (moduleId) {
|
window.require = function (moduleId) {
|
||||||
try {
|
try {
|
||||||
|
@ -102,6 +104,33 @@ new Promise(resolve => {
|
||||||
return module.require(path.join(window.__dirname, moduleId));
|
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) {
|
if (location.protocol.startsWith('http') && 'serviceWorker' in navigator) {
|
||||||
let scope = window.location.protocol + '//' + window.location.host + '/';
|
let scope = window.location.protocol + '//' + window.location.host + '/';
|
||||||
|
|
|
@ -37,6 +37,7 @@ self.addEventListener('fetch', event => {
|
||||||
return res.text().then(text => {
|
return res.text().then(text => {
|
||||||
const js = ts.transpile(text, {
|
const js = ts.transpile(text, {
|
||||||
module: ts.ModuleKind.ES2015,
|
module: ts.ModuleKind.ES2015,
|
||||||
|
target: ts.ScriptTarget.ES2019,
|
||||||
inlineSourceMap: true
|
inlineSourceMap: true
|
||||||
}, request.url);
|
}, request.url);
|
||||||
const rep = new Response(new Blob([js], { type: "text/javascript" }), {
|
const rep = new Response(new Blob([js], { type: "text/javascript" }), {
|
||||||
|
|
Loading…
Reference in New Issue