vite-uni-dev-tool 0.0.5 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -28,6 +28,7 @@ import {
28
28
  DEV_WINDOW_CLOSE,
29
29
  DEV_OPTION_GET,
30
30
  DEV_OPTION_SEND,
31
+ DEV_UNI_EVENT_CLEAR,
31
32
  } from '../const';
32
33
  import { DevStore } from '../devStore';
33
34
  import type { DevTool } from '../type';
@@ -39,6 +40,7 @@ import {
39
40
  saveTextFileH5,
40
41
  saveTextFileMicro,
41
42
  } from '../utils/index';
43
+ import type { EventBus } from '../devEventBus';
42
44
 
43
45
  /**
44
46
  * 事件中心
@@ -49,8 +51,11 @@ import {
49
51
  export class DevEvent {
50
52
  private store: DevStore;
51
53
 
52
- constructor(store: DevStore) {
54
+ private eventBus: EventBus;
55
+
56
+ constructor({ store, eventBus }: { store: DevStore; eventBus: EventBus }) {
53
57
  this.store = store;
58
+ this.eventBus = eventBus;
54
59
  this.acceptMessage();
55
60
  }
56
61
 
@@ -67,7 +72,7 @@ export class DevEvent {
67
72
  data.size = size;
68
73
  data.sizeFormat = sizeFormat;
69
74
 
70
- uni.$emit(DEV_APP_MESSAGE, data);
75
+ this.eventBus.emit(DEV_APP_MESSAGE, data);
71
76
 
72
77
  if (size > this.store.cacheMaxSize) {
73
78
  this.store?.clearDevCache();
@@ -136,8 +141,12 @@ export class DevEvent {
136
141
 
137
142
  this.resetInterceptSwitchTab();
138
143
 
144
+ this.resetUniEvent();
145
+
139
146
  this.store.setDevToolDestroy(true);
140
147
 
148
+ this.eventBus.clear();
149
+
141
150
  console.warn('[DevTool] 调试器已销毁');
142
151
  }
143
152
 
@@ -163,7 +172,7 @@ export class DevEvent {
163
172
  setTimeout(async () => {
164
173
  this.postMessage();
165
174
  }, 100);
166
- uni.$emit(EVENT_DEV_WINDOW, true);
175
+ this.eventBus.emit(EVENT_DEV_WINDOW, true);
167
176
  }
168
177
 
169
178
  /**
@@ -172,7 +181,7 @@ export class DevEvent {
172
181
  * @memberof DevEvent
173
182
  */
174
183
  closeDevToolWindow() {
175
- uni.$emit(EVENT_DEV_WINDOW, false);
184
+ this.eventBus.emit(EVENT_DEV_WINDOW, false);
176
185
  }
177
186
 
178
187
  /**
@@ -183,7 +192,7 @@ export class DevEvent {
183
192
  showDevToolButton() {
184
193
  const isDestroy = uni.getStorageSync(DEV_IS_DESTROY) ?? false;
185
194
  if (isDestroy) return;
186
- uni.$emit(EVENT_DEV_BUTTON, true);
195
+ this.eventBus.emit(EVENT_DEV_BUTTON, true);
187
196
  uni.setStorageSync(EVENT_DEV_BUTTON, true);
188
197
  this.store.setDevToolVisible(true);
189
198
  }
@@ -196,7 +205,7 @@ export class DevEvent {
196
205
  hideDevToolButton() {
197
206
  const isDestroy = uni.getStorageSync(DEV_IS_DESTROY) ?? false;
198
207
  if (isDestroy) return;
199
- uni.$emit(EVENT_DEV_BUTTON, false);
208
+ this.eventBus.emit(EVENT_DEV_BUTTON, false);
200
209
  uni.setStorageSync(EVENT_DEV_BUTTON, false);
201
210
  this.store.setDevToolVisible(false);
202
211
  }
@@ -281,7 +290,7 @@ export class DevEvent {
281
290
  sendDevToolOption() {
282
291
  const options = this.store.getDevToolOptions();
283
292
 
284
- uni.$emit(DEV_OPTION_SEND, options);
293
+ this.eventBus.emit(DEV_OPTION_SEND, options);
285
294
  }
286
295
 
287
296
  /**
@@ -290,7 +299,7 @@ export class DevEvent {
290
299
  * @memberof DevEvent
291
300
  */
