ts支持json导入
This commit is contained in:
parent
7fcab33cf7
commit
72257edb2c
|
@ -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);
|
||||
|
|
|
@ -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,14 @@ 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
|
||||
console.log(event.request.headers.get('origin'));
|
||||
// @ts-ignore
|
||||
if (!event.request.headers.get('origin')) return;
|
||||
}
|
||||
// 请求ts文件
|
||||
const res = fetch(request.url, {
|
||||
method: request.method,
|
||||
|
@ -35,11 +48,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",
|
||||
|
|
Loading…
Reference in New Issue