惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
博客园_首页
IT之家
IT之家
The Cloudflare Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
酷 壳 – CoolShell
酷 壳 – CoolShell
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Y
Y Combinator Blog
A
About on SuperTechFans
Microsoft Azure Blog
Microsoft Azure Blog
美团技术团队
S
SegmentFault 最新的问题
T
Tailwind CSS Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
M
MIT News - Artificial intelligence
WordPress大学
WordPress大学
B
Blog RSS Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 【当耐特】
小众软件
小众软件
有赞技术团队
有赞技术团队

时间的朋友

Windows 命令行密码重置 Anaconda安装 typescript 注解解读1 Konvajs Shape加载自定义图片 sshpass 使用 why-is-node-running webgl笔记 SharedArrayBuffer is not defined blender 常用快捷键 | 时间的朋友 vue -- v3.4commit提交记录2 vue2 升级vue3报错问题整理 着色器 expressjs 源码 hyper-V arch linux 网络配置 element input数字格式化 three 拼接货架 WebAudio笔记 Windows nginx重启bat脚本 vue -- v3.4commit提交记录 URI malformed vue3 -- Class 对象在组件中使用范例 | 时间的朋友 ruby 安装和升级 element-plus 老版本cascader使用卡死问题 vue3 内置Transition组件 | 时间的朋友 前端memo的实现 | 时间的朋友 Vue -- vue-class-component源码 | 时间的朋友 linux 优化脚本 typescript 装饰器 | 时间的朋友 microbundle 源码 | 时间的朋友 WSL2问题解决WslRegisterDistribution failed with error: 0x800701bc
vite -- 客户端错误弹窗展示 | 时间的朋友
2022-06-22 · via 时间的朋友

Published: · LastMod: June 23, 2022 · 438 words

Vite client overlay 🔗

利用Custom Component中的自定义组件,封装了一个自定义的web 标签

1
2
3
4
export const overlayId = 'vite-error-overlay'
if (customElements && !customElements.get(overlayId)) {
  customElements.define(overlayId, ErrorOverlay)
}

实例化方法 🔗

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// 建立标签的根元素
this.root = this.attachShadow({ mode: 'open' })
// 根元素上添加对应的html
this.root.innerHTML = template

codeframeRE.lastIndex = 0
const hasFrame = err.frame && codeframeRE.test(err.frame)
const message = hasFrame
  ? err.message.replace(codeframeRE, '')
  : err.message
// 插件的错误展示
if (err.plugin) {
  this.text('.plugin', `[plugin:${err.plugin}] `)
}
// 写入错误正文
this.text('.message-body', message.trim())

const [file] = (err.loc?.file || err.id || 'unknown file').split(`?`)
// 获取对应的文件
if (err.loc) {
  this.text('.file', `${file}:${err.loc.line}:${err.loc.column}`, true)
} else if (err.id) {
  this.text('.file', file)
}

if (hasFrame) {
  this.text('.frame', err.frame!.trim())
}
// 错误堆栈信息
this.text('.stack', err.stack, true)

// 取消错误的点击冒泡
this.root.querySelector('.window')!.addEventListener('click', (e) => {
  e.stopPropagation()
})
// 关闭事件
this.addEventListener('click', () => {
  this.close()
})

text 🔗

此方法是用来找到对应的节点,并且链接到对应的文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
text(selector: string, text: string, linkFiles = false): void {
  // 找到对应元素
  const el = this.root.querySelector(selector)!
  if (!linkFiles) {
    el.textContent = text
  } else {
    let curIndex = 0
    let match: RegExpExecArray | null
    // 循环匹配文件正则,找到匹配的文件
    while ((match = fileRE.exec(text))) {
      const { 0: file, index } = match
      if (index != null) {
        const frag = text.slice(curIndex, index)
        // 插入文本节点
        el.appendChild(document.createTextNode(frag))
        const link = document.createElement('a')
        link.textContent = file
        link.className = 'file-link'
        link.onclick = () => {
          // 跳转到指定文件
          fetch('/__open-in-editor?file=' + encodeURIComponent(file))
        }
        // 插入链接节点
        el.appendChild(link)
        curIndex += frag.length + file.length
      }
    }
  }
}