-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.js
96 lines (90 loc) · 3.04 KB
/
vite.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import path from 'path';
import { fileURLToPath } from 'url';
import fs from 'fs/promises';
import tailwindcss from 'tailwindcss';
import autoprefixer from 'autoprefixer';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const loadUserConfig = async () => {
const configPath = process.env.CONFIG_PATH || path.resolve(process.cwd(), 'navpress.config.js');
let userConfig = {};
try {
await fs.access(configPath);
userConfig = await import(configPath + `?timestamp=${new Date().getTime()}`).then(module => module.default);
} catch (error) {
console.error(`Failed to load config from ${configPath}`, error);
}
return userConfig;
};
export default defineConfig(async () => {
const outputDir = process.env.OUTPUT_DIR || path.resolve(__dirname, 'dist');
let userConfig = await loadUserConfig();
const indexHtmlPath = path.resolve(__dirname, 'index.html');
const basePath = userConfig.base || '/';
return {
base: basePath,
plugins: [
vue(),
{
name: 'html-transform',
transformIndexHtml(html) {
// 动态替换 title 和 meta 标签内容
return html
.replace(/<title>.*<\/title>/, `<title>${userConfig.meta?.title || userConfig.title}</title>`)
.replace(/<meta name="description" content=".*">/, `<meta name="description" content="${userConfig.meta?.description || ''}">`)
.replace(/<meta name="keywords" content=".*">/, `<meta name="keywords" content="${userConfig.meta?.keywords || ''}">`)
.replace(/<meta name="author" content=".*">/, `<meta name="author" content="${userConfig.meta?.author || ''}">`);
},
},
{
name: 'watch-external',
configureServer(server) {
const configPath = process.env.CONFIG_PATH || path.resolve(process.cwd(), 'navpress.config.js');
server.watcher.add(configPath);
server.watcher.on('change', async (file) => {
if (file === configPath) {
userConfig = await loadUserConfig();
// 更新 define 配置中的 __USER_CONFIG__
server.config.define['__USER_CONFIG__'] = JSON.stringify(userConfig);
server.ws.send({
type: 'custom',
event: 'config-updated',
data: userConfig,
});
console.log(`Config file changed: ${configPath}. Reload`);
}
});
}
}
],
css: {
postcss: {
plugins: [
tailwindcss({
config: path.resolve(__dirname, 'tailwind.config.js'),
}),
autoprefixer,
],
},
},
define: {
__USER_CONFIG__: JSON.stringify(userConfig),
},
root: path.resolve(__dirname),
build: {
outDir: outputDir,
rollupOptions: {
input: indexHtmlPath,
},
},
server: {
open: true,
watch: {
usePolling: true,
interval: 300,
},
},
};
});