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

devtools/2024/10/19 4:20:37/

文章目录

    • ⭐前言
      • 💖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/devtools/27783.html

相关文章

容器安全-镜像扫描

前言 容器镜像安全是云原生应用交付安全的重要一环&#xff0c;对上传的容器镜像进行及时安全扫描&#xff0c;并基于扫描结果选择阻断应用部署&#xff0c;可有效降低生产环境漏洞风险。容器安全面临的风险有&#xff1a;镜像风险、镜像仓库风险、编排工具风险&#xff0c;小…

java.net.BindException: Address already in use: no further information

项目场景&#xff1a; 基于Netty实现的文件管理&#xff0c;对客户端windows系统上的大量中小文件进行上传&#xff0c;并回调rest接口记录文件上传状态。 问题描述 在运行了一段时间后&#xff0c;文件传输过程中出现如下异常&#xff1a; io.netty.channel.AbstractChannel…

Linux系统中搭建Mosquitto MQTT服务并实现远程访问本地消息代理进行通信

文章目录 1. Linux 搭建 Mosquitto2. Linux 安装Cpolar3. 创建MQTT服务公网连接地址4. 客户端远程连接MQTT服务5. 代码调用MQTT服务6. 固定连接TCP公网地址7. 固定地址连接测试 今天和大家分享一下如何在Linux系统中搭建Mosquitto MQTT协议消息服务端,并结合Cpolar内网穿透工具…

数字时代的心理健康

数字时代的心理健康 单选题 1、&#xff08;&#xff09;年&#xff0c;美国国家远程通信和信息管理局&#xff08;NTIA&#xff09;首次对数字鸿沟进行了界定&#xff0c;即信息富有者与信息贫困者之间的鸿沟。 正确答案&#xff1a;B、1999 2、下列选项中&#xff0c;不属于精…

必应广告投放怎么做?怎么开户推广?

今天搜索引擎广告依旧是企业提升品牌知名度、吸引潜在客户的关键渠道之一&#xff0c;必应Bing&#xff0c;作为全球第二大搜索引擎&#xff0c;不仅拥有庞大的用户基础&#xff0c;更以其精准的定向能力和高效的转化效率&#xff0c;成为众多企业拓展市场的优选平台。 一、必…

格瑞威特 | 邀您参加2024全国水科技大会暨技术装备成果展览会

—— 展位号&#xff1a;A13 —— 企业介绍 北京格瑞威特环保设备有限公司成立于2009年&#xff0c;是专业从事设计、研发、销售智能加药计量泵、在线水质分析仪表、便携式水质分析仪表、流量计、液位计、阀门、搅拌机、烟气报警仪、加药装置等各类水处理设备及配件的OEM供服…

力扣sql题

目录 1 找出合作过至少三次的演员和导演的 id 对(actor_id, director_id) 2 获取 Sales 表中所有 sale_id 对应的 product_name 以及该产品的所有 year 和 price 。 3 查询每一个项目中员工的 平均 工作年限&#xff0c;精确到小数点后两位。 4 报告2019年春季才售出的产品…

C++ : 程序设计简单实例

首先定义一个类POINT&#xff0c;有两个int型的保护数据成员x、y表示该类对象在二维坐标系中的坐标位置&#xff0c;定义如下三个公有成员函数&#xff1a; &#xff08;1&#xff09; 构造函数&#xff1a;设置点的初始值&#xff1b; &#xff08;2&#xff09;成员函数chang…