wujingjing
2024-08-29 19b778d2d04bed31ce2e1f167c6ff2fda9906421
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const path = require('path');
 
const { replaceFileContent, rootDir, logSuccess, exit, logError, checkFileExist } = require('../helper');
 
const viteConfigPath = path.join(rootDir, 'vite.config.ts');
checkFileExist(viteConfigPath);
replaceFileContent(viteConfigPath, (data) => {
    const pattern = /hmr\s*:\s*(true|false)\s*/;
    const isHmrStr = data.match(pattern)?.[1];
    if (!isHmrStr) {
        logError('');
        exit();
    }
    const newData = data.replace(pattern, `hmr: ${isHmrStr === 'true' ? 'false' : 'true'}`);
 
    if (isHmrStr === 'true') {
        logSuccess('已关闭 hmr');
    } else {
        logSuccess('已开启 hmr');
    }
 
    return newData;
});