Skip to content

Commit 1dd86e5

Browse files
committed
chore: Upgrade vConsole for dynamic themes (#51)
1 parent bccd0b5 commit 1dd86e5

File tree

4 files changed

+228
-172
lines changed

4 files changed

+228
-172
lines changed

build/vite/index.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
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"

0 commit comments

Comments
 (0)