前端vite+rollup前端监控初始化——封装基础fmp消耗时间的npm包并且发布npm beta版本

ops/2024/9/24 4:03:22/

文章目录

    • ⭐前言
      • 💖vue3系列文章
    • ⭐初始化npm项目
      • 💖type为module
      • 💖rollup.config.js
    • ⭐封装fmp耗时计算的class
      • 💖npm build打包class对象
    • ⭐发布npm的beta版本
      • 💖 npm发布beta版本
    • ⭐安装web-performance-tool的beta版本并使用
      • 💖 安装beta版本
      • 💖 vue3中使用
    • ⭐结束

yma16-logo

⭐前言

大家好,我是yma16,本文分享关于 前端vite+rollup——封装性能优化npm包。

什么是 rollup
Rollup 是一个用于 JavaScript 的模块打包工具,它将小的代码片段编译成更大、更复杂的代码,例如库或应用程序。它使用 JavaScript 的 ES6 版本中包含的新标准化代码模块格式,而不是以前的 CommonJS 和 AMD 等特殊解决方案。ES 模块允许你自由无缝地组合你最喜欢的库中最有用的个别函数。这在未来将在所有场景原生支持,但 Rollup 让你今天就可以开始这样做。
npm普遍性
通过 npm,开发者可以轻松地搜索和安装成千上万个可重用的代码包。npm 提供了一个全球性的软件注册表(registry),开发者可以在其中发布他们的模块,以便其他人能够方便地使用它们。

💖vue3系列文章

