|
1 | | -import path from 'node:path' |
2 | | -import process from 'node:process' |
3 | | -import { loadEnv } from 'vite' |
4 | | -import { viteVConsole } from 'vite-plugin-vconsole' |
5 | | - |
6 | | -export function createViteVConsole(mode: string) { |
7 | | - const env = loadEnv(mode, process.cwd()) |
8 | | - return viteVConsole({ |
9 | | - entry: [path.resolve('src/main.ts')], |
10 | | - enabled: env.VITE_APP_VCONSOLE === 'true', |
11 | | - config: { |
12 | | - maxLogNumber: 1000, |
13 | | - theme: 'light', |
14 | | - }, |
15 | | - // https://github.com/vadxq/vite-plugin-vconsole/issues/21 |
16 | | - dynamicConfig: { |
17 | | - theme: `document.documentElement.classList.contains('dark') ? 'dark' : 'light'`, |
18 | | - }, |
19 | | - eventListener: ` |
20 | | - const targetElement = document.querySelector('html'); // 择要监听的元素 |
21 | | - const observerOptions = { |
22 | | - attributes: true, // 监听属性变化 |
23 | | - attributeFilter: ['class'] // 只监听class属性变化 |
24 | | - }; |
25 | | -
|
26 | | - // 定义回调函数来处理观察到的变化 |
27 | | - function handleAttributeChange(mutationsList) { |
28 | | - for(let mutation of mutationsList) { |
29 | | - if (mutation.type === 'attributes' && mutation.attributeName === 'class') { |
30 | | - if (window && window.vConsole) { |
31 | | - window.vConsole.dynamicChange.value = new Date().getTime(); |
32 | | - } |
33 | | - } |
34 | | - } |
35 | | - } |
36 | | -
|
37 | | - // 创建观察者实例并传入回调函数 |
38 | | - const observer = new MutationObserver(handleAttributeChange); |
39 | | -
|
40 | | - // 开始观察目标元素 |
41 | | - observer.observe(targetElement, observerOptions); |
42 | | -
|
43 | | - // 当不再需要观察时,停止观察 |
44 | | - // observer.disconnect(); |
45 | | - `, |
46 | | - }) |
47 | | -} |
| 1 | +import path from 'node:path' |
| 2 | +import process from 'node:process' |
| 3 | +import { loadEnv } from 'vite' |
| 4 | +import { viteVConsole } from 'vite-plugin-vconsole' |
| 5 | + |
| 6 | +export function createViteVConsole(mode: string) { |
| 7 | + const env = loadEnv(mode, process.cwd()) |
| 8 | + return viteVConsole({ |
| 9 | + entry: [path.resolve('src/main.ts')], |
| 10 | + enabled: env.VITE_APP_VCONSOLE === 'true', |
| 11 | + config: { |
| 12 | + maxLogNumber: 1000, |
| 13 | + theme: 'light', |
| 14 | + }, |
| 15 | + // https://github.com/vadxq/vite-plugin-vconsole/issues/21 |
| 16 | + dynamicConfig: { |
| 17 | + theme: `document.documentElement.classList.contains('dark') ? 'dark' : 'light'`, |
| 18 | + }, |
| 19 | + eventListener: ` |
| 20 | + const targetElement = document.querySelector('html'); // 择要监听的元素 |
| 21 | + const observerOptions = { |
| 22 | + attributes: true, // 监听属性变化 |
| 23 | + attributeFilter: ['class'] // 只监听class属性变化 |
| 24 | + }; |
| 25 | +
|
| 26 | + // 定义回调函数来处理观察到的变化 |
| 27 | + function handleAttributeChange(mutationsList) { |
| 28 | + for(let mutation of mutationsList) { |
| 29 | + if (mutation.type === 'attributes' && mutation.attributeName === 'class') { |
| 30 | + if (window && window.vConsole) { |
| 31 | + window.vConsole.dynamicChange.value = new Date().getTime(); |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | +
|
| 37 | + // 创建观察者实例并传入回调函数 |
| 38 | + const observer = new MutationObserver(handleAttributeChange); |
| 39 | +
|
| 40 | + // 开始观察目标元素 |
| 41 | + observer.observe(targetElement, observerOptions); |
| 42 | +
|
| 43 | + // 当不再需要观察时,停止观察 |
| 44 | + // observer.disconnect(); |
| 45 | + `, |
| 46 | + }) |
| 47 | +} |
0 commit comments