79 lines
2.1 KiB
JavaScript
79 lines
2.1 KiB
JavaScript
/**
|
|
* {一些常量}
|
|
* @author yutent<yutent.io@gmail.com>
|
|
* @date 2022/09/06 11:54:56
|
|
*/
|
|
|
|
export const JS_EXP = /(?<=\n|^)<script[^>]*?>([\w\W]*?)<\/script>/
|
|
export const STYLE_EXP = /(?<=\n|^)<style([^>]*?)>([\w\W]*?)<\/style>/g
|
|
export const HTML_EXP = /(?<=\n|^)<template[^>]*?>([\w\W]*?)\n<\/template>/
|
|
export const V_DEEP = /:deep\(([^)]*?)\)(.*)/
|
|
|
|
export const CSS_SHEET_EXP = /([%@\w\.,#\-:>\+\~\|\(\)\[\]"'\=\s]+)\{/g
|
|
export const PERCENT_EXP = /^\d+%$/
|
|
|
|
export const COMMON_HEADERS = {
|
|
'Cache-Control': 'no-store'
|
|
}
|
|
|
|
export const SHEETS_DEF =
|
|
'const __sheets__ = [...document.adoptedStyleSheets];\n'
|
|
|
|
export function createHmrScript(legacy) {
|
|
return `
|
|
!(function vue_live_hmr(){
|
|
var ws = new WebSocket(\`ws\${location.protocol === 'https:' ? 's' : ''}://\${location.host}/ws-vue-live\`)
|
|
|
|
ws.addEventListener('open', function (r) {
|
|
if(vue_live_hmr.closed){
|
|
delete vue_live_hmr.closed
|
|
location.reload()
|
|
}
|
|
console.log('vue-live hmr ready...')
|
|
})
|
|
|
|
ws.addEventListener('close', function(){
|
|
vue_live_hmr.closed = true
|
|
setTimeout(vue_live_hmr, 2000)
|
|
})
|
|
|
|
ws.addEventListener('message', function (ev) {
|
|
var { action, data } = JSON.parse(ev.data)
|
|
|
|
switch (action) {
|
|
case 'reload':
|
|
location.reload()
|
|
break
|
|
|
|
case 'render':
|
|
{
|
|
${
|
|
legacy
|
|
? `
|
|
let stylesheet = document.head.children.namedItem(data.path)
|
|
if (stylesheet) {
|
|
stylesheet.textContent = data.content
|
|
}
|
|
`
|
|
: `
|
|
let tmp = [...document.adoptedStyleSheets]
|
|
for (let i = -1, it; (it = tmp[++i]); ) {
|
|
if (it.path === data.path) {
|
|
let stylesheet = new CSSStyleSheet()
|
|
stylesheet.path = data.path
|
|
stylesheet.replaceSync(data.content)
|
|
tmp[i] = stylesheet
|
|
document.adoptedStyleSheets = tmp
|
|
break
|
|
}
|
|
}
|
|
`
|
|
}
|
|
}
|
|
break
|
|
}
|
|
})
|
|
})()
|
|
`
|
|
}
|