File tree Expand file tree Collapse file tree 4 files changed +228
-172
lines changed Expand file tree Collapse file tree 4 files changed +228
-172
lines changed Original file line number Diff line number Diff line change 1+ import path from 'node:path'
2+ import { viteVConsole } from 'vite-plugin-vconsole'
3+
4+ // https://github.com/vadxq/vite-plugin-vconsole/issues/21
5+
6+ export function createViteVConsole ( command : 'serve' | 'build' ) {
7+ return viteVConsole ( {
8+ entry : [ path . resolve ( 'src/main.ts' ) ] ,
9+ enabled : command === 'serve' ,
10+ config : {
11+ maxLogNumber : 1000 ,
12+ theme : 'light' ,
13+ } ,
14+ dynamicConfig : {
15+ theme : `document.documentElement.classList.contains('dark') ? 'dark' : 'light'` ,
16+ } ,
17+ eventListener : `
18+ const targetElement = document.querySelector('html'); // 择要监听的元素
19+ const observerOptions = {
20+ attributes: true, // 监听属性变化
21+ attributeFilter: ['class'] // 只监听class属性变化
22+ };
23+
24+ // 定义回调函数来处理观察到的变化
25+ function handleAttributeChange(mutationsList) {
26+ for(let mutation of mutationsList) {
27+ if (mutation.type === 'attributes' && mutation.attributeName === 'class') {
28+ if (window && window.vConsole) {
29+ window.vConsole.dynamicChange.value = new Date().getTime();
30+ }
31+ }
32+ }
33+ }
34+
35+ // 创建观察者实例并传入回调函数
36+ const observer = new MutationObserver(handleAttributeChange);
37+
38+ // 开始观察目标元素
39+ observer.observe(targetElement, observerOptions);
40+
41+ // 当不再需要观察时,停止观察
42+ // observer.disconnect();
43+ ` ,
44+ } )
45+ }
Original file line number Diff line number Diff line change 6666 "unplugin-vue-router" : " ^0.7.0" ,
6767 "vite" : " ^5.0.12" ,
6868 "vite-plugin-mock-dev-server" : " ^1.4.7" ,
69- "vite-plugin-vconsole" : " ^2.0 .1" ,
69+ "vite-plugin-vconsole" : " ^2.1 .1" ,
7070 "vite-plugin-vue-devtools" : " ^7.0.14" ,
7171 "vitest" : " ^1.2.2" ,
7272 "vue-tsc" : " ^1.8.27"
You can’t perform that action at this time.
0 commit comments