292
301
  acceptMessage() {
293
- uni.$on(
302
+ this.eventBus.on(
294
303
  DEV_WINDOW_MESSAGE,
295
304
  (data: {
296
305
  type: string;
@@ -406,6 +415,8 @@ export class DevEvent {
406
415
  // 获取dev options 数据
407
416
  else if (data.type === DEV_OPTION_GET) {
408
417
  this.sendDevToolOption();
418
+ } else if (data.type === DEV_UNI_EVENT_CLEAR) {
419
+ this.uniEventClear();
409
420
  }
410
421
  },
411
422
  );
@@ -662,4 +673,23 @@ export class DevEvent {
662
673
  getDevToolDestroy() {
663
674
  return this.store.getDevToolDestroy();
664
675
  }
676
+
677
+ updateUniEventCount(type: DevTool.EventCountKey) {
678
+ this.store.updateUniEventCount(type);
679
+ this.postMessage();
680
+ }
681
+ updateUniEventList(eventList: DevTool.EventItem[]) {
682
+ this.store.updateUniEventList(eventList);
683
+ this.postMessage();
684
+ }
685
+ uniEventClear() {
686
+ this.store.uniEventClear();
687
+ }
688
+
689
+ resetUniEvent() {
690
+ uni.$on = backup.$on;
691
+ uni.$once = backup.$once;
692
+ uni.$emit = backup.$emit;
693
+ uni.$off = backup.$off;
694
+ }
665
695
  }
@@ -0,0 +1,94 @@
1
+ type EventHandler<T = any> = (payload: T) => void;
2
+
3
+ export class EventBus {
4
+ // 使用 Map 存储事件及其对应的回调函数数组
5
+ private events: Map<string, EventHandler[]> = new Map();
6
+
7
+ /**
8
+ * 订阅事件
9
+ * @param eventName 事件名称
10
+ * @param handler 事件处理函数
11
+ * @returns 返回一个取消订阅的函数
12
+ */
13
+ on<T = any>(eventName: string, handler: EventHandler<T>): () => void {
14
+ if (!this.events.has(eventName)) {
15
+ this.events.set(eventName, []);
16
+ }
17
+ const handlers = this.events.get(eventName)!;
18
+ handlers.push(handler as EventHandler);
19
+
20
+ // 返回取消订阅的函数
21
+ return () => {
22
+ this.off(eventName, handler);
23
+ };
24
+ }
25
+
26
+ /**
27
+ * 发布事件
28
+ * @param eventName 事件名称
29
+ * @param payload 事件携带的数据
30
+ */
31
+ emit<T = any>(eventName: string, payload: T): void {
32
+ if (this.events.has(eventName)) {
33
+ const handlers = [...this.events.get(eventName)!];
34
+ handlers.forEach((handler) => handler(payload));
35
+ }
36
+ }
37
+
38
+ /**
39
+ * 取消订阅事件
40
+ * @param eventName 事件名称
41
+ * @param handler 要取消的事件处理函数
42
+ */
43
+ off<T = any>(eventName: string, handler: EventHandler<T>): void {
44
+ if (this.events.has(eventName)) {
45
+ const handlers = this.events.get(eventName)!;
46
+ this.events.set(
47
+ eventName,
48
+ handlers.filter((h) => h !== handler),
49
+ );
50
+ }
51
+ }
52
+
53
+ /**
54
+ * 只订阅一次事件,触发后自动取消订阅
55
+ * @param eventName 事件名称
56
+ * @param handler 事件处理函数
57
+ */
58
+ once<T = any>(eventName: string, handler: EventHandler<T>): void {
59
+ const wrapper = (payload: T) => {
60
+ handler(payload);
61
+ this.off(eventName, wrapper);
62
+ };
63
+ this.on(eventName, wrapper);
64
+ }
65
+
66
+ /**
67
+ * 清除指定事件的所有订阅者,如果未提供事件名称,则清除所有事件
68
+ * @param eventName 可选的事件名称
69
+ */
70
+ clear(eventName?: string): void {
71
+ if (eventName) {
72
+ this.events.delete(eventName);
73
+ } else {
74
+ this.events.clear();
75
+ }
76
+ }
77
+
78
+ /**
79
+ * 获取指定事件的订阅者数量,如果未提供事件名称,则返回所有事件的订阅者总数
80
+ * @param eventName 可选的事件名称
81
+ * @returns 订阅者数量
82
+ */
83
+ count(eventName?: string): number {
84
+ if (eventName) {
85
+ return this.events.has(eventName)
86
+ ? this.events.get(eventName)!.length
87
+ : 0;
88
+ }
89
+ return Array.from(this.events.values()).reduce(
90
+ (sum, handlers) => sum + handlers.length,
91
+ 0,
92
+ );
93
+ }
94
+ }
@@ -1,3 +1,4 @@
1
+ import { DEV_APP_MESSAGE } from '../const';
1
2
  import { backup } from '../core';
2
3
  import { DevEvent } from '../devEvent';
3
4
  import type { DevTool } from '../type';
@@ -20,6 +21,11 @@ export class DevIntercept {
20
21
 
21
22
  initPinia = false;
22
23
 
24
+ cache$on = new Map();
25
+ cache$once = new Map();
26
+ cache$emit = new Map();
27
+ cache$off = new Map();
28
+
23
29
  constructor(options: DevTool.DevInterceptOptions) {
24
30
  this.event = options.event;
25
31
  this.init(options);
@@ -41,6 +47,8 @@ export class DevIntercept {
41
47
  this.interceptSwitchTab();
42
48
  this.interceptNavigateTo();
43
49
 
50
+ this.interceptUniEvent();
51
+
44
52
  options.enableInterceptPromiseReject && this.interceptPromiseReject();
45
53
  }
46
54
 
@@ -681,4 +689,33 @@ export class DevIntercept {
681
689
  };
682
690
  uni.uploadFile = uploadFile.bind(uni);
683
691
  }
692
+
693
+ interceptUniEventFactory(type: DevTool.EventCountKey) {
694
+ const key = `$${type}` as '$on' | '$emit' | '$once' | '$off';
695
+
696
+ uni[`$${type}`] = (eventName: string, callback: (result: any) => void) => {
697
+ const stockList = new Error()?.stack?.split('\n');
698
+ const stock = stockList?.[2];
699
+ backup?.[key]?.(eventName, callback);
700
+
701
+ this.event.updateUniEventList([
702
+ {
703
+ eventName,
704
+ timer: getCurrentDate(),
705
+ stock,
706
+ type,
707
+ },
708
+ ]);
709
+
710
+ this.event.updateUniEventCount(type);
711
+ };
712
+ }
713
+
714
+ interceptUniEvent() {
715
+ // 判断是否是相同的位置注册,相同的位置注册只算一次
716
+ this.interceptUniEventFactory('on');
717
+ this.interceptUniEventFactory('once');
718
+ this.interceptUniEventFactory('emit');
719
+ this.interceptUniEventFactory('off');
720
+ }
684
721
  }
@@ -11,6 +11,7 @@ import {
11
11
  getWifiIp,
12
12
  isBoolean,
13
13
  } from '../utils/index';
14
+ import { backup } from '../core';
14
15
 
15
16
  /** 数据存储中心 */
16
17
  export class DevStore {
@@ -28,6 +29,13 @@ export class DevStore {
28
29
  wsList: [],
29
30
  netWorkStatus: {},
30
31
  uploadList: [],
32
+ eventList: [],
33
+ eventCount: {
34
+ on: 0,
35
+ once: 0,
36
+ emit: 0,
37
+ off: 0,
38
+ },
31
39
  };
32
40
 
33
41
  /** 调试配置 */
@@ -40,6 +48,9 @@ export class DevStore {
40
48
  private networkMaxSize = 1000;
41
49
  /** ws数据最大值 */
42
50
  private wsDataMaxSize = 1000;
51
+ /** 事件列表最大值 */
52
+ private eventMaxSize = 1000;
53
+
43
54
  /** 缓存最大值 */
44
55
  cacheMaxSize = 8 * 1024 * 1024 * 10;
45
56
 
@@ -314,6 +325,14 @@ export class DevStore {
314
325
  this.state.windowInfo = {};
315
326
  this.state.systemInfo = {};
316
327
  this.state.netWorkStatus = {};
328
+
329
+ this.state.eventList = [];
330
+ this.state.eventCount = {
331
+ on: 0,
332
+ once: 0,
333
+ emit: 0,
334
+ off: 0,
335
+ };
317
336
  }
318
337
 
319
338
  addUploadTask(index: number | string, task: UniApp.UploadTask) {
@@ -599,4 +618,58 @@ export class DevStore {
599
618
  }
600
619
  return '';
601
620
  }
621
+
622
+ /**
623
+ * 新增事件
624
+ *
625
+ * @param {DevTool.EventItem} event
626
+ * @memberof DevStore
627
+ */
628
+ addEventItem(event: DevTool.EventItem) {
629
+ if (!this.state.eventList) {
630
+ this.state.eventList = [];
631
+ }
632
+ this.state.eventList?.push(event);
633
+ }
634
+
635
+ /**
636
+ * 增加注册事件的数量
637
+ *
638
+ * @param {DevTool.EventCountKey} type
639
+ * @memberof DevStore
640
+ */
641
+ updateUniEventCount(type: DevTool.EventCountKey) {
642
+ if (!this.state.eventCount) {
643
+ this.state.eventCount = {
644
+ on: 0,
645
+ once: 0,
646
+ emit: 0,
647
+ off: 0,
648
+ };
649
+ }
650
+ this.state.eventCount[type] = this.state.eventCount[type] + 1;
651
+ }
652
+
653
+ updateUniEventList(evenList: DevTool.EventItem[]) {
654
+ const len = this.state.eventList?.length ?? 0;
655
+ const max = this.eventMaxSize;
656
+
657
+ if (len + evenList.length > max) {
658
+ this.state.eventList?.splice(0, len - max - evenList.length);
659
+ }
660
+ this.state.eventList?.push(...evenList);
661
+ console.log('this.state.eventList: ', this.state.eventList);
662
+
663
+ return this.state.eventList;
664
+ }
665
+
666
+ uniEventClear() {
667
+ this.state.eventCount = {
668
+ on: 0,
669
+ once: 0,
670
+ emit: 0,
671
+ off: 0,
672
+ };
673
+ this.state.eventList = [];
674
+ }
602
675
  }
package/dev/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { default as uniGlobalComponents } from './plugins/uniGlobalComponents/uniGlobalComponents';
2
- import { default as uniDevTool } from './plugins/uniDevTool/uniDevTool';
3
- export { uniDevTool, uniGlobalComponents };
4
- export default uniDevTool;
1
+ import { default as uniGlobalComponents } from './plugins/uniGlobalComponents/uniGlobalComponents';
2
+ import { default as uniDevTool } from './plugins/uniDevTool/uniDevTool';
3
+ export { uniDevTool, uniGlobalComponents };
4
+ export default uniDevTool;
5
5
  //# sourceMappingURL=index.d.ts.map
package/dev/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const o=require("./plugins/uniGlobalComponents/uniGlobalComponents.js"),e=require("./plugins/uniDevTool/uniDevTool.js");exports.uniGlobalComponents=o;exports.default=e;exports.uniDevTool=e;
1
+ "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const o=require("./plugins/uniGlobalComponents/uniGlobalComponents.js"),e=require("./plugins/uniDevTool/uniDevTool.js");exports.uniGlobalComponents=o;exports.default=e;exports.uniDevTool=e;
@@ -1,72 +1,72 @@
1
- import { Plugin } from 'vite';
2
- /**
3
- * vite-uni-dev-tool 插件
4
- *
5
- * 自动在 main.ts 中注入调试插件,并在每个页面的模板中注入 DevTool 组件。
6
- *
7
- * 如果组件不是用 template 定义的,则不会被该插件添加。
8
- *
9
- *
10
- * @export
11
- * @param {{
12
- * pages: {
13
- * pages: {
14
- * path: string;
15
- * }[];
16
- * };
17
- * }} {
18
- * pages,
19
- * }
20
- * @return {*} {Plugin}
21
- */
22
- export default function uniDevTool({ pages, sourceFileServers, ...reset }: {
23
- /** 是否拦截Promise.reject 最好不要拦截 默认禁用 */
24
- enableInterceptPromiseReject?: boolean;
25
- /** 打开窗口时隐藏按钮 */
26
- openWindowHideButton?: boolean;
27
- /** 最大的console条数 */
28
- consoleMaxSize?: number;
29
- /** 最大的网络请求条数 */
30
- networkMaxSize?: number;
31
- /** 最大的上传文件条数 */
32
- uploadMaxSize?: number;
33
- /** 最大的套接字消息条数 */
34
- wsDataMaxSize?: number;
35
- /** 最大占用缓存空间 bytes */
36
- cacheMaxSize?: number;
37
- /** 按钮大小 */
38
- buttonSize?: number;
39
- /** 按钮文本 */
40
- buttonText?: string;
41
- /** 按钮文本颜色 */
42
- buttonColor?: string;
43
- /** 按钮字体大小 */
44
- buttonFontSize?: string;
45
- /** 按钮背景颜色 */
46
- buttonBackgroundColor?: string;
47
- /** 初始化时是否显示调试按钮,默认显示 */
48
- initShowDevTool?: boolean;
49
- /**
50
- * 该属性处于实验当中,谨慎使用
51
- * 读取开发环境 source file,source map,默认 禁用
52
- */
53
- useDevSource?: boolean;
54
- /**
55
- * 该属性处于实验当中,谨慎使用
56
- * 开发环境 source file 服务器地址,默认 [] ,配合 useDevSource 使用
57
- */
58
- sourceFileServers?: string[];
59
- /** 页面配置 用于读取路由 */
60
- pages: {
61
- pages: {
62
- path: string;
63
- }[];
64
- subPackages?: {
65
- root: string;
66
- pages: {
67
- path: string;
68
- }[];
69
- }[];
70
- };
71
- }): Plugin;
1
+ import { Plugin } from 'vite';
2
+ /**
3
+ * vite-uni-dev-tool 插件
4
+ *
5
+ * 自动在 main.ts 中注入调试插件,并在每个页面的模板中注入 DevTool 组件。
6
+ *
7
+ * 如果组件不是用 template 定义的,则不会被该插件添加。
8
+ *
9
+ *
10
+ * @export
11
+ * @param {{
12
+ * pages: {
13
+ * pages: {
14
+ * path: string;
15
+ * }[];
16
+ * };
17
+ * }} {
18
+ * pages,
19
+ * }
20
+ * @return {*} {Plugin}
21
+ */
22
+ export default function uniDevTool({ pages, sourceFileServers, ...reset }: {
23
+ /** 是否拦截Promise.reject 最好不要拦截 默认禁用 */
24
+ enableInterceptPromiseReject?: boolean;
25
+ /** 打开窗口时隐藏按钮 */
26
+ openWindowHideButton?: boolean;
27
+ /** 最大的console条数 */
28
+ consoleMaxSize?: number;
29
+ /** 最大的网络请求条数 */
30
+ networkMaxSize?: number;
31
+ /** 最大的上传文件条数 */
32
+ uploadMaxSize?: number;
33
+ /** 最大的套接字消息条数 */
34
+ wsDataMaxSize?: number;
35
+ /** 最大占用缓存空间 bytes */
36
+ cacheMaxSize?: number;
37
+ /** 按钮大小 */
38
+ buttonSize?: number;
39
+ /** 按钮文本 */
40
+ buttonText?: string;
41
+ /** 按钮文本颜色 */
42
+ buttonColor?: string;
43
+ /** 按钮字体大小 */
44
+ buttonFontSize?: string;
45
+ /** 按钮背景颜色 */
46
+ buttonBackgroundColor?: string;
47
+ /** 初始化时是否显示调试按钮,默认显示 */
48
+ initShowDevTool?: boolean;
49
+ /**
50
+ * 该属性处于实验当中,谨慎使用
51
+ * 读取开发环境 source file,source map,默认 禁用
52
+ */
53
+ useDevSource?: boolean;
54
+ /**
55
+ * 该属性处于实验当中,谨慎使用
56
+ * 开发环境 source file 服务器地址,默认 [] ,配合 useDevSource 使用
57
+ */
58
+ sourceFileServers?: string[];
59
+ /** 页面配置 用于读取路由 */
60
+ pages: {
61
+ pages: {
62
+ path: string;
63
+ }[];
64
+ subPackages?: {
65
+ root: string;
66
+ pages: {
67
+ path: string;
68
+ }[];
69
+ }[];
70
+ };
71
+ }): Plugin;
72
72
  //# sourceMappingURL=uniDevTool.d.ts.map
@@ -1,38 +1,38 @@
1
- "use strict";const I=require("path"),S=require("fs"),l=require("../utils/index.js");function C(a){const d=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(a){for(const u in a)if(u!=="default"){const e=Object.getOwnPropertyDescriptor(a,u);Object.defineProperty(d,u,e.get?e:{enumerable:!0,get:()=>a[u]})}}return d.default=a,Object.freeze(d)}const D=C(I),y=C(S),_=/<template[^>]*>([\s\S]*?)<\/template>/,h=/<script[^>]*>([\s\S]*?)<\/script>/,g={isReady:!1,urls:[]};function T({pages:a,sourceFileServers:d,...u}){return{name:"vite-uni-dev-tool",enforce:"pre",configureServer(e){var p;e.middlewares.use((s,n,t)=>{const{originalUrl:r}=s;if(u.useDevSource&&(r!=null&&r.includes("__dev_sourcefile__"))){const i=r.replace("/__dev_sourcefile__","");try{const c=e.config.root,m=D.join(c,i),o=y.readFileSync(m,"utf-8");n.setHeader("Content-Type",l.getContentType(m)),n.end(o)}catch{t()}}else t()}),(p=e.httpServer)==null||p.once("listening",()=>{var t;const s=(t=e.httpServer)==null?void 0:t.address(),n=l.getLocalIPs();if(s&&!Array.isArray(s)&&typeof s!="string"){const r=n.map(i=>`http://${i}:${s.port}/__dev_sourcefile__`);g.isReady=!0,g.urls=r,console.warn(`
2
- ⚡️ vite-uni-dev-tool source file server running at:
3
- ${n.map(i=>`➜ Source File Network: http://${i}:${s==null?void 0:s.port}/__dev_sourcefile__`).join(`
4
- `)}
5
- `)}})},transform(e,p){var s;if(p.endsWith("/src/main.ts"))try{const n=e.split(`
6
- `);let t=[...n];const r=l.findInsertionIndex(t,c=>c.trim().startsWith("import")||c.trim().startsWith("export"));r!==-1&&t.splice(r,0,"import DevTool from 'vite-uni-dev-tool/dev/components/DevTool/index.vue';");const i=l.findInsertionIndex(t,c=>c.includes(".mount(")||c.includes("createApp("));if(i!==-1&&t.splice(i+1,0," app.component('DevTool', DevTool);"),t.length!==n.length)return{code:t.join(`
7
- `),map:null}}catch(n){return console.error("[DevTool] 转换 main 文件时出错:",n),{code:e,map:null}}if(p.endsWith("/src/App.vue")){const n=e.match(h);if(n&&n[1]){const t=n[1].trim(),r=l.hasImportCurrentInstance(t),i=l.hasImportOnLaunch(t),c=r?"":"import { getCurrentInstance } from 'vue'",m=i?"":"import { onLaunch } from '@dcloudio/uni-app'",o=`
8
- import { initDevTool, console } from 'vite-uni-dev-tool/dev/core';
9
- import pagesJson from './pages.json';
10
- ${t}
11
- onLaunch(() => {
12
- const vue3instance = getCurrentInstance();
13
- initDevTool({
14
- pagesJson,
15
- vue3instance,
16
- mode: import.meta.env.MODE,
17
- sourceFileServers: [
18
- ${[...g.urls??[],...d??[]].map(v=>`'${v}'`)}
19
- ],
20
- useDevSource: ${u.useDevSource},
21
- ...${JSON.stringify(u)},
22
- });
23
- });`;return{code:e.replace(h,`
24
- <script lang="ts" setup>
25
- ${c}
26
- ${m}
27
- ${o}
28
- <\/script>`),map:null}}return{code:e,map:null}}if(p.endsWith(".vue")){const n=e.includes("<template>"),t=a.pages.some(o=>p.includes(o.path)),r=(s=a.subPackages)==null?void 0:s.some(o=>o.pages.some(f=>p.includes(`${o.root}/${f.path}`))),i=["<DevTool />"],c=l.hasImportConsole(e),m=l.hasUseConsole(e);if(!c&&m&&!p.endsWith("/src/App.vue")){const o=e.match(h);if(o&&o[1]){const v=`
29
- import { console } from 'vite-uni-dev-tool/dev/core';
30
- ${o[1]}
31
- `;e=e.replace(h,`
32
- <script lang="ts" setup>
33
- ${v}
34
- <\/script>`)}}if((t||r)&&n){const o=e.match(_);let f=e;if(o&&o[1]){const $=`${o[1].trim()}
35
- ${i.join(`
36
- `)}`;f=e.replace(_,`<template>
37
- ${$}
38
- </template>`)}return{code:f,map:null}}}return{code:e,map:null}}}}module.exports=T;
1
+ "use strict";const I=require("path"),S=require("fs"),l=require("../utils/index.js");function C(a){const d=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(a){for(const u in a)if(u!=="default"){const e=Object.getOwnPropertyDescriptor(a,u);Object.defineProperty(d,u,e.get?e:{enumerable:!0,get:()=>a[u]})}}return d.default=a,Object.freeze(d)}const D=C(I),y=C(S),_=/<template[^>]*>([\s\S]*?)<\/template>/,h=/<script[^>]*>([\s\S]*?)<\/script>/,g={isReady:!1,urls:[]};function T({pages:a,sourceFileServers:d,...u}){return{name:"vite-uni-dev-tool",enforce:"pre",configureServer(e){var p;e.middlewares.use((s,n,t)=>{const{originalUrl:r}=s;if(u.useDevSource&&(r!=null&&r.includes("__dev_sourcefile__"))){const i=r.replace("/__dev_sourcefile__","");try{const c=e.config.root,m=D.join(c,i),o=y.readFileSync(m,"utf-8");n.setHeader("Content-Type",l.getContentType(m)),n.end(o)}catch{t()}}else t()}),(p=e.httpServer)==null||p.once("listening",()=>{var t;const s=(t=e.httpServer)==null?void 0:t.address(),n=l.getLocalIPs();if(s&&!Array.isArray(s)&&typeof s!="string"){const r=n.map(i=>`http://${i}:${s.port}/__dev_sourcefile__`);g.isReady=!0,g.urls=r,console.warn(`
2
+ ⚡️ vite-uni-dev-tool source file server running at:
3
+ ${n.map(i=>`➜ Source File Network: http://${i}:${s==null?void 0:s.port}/__dev_sourcefile__`).join(`
4
+ `)}
5
+ `)}})},transform(e,p){var s;if(p.endsWith("/src/main.ts"))try{const n=e.split(`
6
+ `);let t=[...n];const r=l.findInsertionIndex(t,c=>c.trim().startsWith("import")||c.trim().startsWith("export"));r!==-1&&t.splice(r,0,"import DevTool from 'vite-uni-dev-tool/dev/components/DevTool/index.vue';");const i=l.findInsertionIndex(t,c=>c.includes(".mount(")||c.includes("createApp("));if(i!==-1&&t.splice(i+1,0," app.component('DevTool', DevTool);"),t.length!==n.length)return{code:t.join(`
7
+ `),map:null}}catch(n){return console.error("[DevTool] 转换 main 文件时出错:",n),{code:e,map:null}}if(p.endsWith("/src/App.vue")){const n=e.match(h);if(n&&n[1]){const t=n[1].trim(),r=l.hasImportCurrentInstance(t),i=l.hasImportOnLaunch(t),c=r?"":"import { getCurrentInstance } from 'vue'",m=i?"":"import { onLaunch } from '@dcloudio/uni-app'",o=`
8
+ import { initDevTool, console } from 'vite-uni-dev-tool/dev/core';
9
+ import pagesJson from './pages.json';
10
+ ${t}
11
+ onLaunch(() => {
12
+ const vue3instance = getCurrentInstance();
13
+ initDevTool({
14
+ pagesJson,
15
+ vue3instance,
16
+ mode: import.meta.env.MODE,
17
+ sourceFileServers: [
18
+ ${[...g.urls??[],...d??[]].map(v=>`'${v}'`)}
19
+ ],
20
+ useDevSource: ${u.useDevSource},
21
+ ...${JSON.stringify(u)},
22
+ });
23
+ });`;return{code:e.replace(h,`
24
+ <script lang="ts" setup>
25
+ ${c}
26
+ ${m}
27
+ ${o}
28
+ <\/script>`),map:null}}return{code:e,map:null}}if(p.endsWith(".vue")){const n=e.includes("<template>"),t=a.pages.some(o=>p.includes(o.path)),r=(s=a.subPackages)==null?void 0:s.some(o=>o.pages.some(f=>p.includes(`${o.root}/${f.path}`))),i=["<DevTool />"],c=l.hasImportConsole(e),m=l.hasUseConsole(e);if(!c&&m&&!p.endsWith("/src/App.vue")){const o=e.match(h);if(o&&o[1]){const v=`
29
+ import { console } from 'vite-uni-dev-tool/dev/core';
30
+ ${o[1]}
31
+ `;e=e.replace(h,`
32
+ <script lang="ts" setup>
33
+ ${v}
34
+ <\/script>`)}}if((t||r)&&n){const o=e.match(_);let f=e;if(o&&o[1]){const $=`${o[1].trim()}
35
+ ${i.join(`
36
+ `)}`;f=e.replace(_,`<template>
37
+ ${$}
38
+ </template>`)}return{code:f,map:null}}}return{code:e,map:null}}}}module.exports=T;