Repository navigation
Expand file tree
/
Copy pathvite.config.js
More file actions
141 lines (135 loc) · 4.16 KB
/
Copy pathvite.config.js
File metadata and controls
141 lines (135 loc) · 4.16 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { defineConfig, loadEnv } from 'vite';
import react from '@vitejs/plugin-react';
import path, { resolve } from 'path';
import { fileURLToPath } from 'url';
import fs from 'fs';
import process from 'process';
import eslint from 'vite-plugin-eslint';
import viteCustomPluginImport from './vite-custom-plugin-import';
const VITE_BADGE = '\x1b[33m[vite]\x1b[0m';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const settingPath = path.resolve(__dirname, './src/setting.js');
const getTimestamp = () => {
return new Date().toLocaleTimeString('en-US', {
hour: 'numeric',
minute: '2-digit',
second: '2-digit',
hour12: true
});
};
const loadLocalSettings = async () => {
try {
await fs.promises.access(settingPath);
} catch (e) {
console.log(`${getTimestamp()} \x1b[33m[vite]\x1b[0m No local settings file is applied.`);
return null;
}
try {
const configModule = await import(`${settingPath}?t=${Date.now()}`);
return Object.values(configModule)[0];
} catch (e) {
console.log(`${getTimestamp()} ${VITE_BADGE} Load local settings file failed.`);
return null;
}
};
// https://vite.dev/config/
export default defineConfig(async ({ mode }) => {
const isDev = mode === 'development';
const env = loadEnv(mode, process.cwd(), '');
Object.assign(process.env, env);
return {
base: './',
root: './src',
publicDir: resolve(__dirname, 'public'),
build: {
outDir: resolve(__dirname, 'dist'),
emptyOutDir: true,
rollupOptions: {
output: {
entryFileNames: '[name]-[hash].js',
chunkFileNames: '[name]-[hash].js',
assetFileNames: (assetInfo) => {
if (assetInfo.names?.[0]?.endsWith('.css')) {
return '[name]-[hash][extname]';
}
return 'assets/[name]-[hash][extname]';
},
// split vendor chunks if has chunk size warning limit
// manualChunks(id) {
// if (id.includes('node_modules')) {
// if (id.includes('react') || id.includes('react-dom')) return 'react';
// return null;
// }
// }
},
},
assetsInlineLimit: 4 * 1024, // default is 4KB
chunkSizeWarningLimit: 1000,
},
plugins: [
isDev && {
name: 'inject-html',
configureServer(server) {
server.watcher.add(settingPath);
},
handleHotUpdate({ file, server }) {
if (file === settingPath) {
console.log(`${getTimestamp()} ${VITE_BADGE} setting.js changed, reloading page...`);
server.ws.send({
type: 'full-reload',
path: '*'
});
return [];
}
},
async transformIndexHtml(html) {
const devConfigs = await loadLocalSettings();
return {
html,
tags: [
devConfigs && {
// for development: inject local settings
tag: 'script',
attrs: { type: 'text/javascript' },
children: `window.__HTML_PAGE_DEV_CONFIG__ = ${JSON.stringify(devConfigs)}`,
injectTo: 'head',
}
].filter(Boolean),
};
},
},
react({}),
viteCustomPluginImport({
libraryName: 'dtable-ui-component',
libraryDirectory: 'lib',
camel2DashComponentName: false,
camel2UnderlineComponentName: false,
}),
eslint({
cache: false,
lintOnStart: true,
emitWarning: true,
emitError: true,
failOnError: false,
include: ['src/**/*.{js,ts,vue,jsx,tsx}'],
}),
].filter(Boolean),
server: {
port: parseInt(process.env.VITE_DEV_PORT),
proxy: {
[process.env.PROXY_PATH]: {
target: process.env.PROXY_TARGET,
changeOrigin: true,
},
},
},
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
'@components': path.resolve(__dirname, './src/components'),
'@constants': path.resolve(__dirname, './src/constants'),
'@utils': path.resolve(__dirname, './src/utils'),
},
},
};
});