vite-uni-dev-tool 0.0.4 → 0.0.5

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.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # vite-uni-dev-tool
2
2
 
3
- 用于 vue3 + ts + uni-app 的开发调试插件
3
+ 用于 vue3 + ts + hooks + uni-app 的开发调试插件
4
4
 
5
5
  ## 安装
6
6
 
@@ -28,7 +28,7 @@ import * as path from 'path';
28
28
  // https://vitejs.dev/config/
29
29
  export default defineConfig({
30
30
  plugins: [
31
- // 一定要在 uni() 之前调用 否则微信小程序将无法解组件
31
+ // 一定要在 uni() 之前调用 否则微信小程序将无法正常编译组件
32
32
  uniDevTool({
33
33
  pages,
34
34
  }),
@@ -46,6 +46,27 @@ export default defineConfig({
46
46
  });
47
47
  ```
48
48
 
49
+ ### 为什么不用 subPackages?
50
+
51
+ - 从当前页跳转到subPackages页面时,会触发 uni-app 页面生命周期,有时是不希望如此的,比如在开发过程中,希望可以直接在当前页面进行调试。
52
+
53
+ ### 如何将 console 日志输出到控制台
54
+
55
+ - 0.0.5版本之后为了在生产环境中移除插件,开发环境中插件将会自动导入 `console`,无需手动导入
56
+
57
+ - 0.0.5版本之后不推荐使用 `uni.__dev__console` , 在未来版本中可能会进行移除
58
+
59
+ ```ts
60
+ // 方法 1
61
+ import { console } from 'vite-uni-dev-tool/dev/core';
62
+
63
+ console.log('hello vite-uni-dev-tool');
64
+
65
+ // 方法 2
66
+
67
+ uni.__dev__console.log('hello vite-uni-dev-tool');
68
+ ```
69
+
49
70
  ### 注意事项
50
71
 
51
72
  ### 兼容性说明
@@ -58,6 +79,13 @@ export default defineConfig({
58
79
  | ---------- | ---- | ------ |
59
80
  | Y | Y | 未测试 |
60
81
 
82
+ ### 安全性声明
83
+
84
+ 插件不收收集任何信息,只开发人员调试使用
85
+
86
+ - 插件中使用到了 `fs`,用于栈信息获取源代码文件
87
+ - 插件使用了 `uni.request` ,用于栈信息获取源代码文件
88
+
61
89
  ### 预览
62
90
 
63
91
  ![all.png](https://gitee.com/cloud_l/vite-uni-dev-tool/raw/master/packages/docs/public/all.png)
@@ -21,5 +21,6 @@ function onClick() {
21
21
  border-radius: 50%;
22
22
  border: 1px solid #000;
23
23
  box-sizing: border-box;
24
+ color: #000;
24
25
  }
25
26
  </style>
@@ -0,0 +1,214 @@
1
+ <template>
2
+ <view class="dev-tool-code">
3
+ <view class="dev-tool-code-control">
4
+ <FilterInput
5
+ v-model="modelValue"
6
+ style="width: 100%"
7
+ @search="onSearch"
8
+ />
9
+ <CloseButton style="margin-left: auto" @click="onClose" />
10
+ </view>
11
+ <view class="dev-tool-code-title">{{ fileName }}</view>
12
+ <scroll-view
13
+ scroll-y="true"
14
+ scroll-x="true"
15
+ class="dev-tool-code-list"
16
+ scroll-with-animation="true"
17
+ :scroll-top="scrollTop"
18
+ :scroll-into-view="scrollIntoView"
19
+ >
20
+ <view
21
+ v-for="(code, index) in codes"
22
+ :id="`dev-tool-code-item-${index}`"
23
+ :key="index"
24
+ :class="`dev-tool-code-item ${index === activeRowCol.row ? 'dev-tool-code-item-active' : ''}`"
25
+ >
26
+ <view class="dev-tool-code-item-index"> {{ index + 1 }}</view>
27
+
28
+ <view class="dev-tool-code-item-content" v-html="code"></view>
29
+ </view>
30
+ <Empty v-if="!codes || codes.length === 0" />
31
+ </scroll-view>
32
+ </view>
33
+ </template>
34
+ <script lang="ts" setup>
35
+ import { computed, ref, nextTick, onMounted } from 'vue';
36
+ import FilterInput from '../FilterInput/index.vue';
37
+ import CloseButton from '../CloseButton/index.vue';
38
+ import Empty from '../Empty/index.vue';
39
+ import { escapeHTML, hightLight, isAndroid, parseStock } from '../../utils';
40
+
41
+ const props = defineProps<{
42
+ url: string;
43
+ sourceFileServers?: string[];
44
+ mode?: string;
45
+ }>();
46
+
47
+ const emit = defineEmits<{ (e: 'close'): void }>();
48
+
49
+ const modelValue = ref('');
50
+ const scrollTop = ref(0);
51
+ const fileName = computed(() => {
52
+ const name = props?.url?.split('/')?.pop() ?? '文件名称未知';
53
+ return name;
54
+ });
55
+
56
+ const activeRowCol = ref({ row: -1, col: -1 });
57
+
58
+ let backupCodes: string[] = [];
59
+
60
+ const codes = ref<string[]>([]);
61
+
62
+ const scrollIntoView = ref('');
63
+
64
+ function onClose() {
65
+ emit('close');
66
+ }
67
+
68
+ function onSearch(value: string) {
69
+ codes.value = backupCodes.map((code) => {
70
+ return hightLight(code, value);
71
+ });
72
+ }
73
+
74
+ let index = 0;
75
+ function getCode(url: string, i: number = 0) {
76
+ let allUrl = url;
77
+ // 平台判断
78
+ if (isAndroid()) {
79
+ if (!props.sourceFileServers?.[i]) {
80
+ index = 0;
81
+ return;
82
+ }
83
+
84
+ allUrl = props.sourceFileServers?.[i] + '/src/' + url;
85
+ }
86
+
87
+ uni.showLoading({ mask: true });
88
+ uni.request({
89
+ url: allUrl,
90
+ success: (res) => {
91
+ if (typeof res.data === 'string') {
92
+ // 为什么要注释掉?
93
+ // 在 Android 识别到标签后会进行重启,导致代码无法显示
94
+ // TODO: 还有其它原因导致重启
95
+ const str = res.data
96
+ ?.replace(/<jscript/, '// [DevTool] 注释 <javascript')
97
+ ?.replace(/<\/script>/, '// [DevTool] 注释 </javascript>')
98
+ ?.replace(/<style/, '// [DevTool] 注释 <style')
99
+ ?.replace(/<\/style>/, '// [DevTool] 注释 </style>');
100
+ backupCodes = escapeHTML(str ?? '')
101
+ .toString()
102
+ .split('\n');
103
+
104
+ codes.value = backupCodes;
105
+
106
+ nextTick(() => {
107
+ scrollIntoView.value = `dev-tool-code-item-${activeRowCol.value.row}`;
108
+ });
109
+ }
110
+ },
111
+ fail: (err) => {
112
+ index++;
113
+ getCode(url, index);
114
+ uni.showToast({ icon: 'none', title: '正在重新尝试中...' });
115
+ },
116
+ complete: () => {
117
+ uni.hideLoading();
118
+ },
119
+ });
120
+ }
121
+
122
+ /** 开发环境获取源代码 */
123
+ function getSourceCodeDev(url: string) {
124
+ if (!url) {
125
+ uni.showToast({ icon: 'none', title: '[DevTool] url 处理异常' });
126
+ uni?.__dev__console?.log('[DevTool] url 处理异常');
127
+ return;
128
+ }
129
+
130
+ getCode(url, index);
131
+ }
132
+
133
+ onMounted(() => {
134
+ let url = props?.url;
135
+
136
+ const { path, col, row } = parseStock(props?.url ?? '');
137
+
138
+ if (path) {
139
+ url = path;
140
+ }
141
+
142
+ activeRowCol.value = {
143
+ col,
144
+ row,
145
+ };
146
+
147
+ if (props.mode === 'development') {
148
+ // 开发环境查看源码
149
+ getSourceCodeDev(url);
150
+ } else if (props.mode === 'production') {
151
+ // TODO 生产环境查看源码
152
+ }
153
+ });
154
+ </script>
155
+ <style scoped>
156
+ .dev-tool-code {
157
+ position: fixed;
158
+ width: 100vw;
159
+ height: 100vh;
160
+ z-index: 10000;
161
+ top: 0;
162
+ left: 0;
163
+ padding: 0 16px;
164
+ /* #ifdef H5 */
165
+ padding: 50px 16px;
166
+ /* #endif */
167
+
168
+ background-color: rgba(255, 255, 255, 0.95);
169
+ box-sizing: border-box;
170
+ color: #000;
171
+ }
172
+
173
+ .dev-tool-code-control {
174
+ display: flex;
175
+ align-items: center;
176
+ gap: 12px;
177
+ height: 32px;
178
+ border-bottom: 1px solid var(--dev-tool-border-color);
179
+ box-sizing: border-box;
180
+ }
181
+
182
+ .dev-tool-code-title {
183
+ height: 32px;
184
+ line-height: 32px;
185
+ margin-bottom: 4px;
186
+ border-bottom: 1px solid var(--dev-tool-border-color);
187
+ box-sizing: border-box;
188
+ }
189
+
190
+ .dev-tool-code-list {
191
+ height: calc(100% - 68px);
192
+ }
193
+
194
+ .dev-tool-code-item {
195
+ display: flex;
196
+ align-items: center;
197
+ height: 28px;
198
+ }
199
+ .dev-tool-code-item-active {
200
+ color: #fff;
201
+ background-color: var(--dev-tool-main-color);
202
+ }
203
+
204
+ .dev-tool-code-item-index {
205
+ flex-shrink: 0;
206
+ width: 20px;
207
+ margin-right: 8px;
208
+ text-align: right;
209
+ }
210
+
211
+ .dev-tool-code-item-content {
212
+ white-space: pre;
213
+ }
214
+ </style>
@@ -18,21 +18,85 @@
18
18
  <view class="console-time">{{ consoleItem.time }}</view>
19
19
  <view class="console-other" v-html="consoleItem.position"></view>
20
20
  <view
21
- class="console-other"
21
+ :class="`console-other ${isUseDevSource ? 'console-stack' : ''}`"
22
22
  v-if="consoleItem.stack"
23
23
  v-html="consoleItem.stack"
24
+ @click="onStackClick"
24
25
  ></view>
25
26
  </view>
26
27
  </view>
27
28
  </view>
28
29
  <!-- <view class="console-copy">C</view> -->
30
+ <Code
31
+ v-if="openCode && consoleItem.stack"
32
+ :url="consoleItem.stack"
33
+ :sourceFileServers="sourceFileServers"
34
+ :mode="mode"
35
+ @close="onCloseCode"
36
+ />
29
37
  </view>
30
38
  </template>
31
39
 
32
40
  <script setup lang="ts">
41
+ import { ref, computed } from 'vue';
33
42
  import Tag from '../Tag/index.vue';
43
+ import Code from './Code.vue';
34
44
  import type { DevTool } from '../../type';
35
- defineProps<{ consoleItem: DevTool.ConsoleItem }>();
45
+ import { isAndroid, isMockWX } from '../../utils';
46
+ const props = defineProps<{
47
+ consoleItem: DevTool.ConsoleItem;
48
+ sourceFileServers?: string[];
49
+ mode?: string;
50
+ useDevSource?: boolean;
51
+ }>();
52
+
53
+ const openCode = ref(false);
54
+
55
+ const isUseDevSource = computed(() => {
56
+ return (
57
+ !isMockWX(props?.consoleItem?.stack ?? '') &&
58
+ props.mode === 'development' &&
59
+ props.useDevSource
60
+ );
61
+ });
62
+
63
+ const isWXLink = computed(() => {
64
+ return (
65
+ isMockWX(props?.consoleItem?.stack ?? '') || props.mode !== 'development'
66
+ );
67
+ });
68
+
69
+ function onStackClick() {
70
+ if (isWXLink.value) {
71
+ uni.showToast({
72
+ icon: 'none',
73
+ title: '[DevTool] 请在小程序真机调试模式下查看源码',
74
+ });
75
+ return;
76
+ }
77
+
78
+ if (!isUseDevSource.value) {
79
+ return;
80
+ }
81
+
82
+ if (isAndroid()) {
83
+ if (props.sourceFileServers && props.sourceFileServers.length > 0) {
84
+ openCode.value = true;
85
+ } else {
86
+ uni.showToast({
87
+ icon: 'none',
88
+ title: '[DevTool] sourceFileServers 配置异常',
89
+ });
90
+ uni?.__dev__console?.log('[DevTool] sourceFileServers 配置异常');
91
+ }
92
+ } else {
93
+ openCode.value = true;
94
+ }
95
+ }
96
+
97
+ function onCloseCode() {
98
+ openCode.value = false;
99
+ }
36
100
  </script>
37
101
  <style scoped>
38
102
  .console-item {
@@ -41,19 +105,20 @@ defineProps<{ consoleItem: DevTool.ConsoleItem }>();
41
105
  border-bottom: 1px solid var(--dev-tool-border-color);
42
106
  font-size: var(--dev-tool-base-font-size);
43
107
  }
44
- .console-item .console-info {
108
+ .console-info {
45
109
  flex: 1;
46
110
  }
47
- .console-item .console-info .console-args {
111
+ .console-args {
48
112
  display: flex;
49
113
  flex-wrap: wrap;
50
114
  color: #000;
51
115
  }
52
- .console-item .console-info .console-args .console-arg {
116
+ .console-arg {
53
117
  margin-right: 4px;
118
+ white-space: pre-line;
54
119
  word-break: break-all;
55
120
  }
56
- .console-item .console-info .console-position {
121
+ .console-position {
57
122
  display: flex;
58
123
  align-items: flex-start;
59
124
  justify-content: space-between;
@@ -61,14 +126,20 @@ defineProps<{ consoleItem: DevTool.ConsoleItem }>();
61
126
  color: #616161;
62
127
  text-align: right;
63
128
  }
64
- .console-item .console-info .console-position .console-time {
129
+ .console-time {
65
130
  margin-right: auto;
66
131
  word-break: break-all;
67
132
  }
68
- .console-item .console-info .console-position .console-other {
133
+ .console-other {
69
134
  word-break: break-all;
70
135
  }
71
- .console-item .console-copy {
136
+
137
+ .console-stack {
138
+ text-decoration: underline;
139
+ cursor: pointer;
140
+ }
141
+
142
+ .console-copy {
72
143
  flex-shrink: 0;
73
144
  margin-left: 16px;
74
145
  display: flex;
@@ -20,6 +20,9 @@
20
20
  v-for="(item, index) in consoleList"
21
21
  :consoleItem="item"
22
22
  :key="index"
23
+ :sourceFileServers="sourceFileServers"
24
+ :mode="mode"
25
+ :useDevSource="useDevSource"
23
26
  />
24
27
  <Empty v-if="!consoleList || consoleList.length === 0" />
25
28
  </view>
@@ -36,6 +39,9 @@ defineProps<{
36
39
  consoleList: DevTool.ConsoleItem[];
37
40
  currentConsoleType: string;
38
41
  modelValue: string;
42
+ sourceFileServers?: string[];
43
+ mode?: string;
44
+ useDevSource?: boolean;
39
45
  }>();
40
46
  const emit = defineEmits<{
41
47
  (e: 'choose', type: ConsoleType): void;
@@ -12,6 +12,9 @@
12
12
  <DevToolWindow
13
13
  :open="devToolWindowVisible"
14
14
  :data="windowData"
15
+ :sourceFileServers="sourceFileServers"
16
+ :mode="mode"
17
+ :useDevSource="useDevSource"
15
18
  @close="onShowDevToolWindow(false)"
16
19
  @sendMessage="onSendMessage"
17
20
  />
@@ -51,6 +54,13 @@ const windowData = ref<DevTool.WindowData>({});
51
54
 
52
55
  const devToolWindowVisible = ref(false);
53
56
  const devToolButtonVisible = ref(false);
57
+
58
+ const sourceFileServers = ref<string[]>([]);
59
+
60
+ const mode = ref<string>();
61
+
62
+ const useDevSource = ref(false);
63
+
54
64
  function onDevToolButtonClick() {
55
65
  onShowDevToolWindow(true);
56
66
  onSendMessage({
@@ -83,6 +93,12 @@ function onDevOptionSend(options: DevTool.DevToolOptions) {
83
93
  buttonProps.buttonFontSize = options.buttonFontSize ?? '16px';
84
94
  buttonProps.buttonBackgroundColor =
85
95
  options.buttonBackgroundColor ?? 'rgba(255, 255, 255, 0)';
96
+
97
+ sourceFileServers.value = options.sourceFileServers ?? [];
98
+
99
+ mode.value = options.mode ?? '';
100
+
101
+ useDevSource.value = options.useDevSource ?? false;
86
102
  }
87
103
  }
88
104
 
@@ -20,6 +20,9 @@
20
20
  <ConsoleList
21
21
  :currentConsoleType="currentConsoleType"
22
22
  :consoleList="consoleList"
23
+ :sourceFileServers="sourceFileServers"
24
+ :mode="mode"
25
+ :useDevSource="useDevSource"
23
26
  v-if="activeTab === 'Console'"
24
27
  v-model="searchConsole"
25
28
  @choose="onConsoleChoose"
@@ -260,7 +263,13 @@ const searchWs = ref('');
260
263
  const searchRoute = ref('');
261
264
  const searchStorage = ref('');
262
265
 
263
- const props = defineProps<{ open?: boolean; data?: DevTool.WindowData }>();
266
+ const props = defineProps<{
267
+ open?: boolean;
268
+ data?: DevTool.WindowData;
269
+ sourceFileServers?: string[];
270
+ mode?: string;
271
+ useDevSource?: boolean;
272
+ }>();
264
273
 
265
274
  const emits = defineEmits<{
266
275
  (e: 'close'): void;
@@ -700,7 +709,7 @@ function onGoTo(page: DevTool.Page) {
700
709
  type: DEV_PAGE_JUMP,
701
710
  data: {
702
711
  path: '/' + page.path,
703
- isNav: page.isNav,
712
+ type: page.type,
704
713
  },
705
714
  });
706
715
  }
@@ -14,7 +14,7 @@
14
14
  <view class="route-item" v-for="item in routeList" :key="item.path">
15
15
  <view class="route-item-name">
16
16
  <view v-html="item.name" />
17
- <Tag v-if="item.isNav" mode="info">Nav</Tag>
17
+ <Tag v-if="item.type" mode="info">{{ item.type }}</Tag>
18
18
  <Tag mode="warn" v-if="item.index === 4 || item.index === 3">
19
19
  当前页
20
20
  </Tag>
package/dev/core.ts CHANGED
@@ -45,7 +45,13 @@ function getDevToolOptions() {
45
45
  }
46
46
 
47
47
  function initDevTool(options?: DevTool.DevToolOptions) {
48
- store.setDevToolOptions(options || {});
48
+ if (!options?.mode) {
49
+ console.error('[DevTool] 请传入 mode: import.meta.env.MODE');
50
+ }
51
+
52
+ store.setDevToolOptions(options || { mode: '' });
53
+
54
+ intercept.interceptVue3(options?.vue3instance);
49
55
  }
50
56
 
51
57
  function showDevToolButton() {
@@ -56,7 +56,6 @@ export class DevConsole {
56
56
  const time = getCurrentDate();
57
57
  // 获取调用栈(从第4行开始,跳过 console.log 和 wrapper 函数)
58
58
  const stackList = new Error()?.stack?.split('\n');
59
-
60
59
  const stack = stackList?.slice(3)?.[0];
61
60
 
62
61
  let argList = args?.map((item) => serializeCircular(item));
@@ -351,7 +351,7 @@ export class DevEvent {
351
351
  }
352
352
  // 切换页面
353
353
  else if (data.type === DEV_PAGE_JUMP) {
354
- data.data?.isNav
354
+ data.data?.type === 'nav'
355
355
  ? this.switchTo(data.data.path ?? '')
356
356
  : this.navigateTo(data.data.path ?? '');
357
357
  }