-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.js
More file actions
76 lines (63 loc) · 1.7 KB
/
Copy pathvite.config.js
File metadata and controls
76 lines (63 loc) · 1.7 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
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath, URL } from 'node:url';
import { defineConfig, normalizePath } from 'vite';
import postcssNesting from 'postcss-nested';
import postcssCustomMedia from 'postcss-custom-media';
import { minify } from 'html-minifier-terser';
import { viteStaticCopy } from 'vite-plugin-static-copy';
const minifier = () => ({
name: 'minifier',
apply: 'build',
enforce: 'post',
transformIndexHtml: (html) => {
return minify(html, {
collapseWhitespace: true,
minifyCSS: true,
minifyJS: true,
minifyURLs: true,
removeComments: true,
});
},
});
const createTargets = () => {
const targets = [];
const dir = normalizePath(path.resolve(__dirname, 'data', 'projects'));
fs.readdirSync(dir).forEach((item) => {
const folder = path.resolve(dir, item);
const files = fs.readdirSync(folder);
const names = ['image', 'thumb']
.map((name) => files.find((file) => file.startsWith(name)))
.filter((name) => !!name);
const srcs = names.map((name) => normalizePath(path.resolve(folder, name)));
const dest = normalizePath(path.resolve(__dirname, 'dist', 'assets', item));
for (const src of srcs) {
targets.push({
src,
dest,
});
}
});
return targets;
};
export default defineConfig((mode) => {
const plugins = [minifier()];
if (mode.command === 'build') {
plugins.push(viteStaticCopy({ targets: createTargets() }));
}
return {
css: {
postcss: {
plugins: [postcssCustomMedia(), postcssNesting()],
},
},
publicDir: false,
plugins,
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
'@data': fileURLToPath(new URL('./data', import.meta.url)),
},
},
};
});