vite-uni-dev-tool
Version:
vite-uni-dev-tool, debug, uni-app, 一处编写,到处调试
564 lines (563 loc) • 22.2 kB
JavaScript
import { DEV_TOOL_INFO as S } from "../const.js";
import "../utils/index.js";
import { setDevToolInfo as T, getDevToolInfo as k } from "../devToolInfo/index.js";
import { isBoolean as I, isNil as m, isNumber as f } from "../utils/language.js";
import { getWifiIp as y, getLanIp as M, getMicroAppIp as E } from "../utils/ip.js";
import { setValueByPath as D } from "../utils/object.js";
import { hasUrl as C, splitStrByUrl as z } from "../utils/string.js";
class O {
constructor() {
this.state = {
consoleList: [],
networkList: [],
storageList: [],
routeList: [],
vuexList: {},
piniaList: {},
deviceInfo: {},
windowInfo: {},
systemInfo: {},
appInfo: {},
wsList: [],
netWorkStatus: {},
uploadList: [],
elEventList: [],
uniEventList: [],
eventCount: {
on: 0,
once: 0,
emit: 0,
off: 0
},
screenList: [],
interceptNetworkMap: /* @__PURE__ */ new Map(),
scanCodeList: []
}, this.uploadMaxSize = 1e3, this.consoleMaxSize = 1e3, this.networkMaxSize = 1e3, this.wsDataMaxSize = 1e3, this.elEventListMaxSize = 1e3, this.uniEventListMaxSize = 1e3, this.screenMaxSize = 1e3, this.scanCodeListMaxSize = 1e3, this.cacheMaxSize = 8 * 1024 * 1024 * 10, this.uploadTaskMap = /* @__PURE__ */ new Map(), this.requestIndex = -1, this.uploadIndex = -1, this.screenIndex = -1, this.devToolVisible = !1, this.devToolDestroy = !1, this.zIndex = 1e3, this.cacheInterceptConfig = !0, this.devToolVersion = "0.0.0", this.devToolBuildTime = "", this.devToolGit = "", this.prePath = "", this.captureList = [];
const {
devToolDestroy: t = !1,
devToolButtonVisible: e = !1,
cacheInterceptConfig: s = !0,
interceptNetworkList: i = []
} = uni.getStorageSync(S);
this.devToolDestroy = t, this.devToolVisible = e, this.cacheInterceptConfig = s, this.updateInterceptNetworkList(i);
}
updateHasUpload() {
var t, e, s, i, o, a;
this.state.consoleList = (t = this.state.consoleList) == null ? void 0 : t.map((n) => ({
...n,
hasUpload: !0
})), this.state.networkList = (e = this.state.networkList) == null ? void 0 : e.map((n) => ({
...n,
hasUpload: !0
})), this.state.uploadList = (s = this.state.uploadList) == null ? void 0 : s.map((n) => ({
...n,
hasUpload: !0
})), this.state.elEventList = (i = this.state.elEventList) == null ? void 0 : i.map((n) => ({
...n,
hasUpload: !0
})), this.state.uniEventList = (o = this.state.uniEventList) == null ? void 0 : o.map((n) => ({
...n,
hasUpload: !0
})), this.state.screenList = (a = this.state.screenList) == null ? void 0 : a.map((n) => ({
...n,
hasUpload: !0
}));
}
setDevToolVisible(t) {
this.devToolVisible = t, T({
devToolButtonVisible: t
});
}
getDevToolVisible() {
return this.devToolVisible;
}
setWindowInfo(t) {
this.state.windowInfo = t;
}
setDeviceInfo(t) {
this.state.deviceInfo = t;
}
setSystemInfo(t) {
this.state.systemInfo = t;
}
setNetWorkStatus(t) {
this.state.netWorkStatus = t;
}
setVuexList(t) {
this.state.vuexList = t;
}
setPiniaList(t) {
this.state.piniaList = {
...this.state.piniaList || {},
...t
};
}
setRequestIndex(t) {
return this.requestIndex = t, this.requestIndex;
}
getDevToolOptions() {
return this.devToolOptions;
}
getRequestIndex() {
return this.requestIndex;
}
setUploadIndex(t) {
return this.uploadIndex = t, this.uploadIndex;
}
getUploadIndex() {
return this.uploadIndex;
}
setScreenIndex(t) {
return this.screenIndex = t, this.screenIndex;
}
getScreenIndex() {
return this.screenIndex;
}
setCaptureList(t) {
this.captureList = t;
}
getCaptureList() {
return this.captureList;
}
setDevToolOptions(t) {
var i, o, a;
this.devToolOptions = t, this.uploadMaxSize = t.uploadMaxSize || 1e3, this.consoleMaxSize = t.consoleMaxSize || 1e3, this.networkMaxSize = t.networkMaxSize || 1e3, this.wsDataMaxSize = t.wsDataMaxSize || 1e3, this.cacheMaxSize = t.cacheMaxSize || 8 * 1024 * 1024 * 10, this.elEventListMaxSize = t.elEventListMaxSize || 1e3, this.uniEventListMaxSize = t.uniEventListMaxSize || 1e3, this.screenMaxSize = t.screenMaxSize || 1e3, this.scanCodeListMaxSize = t.scanCodeListMaxSize || 1e3, this.zIndex = t.zIndex || 998, this.devToolVersion = t.devToolVersion ?? "0.0.0", this.devToolBuildTime = t.devToolBuildTime ?? "", this.devToolGit = t.devToolGit ?? "", this.setCaptureList(t.captureList ?? []);
const { devToolButtonVisible: e } = k();
this.devToolVisible = I(e) ? e : t.initShowDevTool ?? !0, this.setDevToolVisible(this.devToolVisible), this.setDevToolDestroy(!this.devToolVisible);
const s = ((i = t.pagesJson) == null ? void 0 : i.pages.map((n) => {
var r, h, c;
const l = (c = (h = (r = t.pagesJson) == null ? void 0 : r.tabBar) == null ? void 0 : h.list) == null ? void 0 : c.some(
(d) => d.pagePath === n.path
);
return {
...n,
type: l ? "nav" : "main"
};
})) ?? [];
(a = (o = t.pagesJson) == null ? void 0 : o.subPackages) == null || a.forEach((n) => {
n.pages.forEach((l) => {
const r = {
...l,
path: `${n.root}/${l.path}`,
type: "sub",
uvList: []
};
s.push(r);
});
}), this.setRouteList(s);
}
async getDevData() {
var n, l, r, h, c;
const t = await uni.getNetworkType();
let e = {};
(uni == null ? void 0 : uni.__dev_tool_platform__) === "mp-weixin" ? e = uni.getSystemSetting() : e = await ((n = uni == null ? void 0 : uni.getSystemInfo) == null ? void 0 : n.call(uni));
const s = (l = uni == null ? void 0 : uni.getDeviceInfo) == null ? void 0 : l.call(uni), i = (r = uni == null ? void 0 : uni.getWindowInfo) == null ? void 0 : r.call(uni), o = {
...await ((h = uni == null ? void 0 : uni.getAppBaseInfo) == null ? void 0 : h.call(uni)),
...await ((c = uni == null ? void 0 : uni.getAppAuthorizeSetting) == null ? void 0 : c.call(uni)) ?? {}
}, a = y() || M() || await E();
return {
...this.state,
systemInfo: e,
deviceInfo: s,
windowInfo: i,
devToolVisible: this.getDevToolVisible(),
appInfo: o,
netWorkStatus: {
ip: a,
...t,
isConnected: t.networkType !== "none"
},
cacheInterceptConfig: this.cacheInterceptConfig,
devToolVersion: this.devToolVersion,
devToolBuildTime: this.devToolBuildTime,
devToolGit: this.devToolGit
};
}
updateStore(t) {
var o, a;
const { key: e, _oldKey: s, value: i } = t;
return s !== e && (uni.removeStorageSync(s), this.state.storageList = (o = this.state.storageList) == null ? void 0 : o.filter(
(n) => n.key === s
)), uni.setStorageSync(e, i), (a = this.state.storageList) == null || a.push({
key: e,
_oldKey: s,
value: i
}), this.state.storageList;
}
updateVuexStore(t) {
var e, s;
try {
Object.assign(((e = this.vuexStore) == null ? void 0 : e.state) ?? {}, t);
} catch (i) {
console.error("[DevTool] updateVuexStore error", i);
}
return this.setVuexList({
...((s = this.vuexStore) == null ? void 0 : s.state) ?? {}
}), this.state.vuexList;
}
setPiniaStore(t) {
this.piniaStore = t;
}
updatePiniaStore(t) {
try {
if (!this.piniaStore)
return console.error("[DevTool] updatePiniaStore piniaStore is undefined"), {};
const [e, s] = Object.entries(t)[0];
if (m(e))
return console.error("[DevTool] updatePiniaStore key is undefined or null"), {};
D(this.piniaStore.state.value, e, s), this.setPiniaList({
...this.piniaStore.state.value ?? {}
});
} catch (e) {
console.error("[DevTool] updatePiniaStore error", e);
}
return this.state.piniaList ?? {};
}
getExportData(t) {
const e = {};
return t.exportLog && (e.consoleList = this.state.consoleList), t.exportNetwork && (e.networkList = this.state.networkList), t.exportStorage && (e.storageList = this.state.storageList, e.vuexList = this.state.vuexList, e.piniaList = this.state.piniaList), t.exportUpload && (e.uploadList = this.state.uploadList), t.exportWebSocket && (e.wsList = this.state.wsList), t.exportWindow && (e.windowInfo = this.state.windowInfo), t.exportDevice && (e.deviceInfo = this.state.deviceInfo), t.exportSystem && (e.systemInfo = this.state.systemInfo), t.exportUniEvent && (e.uniEventList = this.state.uniEventList), t.exportElEvent && (e.elEventList = this.state.elEventList), t.exportScanCodeList && (e.scanCodeList = this.state.scanCodeList), e;
}
clearConsoleList() {
this.state.consoleList = [];
}
clearDevCache() {
this.state.consoleList = [], this.state.networkList = [], this.state.wsList = [], this.state.uploadList = [], this.state.elEventList = [], this.state.uniEventList = [], this.state.scanCodeList = [], this.state.eventCount = {
on: 0,
once: 0,
emit: 0,
off: 0
};
}
clearAll() {
this.state.consoleList = [], this.state.networkList = [], this.state.storageList = [], this.state.routeList = [], this.state.wsList = [], this.state.uploadList = [], this.state.vuexList = {}, this.state.piniaList = {}, this.state.deviceInfo = {}, this.state.windowInfo = {}, this.state.systemInfo = {}, this.state.netWorkStatus = {}, this.state.appInfo = {}, this.state.elEventList = [], this.state.uniEventList = [], this.state.scanCodeList = [], this.state.eventCount = {
on: 0,
once: 0,
emit: 0,
off: 0
}, this.state.screenList = [], this.state.interceptNetworkMap = /* @__PURE__ */ new Map();
}
addUploadTask(t, e) {
this.uploadTaskMap.set(t, e);
}
removeUploadTask(t) {
this.uploadTaskMap.delete(t);
}
clearUploadTask() {
this.uploadTaskMap.clear();
}
clearWsList() {
this.state.wsList = [];
}
updateWsList(t) {
var s, i, o, a, n, l;
if (!this.getDevToolVisible())
return [];
const e = ((i = (s = this.state) == null ? void 0 : s.wsList) == null ? void 0 : i.findIndex((r) => r.url === t.url)) ?? -1;
if (e > -1) {
if ((o = this.state.wsList) != null && o[e]) {
const r = this.state.wsList[e];
r.headers = t.headers ?? r.headers, r.readyState = t.readyState ?? r.readyState, r.method = t.method ?? r.method, r.protocols = t.protocols ?? r.protocols, r.message = [
...this.state.wsList[e].message ?? [],
...t.message ?? []
];
const h = this.wsDataMaxSize, c = ((a = r == null ? void 0 : r.message) == null ? void 0 : a.length) ?? 0;
Array.isArray(r == null ? void 0 : r.message) && c > h && ((n = r == null ? void 0 : r.message) == null || n.splice(0, c - h));
}
} else
(l = this.state.wsList) == null || l.push(t);
return this.state.wsList;
}
clearWsMessage(t) {
var s, i, o;
const e = ((i = (s = this.state) == null ? void 0 : s.wsList) == null ? void 0 : i.findIndex((a) => a.url === t)) ?? -1;
e !== -1 && (o = this.state.wsList) != null && o[e] && (this.state.wsList[e].message = []);
}
clearStorageList(t) {
return this.state.storageList = [], t && uni.clearStorage(), this.state.storageList;
}
setStorageList(t) {
if (this.getDevToolVisible())
return this.state.storageList = t, this.state.storageList;
}
removeStorage(t, e) {
var s;
return this.state.storageList = ((s = this.state.storageList) == null ? void 0 : s.filter((i) => i.key !== t)) ?? [], e && uni.removeStorageSync(t), this.state.storageList;
}
addStorage(t, e) {
var o;
const { key: s, value: i } = t;
return (o = this.state.storageList) == null || o.unshift({
key: s,
_oldKey: s,
value: i
}), e && uni.setStorageSync(s, i), this.state.storageList;
}
refreshStore() {
const { keys: t } = uni.getStorageInfoSync();
return this.state.storageList = t.map((e) => {
const s = uni.getStorageSync(e);
return {
key: e,
_oldKey: e,
value: s
};
}), this.state.storageList;
}
updateStoreList(t) {
var i, o, a;
const e = t[0], s = ((i = this.state.storageList) == null ? void 0 : i.findIndex((n) => n.key === e._oldKey)) ?? -1;
return s > -1 && t.length === 1 ? (o = this.state.storageList) != null && o[s] && (this.state.storageList[s] = t[0]) : (a = this.state.storageList) == null || a.push(...t), this.state.storageList;
}
setRouteList(t, e) {
var i;
if (!this.getDevToolVisible())
return;
const s = e || ((i = t == null ? void 0 : t[0]) == null ? void 0 : i.path) || "";
return this.state.routeList = t.map((o, a) => ({
...o,
name: o.style.navigationBarTitleText || "无名",
uvList: [],
index: o.path === s && a === 0 ? 4 : o.path === s ? 3 : a === 0 ? 2 : 1
})), this.state.routeList;
}
getRouteList() {
return this.state.routeList;
}
updateRouteList(t) {
var n, l, r;
const e = t ? t === "/" ? this.getCurrentPagePath() : t : this.getCurrentPagePath(), s = this.prePath;
this.state.routeList = ((n = this.state.routeList) == null ? void 0 : n.map((h, c) => ({
...h,
index: h.path === e && c === 0 ? 4 : h.path === e ? 3 : c === 0 ? 2 : 1
}))) ?? [];
function i(h, c, d) {
if (!(h != null && h[c]))
return;
h[c].uvList || (h[c].uvList = []);
const u = h[c], L = u.uvList ?? [];
if (L != null && L.length) {
const p = L.length - 1, v = L[p];
if (v && (v != null && v.inTime) && !v.outTime) {
const g = Date.now();
u.outTime = g, L[p].outTime = g, L[p].duration = g - v.inTime, u.duration = L[p].duration, u.totalDuration = L.reduce((w, x) => w + (x.duration || 0), 0), d && (u.visitTimes = L.length);
} else {
const g = Date.now();
u.inTime = g, u.outTime = 0, u.duration = 0, u.uvList || (u.uvList = []), u.uvList.push({
index: p + 1,
inTime: g,
outTime: 0,
duration: 0
}), d && (u.visitTimes = u.uvList.length);
}
} else {
const p = Date.now();
u.inTime = p, u.outTime = 0, u.duration = 0, u.totalDuration = 0, u.visitTimes = 1, u.uvList = [
{
index: 0,
inTime: p,
outTime: 0,
duration: 0
}
];
}
return h;
}
const o = ((l = this.state.routeList) == null ? void 0 : l.findIndex((h) => h.path === s)) ?? -1, a = ((r = this.state.routeList) == null ? void 0 : r.findIndex((h) => h.path === e)) ?? -1;
if (!(o === -1 && a === -1))
return o === -1 && a > -1 ? this.state.routeList = i(
[...this.state.routeList ?? []],
a,
!0
) : (this.state.routeList = i(
[...this.state.routeList ?? []],
o
), this.state.routeList = i(
[...this.state.routeList ?? []],
a,
!0
)), this.prePath = e, this.state.routeList;
}
setConsoleList(t) {
var e, s;
this.getDevToolVisible() && ((((s = (e = this.state) == null ? void 0 : e.consoleList) == null ? void 0 : s.length) ?? 0) + t.length > this.consoleMaxSize && (this.state.consoleList = []), this.state.consoleList = t);
}
updateConsoleList(t, e) {
var s, i, o, a, n, l, r;
if (this.getDevToolVisible())
return f(e) && e > -1 && t.length === 1 ? (s = this.state.consoleList) != null && s[e] && (this.state.consoleList[e] = t[0]) : ((((o = (i = this.state) == null ? void 0 : i.consoleList) == null ? void 0 : o.length) ?? 0) + t.length > this.cacheMaxSize && ((a = this.state.consoleList) == null || a.splice(
0,
this.state.consoleList.length - this.cacheMaxSize - t.length
)), (l = (n = this.state) == null ? void 0 : n.consoleList) == null || l.push(...t)), this.state.consoleList = (r = this.state.consoleList) == null ? void 0 : r.map((h) => {
const c = h.args.reduce((d, u) => {
if (u.type === "string" && C(u.value)) {
const L = z(u.value);
return [...d, ...L];
}
return [...d, u];
}, []);
return {
...h,
args: c
};
}), this.state.consoleList;
}
clearNetworkList() {
this.state.networkList = [];
}
setNetworkList(t) {
var e, s;
this.getDevToolVisible() && ((((s = (e = this.state) == null ? void 0 : e.networkList) == null ? void 0 : s.length) ?? 0) + t.length > this.networkMaxSize && (this.state.networkList = []), this.state.networkList = t);
}
updateNetworkList(t, e) {
var s, i, o, a, n, l, r;
if (f(e) && e > -1)
(i = (s = this.state) == null ? void 0 : s.networkList) != null && i[e] && (this.state.networkList[e] = t[0]);
else {
const h = this.networkMaxSize, c = ((a = (o = this.state) == null ? void 0 : o.networkList) == null ? void 0 : a.length) ?? 0;
c + t.length > h && ((n = this.state.networkList) == null || n.splice(0, c - h - t.length)), (r = (l = this.state) == null ? void 0 : l.networkList) == null || r.push(...t);
}
return this.state.networkList;
}
updateUploadList(t, e) {
var s, i, o, a, n;
if (f(e) && e > -1 && t.length === 1)
if ((s = this.state.uploadList) != null && s[e])
this.state.uploadList[e] = {
...this.state.uploadList[e],
...t[0]
};
else {
const l = this.uploadMaxSize, r = ((o = (i = this.state) == null ? void 0 : i.uploadList) == null ? void 0 : o.length) ?? 0;
r + t.length > l && ((a = this.state.uploadList) == null || a.splice(0, r - l - t.length)), (n = this.state.uploadList) == null || n.push(t[0]);
}
return this.state.uploadList;
}
clearUploadList() {
this.state.uploadList = [];
}
setDevToolDestroy(t) {
this.devToolDestroy = t, T({
devToolDestroy: t
});
}
getDevToolDestroy() {
const { devToolDestroy: t = !1 } = uni.getStorageSync(S);
return this.devToolDestroy ?? t;
}
getCurrentPagePath() {
var s, i, o;
let t = getCurrentPages(), e = t == null ? void 0 : t[t.length - 1];
return e && e.route ? e.route : ((o = (i = (s = this.state) == null ? void 0 : s.routeList) == null ? void 0 : i[0]) == null ? void 0 : o.path) ?? "";
}
/**
* 新增节点事件
*
* @param {DevTool.ElEventItem} event
* @memberof DevStore
*/
addElEvent(t) {
var i, o;
this.state.elEventList || (this.state.elEventList = []);
const e = ((i = this.state.elEventList) == null ? void 0 : i.length) ?? 0, s = this.elEventListMaxSize;
e > s && this.state.elEventList.splice(0, e - s), (o = this.state.elEventList) == null || o.push(t), this.state.elEventList = this.state.elEventList.sort((a, n) => a.timer - n.timer);
}
/**
* 清空节点事件
*
* @memberof DevStore
*/
clearElEventList() {
this.state.elEventList = [];
}
/**
* 新增事件
*
* @param {DevTool.UniEventItem} event
* @memberof DevStore
*/
addUniEvent(t) {
var e;
this.state.uniEventList || (this.state.uniEventList = []), (e = this.state.uniEventList) == null || e.push(t);
}
/**
* 增加注册事件的数量
*
* @param {DevTool.EventCountKey} type
* @memberof DevStore
*/
updateUniEventCount(t) {
this.state.eventCount || (this.state.eventCount = {
on: 0,
once: 0,
emit: 0,
off: 0
}), this.state.eventCount[t] = this.state.eventCount[t] + 1;
}
updateUniEventList(t) {
var i, o, a;
const e = ((i = this.state.uniEventList) == null ? void 0 : i.length) ?? 0, s = this.uniEventListMaxSize;
return e + t.length > s && ((o = this.state.uniEventList) == null || o.splice(0, e - s - t.length)), (a = this.state.uniEventList) == null || a.push(...t), this.state.uniEventList;
}
clearUniEvent() {
this.state.eventCount = {
on: 0,
once: 0,
emit: 0,
off: 0
}, this.state.uniEventList = [];
}
updateScreenList(t, e) {
var s, i, o, a, n;
if (f(e) && e > -1 && t.length === 1 && ((s = this.state.screenList) != null && s[e]))
(i = this.state.screenList) != null && i[e] && (this.state.screenList[e] = {
...this.state.screenList[e],
...t[0]
});
else {
const l = ((o = this.state.screenList) == null ? void 0 : o.length) ?? 0, r = this.screenMaxSize;
l + t.length > r && ((a = this.state.screenList) == null || a.splice(0, l - r - t.length)), (n = this.state.screenList) == null || n.push(...t);
}
return this.state.screenList;
}
clearCaptureScreenList() {
this.state.screenList = [];
}
addInterceptNetworkItem(t) {
var s, i, o;
const e = `${(s = t.method) == null ? void 0 : s.toUpperCase()}|${(i = t.url) == null ? void 0 : i.split("?")[0]}`;
(o = this.state.interceptNetworkMap) == null || o.set(e, t);
}
updateInterceptNetworkList(t) {
this.getDevToolVisible() && t.forEach((e) => {
var i, o, a;
const s = `${(i = e.method) == null ? void 0 : i.toUpperCase()}|${(o = e.url) == null ? void 0 : o.split("?")[0]}`;
(a = this.state.interceptNetworkMap) == null || a.set(s, e);
});
}
clearInterceptNetworkList() {
var t;
(t = this.state.interceptNetworkMap) == null || t.clear();
}
getInterceptNetworkMap() {
return this.state.interceptNetworkMap;
}
removeInterceptNetwork(t) {
var s, i, o;
const e = `${(s = t.method) == null ? void 0 : s.toUpperCase()}|${(i = t.url) == null ? void 0 : i.split("?")[0]}`;
(o = this.state.interceptNetworkMap) == null || o.delete(e);
}
changeCacheInterceptConfig(t) {
this.cacheInterceptConfig = t;
}
updateScanCodeList(t) {
var i, o, a;
const e = ((i = this.state.scanCodeList) == null ? void 0 : i.length) ?? 0, s = this.scanCodeListMaxSize;
e + t.length > s && ((o = this.state.scanCodeList) == null || o.splice(0, e - s - t.length)), (a = this.state.scanCodeList) == null || a.push(...t);
}
clearScanCodeList() {
this.state.scanCodeList = [];
}
}
export {
O as DevStore
};