Repository navigation
Expand file tree
/
Copy pathvite.config.js
More file actions
93 lines (80 loc) · 2.48 KB
/
Copy pathvite.config.js
File metadata and controls
93 lines (80 loc) · 2.48 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 { fileURLToPath } from 'url';
import * as sass from 'sass';
import autoprefixer from 'autoprefixer';
import postcss from 'postcss';
const style = process.env.STYLE || 'bs5';
const scssPath = resolve(process.cwd(), `${style}/styles/${style}.scss`);
const scssFiles = new Set();
export default defineConfig({
root: '.',
cacheDir: '.vite_cache',
build: {
outDir: `${style}/dist`,
sourcemap: true,
emptyOutDir: false,
assetsDir: 'images',
rollupOptions: {
input: {
scripts: resolve(process.cwd(), 'src/ts/index.ts'),
},
output: {
entryFileNames: '[name].min.js',
chunkFileNames: '[name].min.js',
assetFileNames: (assetInfo) => {
if (assetInfo.name?.endsWith('.css')) {
const name = assetInfo.name.includes('entry') ? 'styles' : assetInfo.name.replace('.css', '');
return `${name}.min.css`;
}
return 'images/[name].[hash][extname]';
},
},
},
minify: 'esbuild',
target: 'es2020',
cssCodeSplit: false,
watch: {
usePolling: true,
},
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.scss', '.css'],
},
plugins: [
{
name: 'compile-scss-to-css',
buildStart() {
this.addWatchFile(scssPath);
scssFiles.forEach(file => this.addWatchFile(file));
},
async generateBundle(options, bundle) {
const result = sass.compile(scssPath, {
sourceMap: true,
style: 'compressed',
silenceDeprecations: ['if-function', 'color-functions', 'global-builtin', 'import'],
});
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() });
}
},
}
],
});