-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathvite.config.js
More file actions
93 lines (83 loc) · 2.43 KB
/
vite.config.js
File metadata and controls
93 lines (83 loc) · 2.43 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
import { defineConfig } from 'vite';
import { resolve } from 'path';
import { copyFileSync, mkdirSync, existsSync, readdirSync, statSync } from 'fs';
// Custom plugin to copy assets to the correct structure
function copyAssetsPlugin() {
return {
name: 'copy-assets',
writeBundle() {
const copyDir = (src, dest) => {
if (!existsSync(dest)) {
mkdirSync(dest, { recursive: true });
}
const entries = readdirSync(src);
for (const entry of entries) {
const srcPath = resolve(src, entry);
const destPath = resolve(dest, entry);
if (statSync(srcPath).isDirectory()) {
copyDir(srcPath, destPath);
} else {
copyFileSync(srcPath, destPath);
}
}
};
// Copy assets to dist
try {
copyDir('js/lib', 'dist/lib');
copyDir('lang', 'dist/lang');
copyDir('css', 'dist/css');
copyDir('images', 'dist/images');
copyFileSync('js/notify.js', 'dist/notify.js');
copyFileSync('js/resources.js', 'dist/resources.js');
// Copy unminified version of main.js
copyFileSync('js/main.js', 'dist/js/main.js');
// Move index.html from src/ to root of dist/
if (existsSync('dist/src/index.html')) {
copyFileSync('dist/src/index.html', 'dist/index.html');
}
} catch (error) {
console.warn('Some assets could not be copied:', error.message);
}
}
};
}
export default defineConfig(({ mode }) => {
const isProduction = mode === 'production';
return {
root: '.',
build: {
outDir: 'dist',
emptyOutDir: true,
rollupOptions: {
input: {
main: resolve(__dirname, 'js/main.js'),
index: resolve(__dirname, 'src/index.html')
},
output: {
entryFileNames: (chunkInfo) => {
return chunkInfo.name === 'main' ? 'js/main.min.js' : '[name].js';
},
assetFileNames: 'assets/[name][extname]'
}
},
minify: isProduction ? 'terser' : false,
terserOptions: isProduction ? {
compress: {
drop_console: true,
},
format: {
comments: false,
},
} : {}
},
server: {
port: 3000,
open: '/src/index.html'
},
preview: {
port: 4173,
open: true
},
plugins: [copyAssetsPlugin()]
};
});