增加部分语法检测

master
yutent 2023-07-19 10:48:04 +08:00
parent 549718fac8
commit 51becf6943
2 changed files with 77 additions and 13 deletions

View File

@ -31,3 +31,42 @@ export function checkMedia(type, isAudio = 1) {
} }
return 0 return 0
} }
export function checkAsyncAwait() {
try {
eval('async function foo(){return await 123}')
return 1.25
} catch (e) {
return 0
}
}
export function checkPrivateFeatures() {
try {
eval('class Foo { #bar = 3}')
return 1.25
} catch (e) {
return 0
}
}
export function checkObjectSpread() {
try {
eval(`let a = [11, 22]
let b = [...a]
;[a[1], a[0]] = [a[0], a[1]]`)
return 1
} catch (e) {
return 0
}
}
export function checkOptionalChaining() {
try {
// ?。 和 ?? 同时期出现的, 检测一个即可
window.a?.b?.c()
return 1
} catch (e) {
return 0
}
}

View File

@ -1,7 +1,14 @@
import { reactive } from 'vue' import { reactive } from 'vue'
import * as env from '@/lib/env.js' import * as env from '@/lib/env.js'
import { checkCanvas, checkMedia } from '@/lib/checks.js' import {
checkCanvas,
checkMedia,
checkAsyncAwait,
checkPrivateFeatures,
checkObjectSpread,
checkOptionalChaining
} from '@/lib/checks.js'
const store = reactive({ const store = reactive({
scores: 80, scores: 80,
@ -160,30 +167,48 @@ const store = reactive({
] ]
}, },
{ {
name: 'ES6模块', name: '一些语法支持',
total: 0, total: 0,
factor: 40, factor: 60,
scores: [1, env.SUPPORT_ASSERTIONS ? 2.5 : 0], scores: [
items: ['import/export', 'Import assertions'] !!window.BigInt ^ 0,
}, checkObjectSpread(),
{ checkOptionalChaining(),
name: '性能', 1 /* 能打开这个页面, 就是支持esm的了 */,
total: 0, env.SUPPORT_ASSERTIONS ? 2.5 : 0,
factor: 70, checkAsyncAwait(),
scores: [!!navigator.serviceWorker ^ 0, window.WebAssembly ? 1.5 : 0], checkPrivateFeatures()
items: ['Web Workers', 'WebAssembly'] ],
items: [
'BigInt',
'Object Spread',
'?. and ??',
'import/export',
'Import assertions',
'async / await',
'Private class features'
]
}, },
{ {
name: '其他', name: '其他',
total: 0, total: 0,
factor: 80, factor: 80,
scores: [ scores: [
!!navigator.serviceWorker ^ 0,
window.WebAssembly ? 1.5 : 0,
!!navigator.bluetooth ^ 0, !!navigator.bluetooth ^ 0,
navigator.clipboard ? 0.8 : 0, navigator.clipboard ? 0.8 : 0,
navigator.usb ? 2 : 0, navigator.usb ? 2 : 0,
!!navigator.windowControlsOverlay ^ 0 !!navigator.windowControlsOverlay ^ 0
], ],
items: ['Bluetooth', 'clipboard', 'USB', 'Window Controls Overlay'] items: [
'Web Workers',
'WebAssembly',
'Bluetooth',
'clipboard',
'USB',
'Window Controls Overlay'
]
} }
] ]
}) })