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

本文主要是介绍前端vite+rollup前端监控初始化——封装基础fmp消耗时间的npm包并且发布npm beta版本,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

    • ⭐前言
      • 💖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项目

初始化

npm init

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 build打包class对象

打包对象

npm run build

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

⭐发布npm的beta版本

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

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"
}

💖 npm发布beta版本

npm publish --tag beta

发布成功
npm publish beta
npm包的地址
https://www.npmjs.com/package/web-performance-tool
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

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

这篇关于前端vite+rollup前端监控初始化——封装基础fmp消耗时间的npm包并且发布npm beta版本的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/952372

相关文章

问题:第一次世界大战的起止时间是 #其他#学习方法#微信

问题:第一次世界大战的起止时间是 A.1913 ~1918 年 B.1913 ~1918 年 C.1914 ~1918 年 D.1914 ~1919 年 参考答案如图所示

RedHat运维-Linux文本操作基础-AWK进阶

你不用整理,跟着敲一遍,有个印象,然后把它保存到本地,以后要用再去看,如果有了新东西,你自个再添加。这是我参考牛客上的shell编程专项题,只不过换成了问答的方式而已。不用背,就算是我自己亲自敲,我现在好多也记不住。 1. 输出nowcoder.txt文件第5行的内容 2. 输出nowcoder.txt文件第6行的内容 3. 输出nowcoder.txt文件第7行的内容 4. 输出nowcode

Vim使用基础篇

本文内容大部分来自 vimtutor,自带的教程的总结。在终端输入vimtutor 即可进入教程。 先总结一下,然后再分别介绍正常模式,插入模式,和可视模式三种模式下的命令。 目录 看完以后的汇总 1.正常模式(Normal模式) 1.移动光标 2.删除 3.【:】输入符 4.撤销 5.替换 6.重复命令【. ; ,】 7.复制粘贴 8.缩进 2.插入模式 INSERT

零基础STM32单片机编程入门(一)初识STM32单片机

文章目录 一.概要二.单片机型号命名规则三.STM32F103系统架构四.STM32F103C8T6单片机启动流程五.STM32F103C8T6单片机主要外设资源六.编程过程中芯片数据手册的作用1.单片机外设资源情况2.STM32单片机内部框图3.STM32单片机管脚图4.STM32单片机每个管脚可配功能5.单片机功耗数据6.FALSH编程时间,擦写次数7.I/O高低电平电压表格8.外设接口

公共筛选组件(二次封装antd)支持代码提示

如果项目是基于antd组件库为基础搭建,可使用此公共筛选组件 使用到的库 npm i antdnpm i lodash-esnpm i @types/lodash-es -D /components/CommonSearch index.tsx import React from 'react';import { Button, Card, Form } from 'antd'

vue, 左右布局宽,可拖动改变

1:建立一个draggableMixin.js  混入的方式使用 2:代码如下draggableMixin.js  export default {data() {return {leftWidth: 330,isDragging: false,startX: 0,startWidth: 0,};},methods: {startDragging(e) {this.isDragging = tr

ONLYOFFICE 8.1 版本桌面编辑器测评

在现代办公环境中,办公软件的重要性不言而喻。从文档处理到电子表格分析,再到演示文稿制作,强大且高效的办公软件工具能够极大提升工作效率。ONLYOFFICE 作为一个功能全面且开源的办公软件套件,一直以来都受到广大用户的关注与喜爱。而其最新发布的 ONLYOFFICE 8.1 版本桌面编辑器,更是带来了诸多改进和新特性。本文将详细评测 ONLYOFFICE 8.1 版本桌面编辑器,探讨其在功能、用户

17.用300行代码手写初体验Spring V1.0版本

1.1.课程目标 1、了解看源码最有效的方式,先猜测后验证,不要一开始就去调试代码。 2、浓缩就是精华,用 300行最简洁的代码 提炼Spring的基本设计思想。 3、掌握Spring框架的基本脉络。 1.2.内容定位 1、 具有1年以上的SpringMVC使用经验。 2、 希望深入了解Spring源码的人群,对 Spring有一个整体的宏观感受。 3、 全程手写实现SpringM

ps基础入门

1.基础      1.1新建文件      1.2创建指定形状      1.4移动工具          1.41移动画布中的任意元素          1.42移动画布          1.43修改画布大小          1.44修改图像大小      1.5框选工具      1.6矩形工具      1.7图层          1.71图层颜色修改          1

时序预测 | MATLAB实现LSTM时间序列未来多步预测-递归预测

时序预测 | MATLAB实现LSTM时间序列未来多步预测-递归预测 目录 时序预测 | MATLAB实现LSTM时间序列未来多步预测-递归预测基本介绍程序设计参考资料 基本介绍 MATLAB实现LSTM时间序列未来多步预测-递归预测。LSTM是一种含有LSTM区块(blocks)或其他的一种类神经网络,文献或其他资料中LSTM区块可能被描述成智能网络单元,因为