vue3 + fastapi 实现选择目录所有文件自定义上传到服务器
前端vue2、vue3去掉url路由“ # ”号——nginx配置
csdn新星计划vue3+ts+antd赛道——利用inscode搭建vue3(ts)+antd前端模板
认识vite_vue3 初始化项目到打包
python_selenuim获取csdn新星赛道选手所在城市用echarts地图显示
让大模型分析csdn文章质量 —— 提取csdn博客评论在文心一言分析评论区内容
前端vue3——html2canvas给网站截图生成宣传海报
vue3+echarts可视化——记录我的2023编程之旅
前端vite+vue3——自动化配置路由布局
前端vite+vue3——可视化页面性能耗时指标(fmp、fp

npm_22">⭐初始化npm项目

初始化

npm init

<a class=npm init" />

💖type为module

package.json 添加type为module

   {"type": "module"}

安装 rollup

npm install rollup --save-dev
npm install --save-dev @rollup/plugin-json
npm install --save-dev @rollup/plugin-terser

添加build

{"scripts": {"build": "rollup --config"}
}

💖rollup.config.js

配置rollup.config.js,打包src目录下的main.js

// rollup.config.js
import json from '@rollup/plugin-json';
import terser from '@rollup/plugin-terser';export default {input: 'src/main.js',output: [{file: 'dist/index.js',}],plugins: [json(), terser()]
};

⭐封装fmp耗时计算的class

原理:
Performance 接口可以获取到当前页面中与性能相关的信息
performance
并对外暴露一个mutation的使用方式
完整代码如下

class WebPerformance {// performanceperformanceConfig = {}constructor() {// 初始化为{}this.performanceConfig = {}}// 获取performancegetPerformance() {return this.performanceConfig}// 配置performancesetPerformance(key, value) {this.performanceConfig[key] = value}calcPerformance() {// Time to when activation occurredlet activationStart =performance.getEntriesByType("navigation")[0].activationStart;// Time to first paintlet firstPaint = performance.getEntriesByName("first-paint")[0].startTime;// Time to first contentful paintlet firstContentfulPaint = performance.getEntriesByName("first-contentful-paint",)[0].startTime;console.log("time to first paint: " + (firstPaint - activationStart));console.log("time to first-contentful-paint: " + (firstContentfulPaint - activationStart),);this.setPerformance('time to first paint', firstPaint - activationStart)this.setPerformance('time to first-contentful-paint', firstContentfulPaint - activationStart)const entries = performance.getEntriesByType("navigation");const that = thisentries.forEach((entry) => {console.log(`${entry.name}: domComplete time: ${entry.domComplete}ms`);that.setPerformance('domComplete time', entry.domComplete)const domContentLoadedTime =entry.domContentLoadedEventEnd - entry.domContentLoadedEventStart;console.log(`${entry.name}: DOMContentLoaded processing time: ${domContentLoadedTime}ms`,);that.setPerformance(entry.name, domContentLoadedTime)});}// 监听 dom变化mutationDomAction(listenDom, callbackAction) {console.log('listenDom', listenDom);// 观察器的配置(需要观察什么变动)const config = { attributes: true, childList: true, subtree: true };// 当观察到变动时执行的回调函数const callback = function(mutationsList, observer) {console.log('listenDom', listenDom)const renderHeight = listenDom.offsetHeightconsole.log('renderHeight_____________', renderHeight)console.log('change_______________', mutationsList)// Use traditional 'for loops' for IE 11// for (let mutation of mutationsList) {//     if (mutation.type === "childList") {//         console.log("A child node has been added or removed.");//     } else if (mutation.type === "attributes") {//         console.log("The " + mutation.attributeName + " attribute was modified.");//     }// }if (parseInt(renderHeight)) {// 第一次监听dom 存在高度则判定已经渲染完root节点,不关注子节点callbackAction()// 停止观察observer.disconnect();}};// 创建一个观察器实例并传入回调函数const observer = new MutationObserver(callback);// 以上述配置开始观察目标节点observer.observe(listenDom, config);}
}export { WebPerformance }

注意其中的class需要export抛出来。

npm_buildclass_171">💖npm build打包class对象

打包对象

npm run build

结果如下,打包的index文件已经压缩
build

npmbeta_178">⭐发布npm的beta版本

发布npm的基础篇
nodejs_npm发布package
配置npm包的package.json

<a class=npm set" />
配置如下

{"name": "web-performance-tool","version": "1.0.0-bata.0","description": "web performance calc","main": "index.js","scripts": {"test": "echo \"Error: no test specified\" && exit 1"},"keywords": ["fmp","fp","performance"],"author": "yma16","license": "ISC"
}

npmbeta_204">💖 npm发布beta版本

npm publish --tag beta

发布成功
<a class=npm publish beta" />
npm包的地址
https://www.npmjs.com/package/web-performance-tool
<a class=npm package" />

⭐安装web-performance-tool的beta版本并使用

💖 安装beta版本

npm 安装指定镜像https://registry.npmmirror.com

npm install web-performance-tool@beta --registry https://registry.npmmirror.com

使用yarn

yarn add  web-performance-tool@beta --registry https://registry.npmmirror.com

💖 vue3中使用

页面渲染完使用WebPerformance

import {WebPerformance} from 'web-performance-tool';
onMounted(()=>{const WebPerformanceInstance=new WebPerformance();// 计算性能WebPerformance.calcPerformance();console.log('性能指标:',WebPerformanceInstance.getPerformance())
})

计算结果符合预期:

time to first paint: 1326.7000000001863
time to first-contentful-paint: 1326.7000000001863

在这里插入图片描述

⭐结束

本文分享到这结束,如有错误或者不足之处欢迎指出!
earth

👍 点赞,是我创作的动力!
⭐️ 收藏,是我努力的方向!
✏️ 评论,是我进步的财富!
💖 最后,感谢你的阅读!


http://www.ppmy.cn/ops/32431.html

相关文章

【LLM第二篇】stable diffusion扩散模型、名词解释

最近在整理大模型的相关资料&#xff0c;发现了几个名词&#xff0c;不是很懂&#xff0c;这里整理一下&#xff1a; stable diffusion&#xff08;SD)模型&#xff1a; 扩散模型&#xff08;Diffusion model&#xff09;的一种&#xff0c;主要用于生成高质量的图像&#xf…

USB2.0和USB3.0识别方式

一. USB2.0识别方式 USB2.0向下兼容USB1.0和USB1.1&#xff0c;分为低速、全速和高速三种模式。 1. 全速和低速识别 根据规范&#xff0c;全速和低速通过设备端的上拉电阻进行区分。当设备插入HUB或上电时&#xff0c;有上拉电阻的那根数据线就会被拉高&#xff0c;HUB根据D…

StarryCoding 算法小白周赛2 题解与代码(含视频题解)

比赛链接&#xff08;含视频题解&#xff09;&#xff1a;https://www.starrycoding.com/contest/4 A题题解&#xff1a; 题目大意 给你一个由 n n n 个正整数组成的数组 a a a&#xff0c;询问这个数组是否是严格单调递增的。 思路 因为他会按照“拜访时间安排表”的顺…

android_systemServer进程启动流程

一&#xff0c;systemServer进程是被Zygote进程fork出来的&#xff0c;具体代码&#xff0c; 在startBootstrapServices、startCoreServices、startOtherServices、startApexServices中&#xff0c;对各类服务进行了启动&#xff0c;比如我们常见的ActivityManagerService、Pa…

区块链 | IPFS:IPNS(实操版)

&#x1f98a;原文&#xff1a;Publishing IPNS names Publishing IPNS names with Kubo Step1&#xff1a; 启动你的 IPFS 守护进程&#xff08;如果尚未运行&#xff09;&#xff1a; $ ipfs daemon说明&#xff1a;以 $ 开头的是命令&#xff0c;以 > 开头的是执行结果…

(一)深度神经网络的安全性和可信度的调查----验证、测试、对抗性攻击和防御,以及可解释性

在过去的几年中&#xff0c;深度神经网络&#xff08;DNNs&#xff09;在几个长期任务上实现人类水平的表现方面取得了重大进展。随着dnn在各种应用上的广泛应用&#xff0c;人们对其安全性和可信度的担忧被公开提出&#xff0c;特别是在广泛报道涉及自动驾驶汽车的致命事件之后…

3.SpringSecurity基本原理

SpringSecurity本质是一个过滤器链。十多个过滤器构成一个过滤器链。 这些过滤器在项目启动就会进行加载。每个过滤器执行放行操作才会执行下一个过滤器。 常见过滤器 FilterSecurityInterceptor 是一个方法级的权限过滤器&#xff0c;基本位于过滤器链的最底部。 Excepti…

软考网络工程师 第六章 第二部分 第三节 IP分类与特殊IP地址

IPV4地址分类 A类&#xff1a;1.0.0.0 - 127.255.255.255 B类&#xff1a;128.0.0.0 - 191.255.255.255 ABC类单播地址&#xff0c;应用最广 C类&#xff1a;192.0.0.0 - 223.255.255.255 D类&#xff1a;224.0.0.0 - 239.255.255.255 D类组播地址 E类&#x…