-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathvite.config.js
More file actions
97 lines (82 loc) · 2.64 KB
/
Copy pathvite.config.js
File metadata and controls
97 lines (82 loc) · 2.64 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
import { defineConfig } from 'vite';
import { resolve } from 'path';
import { fileURLToPath } from 'url';
import * as sass from 'sass';
import postcss from 'postcss';
import autoprefixer from 'autoprefixer';
const scssPath = resolve(process.cwd(), 'src/styles/index.scss');
// Cache SCSS dependencies so they stay watched even after compile errors
const scssFiles = new Set();
export default defineConfig({
root: '.',
cacheDir: '.vite_cache',
build: {
outDir: 'staging/dist',
sourcemap: true,
emptyOutDir: true,
assetsDir: 'images',
rollupOptions: {
input: {
scripts: resolve(process.cwd(), 'src/ts/index.ts'),
},
output: {
entryFileNames: '[name].min.js',
chunkFileNames: '[name].min.js',
assetFileNames: (assetInfo) => {
if (/\.(png|jpe?g|gif|svg|webp)$/i.test(assetInfo.name)) {
return 'images/[name]-[hash][extname]';
}
return '[name]-[hash][extname]';
},
},
},
minify: 'esbuild',
target: 'es2020',
cssCodeSplit: false,
// Polling required for watch mode on network drives (prevents UNKNOWN fs error)
watch: {
usePolling: true,
},
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.scss', '.css'],
},
plugins: [
{
name: 'compile-scss-to-css',
buildStart() {
// Register main SCSS file + all cached dependencies
this.addWatchFile(scssPath);
scssFiles.forEach(file => this.addWatchFile(file));
},
async generateBundle() {
const result = sass.compile(scssPath, {
sourceMap: true,
style: 'compressed',
silenceDeprecations: ['mixed-decls', 'color-functions', 'global-builtin', 'import'],
});
// Cache all imported SCSS files so they stay watched even after compile errors
result.loadedUrls.forEach(url => {
if (url.protocol === 'file:') {
const filePath = fileURLToPath(url);
scssFiles.add(filePath);
this.addWatchFile(filePath);
}
});
const postcssResult = await postcss([autoprefixer()]).process(result.css, {
from: scssPath,
to: 'styles.min.css',
map: {
inline: false,
annotation: true,
prev: result.sourceMap ? JSON.stringify(result.sourceMap) : false,
},
});
this.emitFile({ type: 'asset', fileName: 'styles.min.css', source: postcssResult.css });
if (postcssResult.map) {
this.emitFile({ type: 'asset', fileName: 'styles.min.css.map', source: postcssResult.map.toString() });
}
},
},
],
});