-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.js
116 lines (105 loc) · 3.26 KB
/
vite.config.js
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
// vite.config.js
import { defineConfig } from 'vite'
import fs from 'fs'
import lessPluginGlob from 'less-plugin-glob'
import path from 'path'
import VitePluginBrowserSync from 'vite-plugin-browser-sync'
// this is walking through all the directories in the /components folder
// and creating an entry for each *.library.js file it finds. These are
// then used as the entry points for the build, and each is exported to
// the dist folder with the same name as the directory it was found in.
// SOMETIMES vite will decide to add an extra dist folder for imported
// modules that are dynamically imported at runtime. Always add the
// entire contents of the dist folder to the repository.
function getEntries(dir, parent = '') {
let entries = {}
const files = fs.readdirSync(dir)
for (const file of files) {
const fullPath = path.join(dir, file)
const stat = fs.statSync(fullPath)
if (stat.isDirectory()) {
const newParent = parent ? `${parent}/${file}` : file
entries = { ...entries, ...getEntries(fullPath, newParent) }
} else if (file.endsWith('.library.js')) {
const name = `${path.basename(file, '.library.js')}`
entries[name] = fullPath
}
}
return entries
}
const componentsDir = path.resolve(__dirname, 'components')
const entries = getEntries(componentsDir)
// get a list of the folders in the /less/ directory and set up
// an @import statement for each one using the glob plugin to
// import all .less files in each folder
function getLessImports() {
const lessDir = path.resolve(__dirname, 'less')
const imports = fs
.readdirSync(lessDir)
.filter((file) => fs.statSync(path.join(lessDir, file)).isDirectory())
.map((dir) => `@import (reference) './less/${dir}/*.less';`)
return imports.join('\n')
}
export default defineConfig({
base: './',
build: {
cssCodeSplit: true,
cssMinify: true,
minify: true,
publicPath: '',
reportCompressedSize: true,
rollupOptions: {
input: entries,
output: {
dir: 'dist',
entryFileNames: '[name]/[name].js',
chunkFileNames: '[name]/[name].js',
assetFileNames: (assetInfo) => {
// set an output path for each asset depending on the file extension
const extension = path.extname(assetInfo.name).slice(1)
let group = '[name]'
switch (extension) {
case 'woff':
case 'woff2':
group = 'webfonts'
break
case 'jpg':
case 'gif':
case 'png':
case 'svg':
group = 'images'
break
}
return `${group}/[name].[ext]`
}
}
}
},
css: {
preprocessorOptions: {
less: {
additionalData: getLessImports(),
math: 'strict',
plugins: [lessPluginGlob]
}
}
},
plugins: [
VitePluginBrowserSync({
dev: { enable: false },
preview: { enable: false },
buildWatch: {
enable: true,
bs: {
host: 'localhost',
proxy: 'https://CHANGME.lndo.site',
files: ['./dist', './templates'],
watchEvents: ['add', 'change', 'unlink', 'addDir', 'unlinkDir'],
ghostMode: false,
ui: false,
open: true
}
}
})
]
})