/
vite.config.ts
238 lines (229 loc) · 5.95 KB
/
vite.config.ts
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
import vue from "@vitejs/plugin-vue"
// @ts-ignore
import path from "path"
// @ts-ignore
import { ComponentResolver } from "unplugin-vue-components"
import Components from "unplugin-vue-components/vite"
import { defineConfig, PluginOption } from "vite"
// import visualizer from "rollup-plugin-visualizer"
import { writeFile } from "fs/promises"
import * as zlib from "zlib"
import { OutputAsset, OutputChunk } from "rollup"
import { configDefaults } from "vitest/config"
// https://vitejs.dev/config/
// noinspection SpellCheckingInspection,TypeScriptUnresolvedVariable
export default defineConfig({
test: {
root: "dashboard/new-dashboard",
include: [...configDefaults.include, "**/*.{test,spec}.ts"],
globals: true,
environment: "jsdom",
testTimeout: 10000,
},
plugins: [
vue(),
// visualizer({template: "sunburst"}),
Components({
directoryAsNamespace: true,
dts: path.resolve(__dirname, "dashboard/new-dashboard/src/components.d.ts"),
resolvers: [
PrimeVueResolver(),
// HeadlessUiResolver(),
(name) => {
// @ts-ignore
const kind = process.env.NODE_ENV === "test" ? "" : "esm/"
if (name.endsWith("Icon")) {
return {
path: `@heroicons/vue/24/outline/${kind}${name}.js`,
}
} else if (name.endsWith("IconSolid")) {
return {
path: `@heroicons/vue/20/solid/${kind}${name.substring(0, name.length - "Solid".length)}.js`,
}
} else {
return null
}
},
],
}),
brotli(),
],
root: "dashboard/app",
publicDir: path.resolve(__dirname, "dashboard/app/public"),
server: {
host: "localhost",
port: 8080,
},
build: {
// sourcemap: true,
reportCompressedSize: false,
emptyOutDir: true,
chunkSizeWarningLimit: 600,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
outDir: path.resolve(__dirname, "cmd/frontend/resources"),
cssMinify: "lightningcss",
},
css: {
preprocessorMaxWorkers: true,
},
})
function brotli(): PluginOption {
return {
name: "offline-compression",
writeBundle(outputOptions, bundle) {
const outDir = outputOptions.dir!
return Promise.all(Object.values(bundle).map((it) => brotliCompressFile(it, outDir))) as Promise<never>
},
apply: "build",
}
}
async function brotliCompressFile(asset: OutputAsset | OutputChunk, outDir: string): Promise<void> {
const file = path.join(outDir, asset.fileName)
// woff2 is based on the Brotli compression algorithm - no need to compress
if (file.endsWith(".png") || file.endsWith(".woff2")) {
return
}
const data = Buffer.from("code" in asset ? asset.code : (asset as OutputAsset).source)
// https://github.com/google/ngx_brotli#brotli_min_length default is 20, so, we will compress any asset regardless of size
if (data.length < 20) {
throw new Error("Asset size is suspiciously small")
}
const mode = file.endsWith(".wasm") ? zlib.constants.BROTLI_MODE_GENERIC : zlib.constants.BROTLI_MODE_TEXT
await new Promise((resolve, reject) => {
zlib.brotliCompress(
data,
{
params: {
[zlib.constants.BROTLI_PARAM_MODE]: mode,
[zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY,
},
},
(error, buffer) => {
if (error != null) {
reject(error)
return
}
writeFile(`${file}.br`, buffer).then(resolve, reject)
}
)
})
}
// noinspection SpellCheckingInspection
const components = new Set<string>([
"Accordion",
"AccordionTab",
"AutoComplete",
"Avatar",
"AvatarGroup",
"Badge",
// "BlockUI",
"Breadcrumb",
"Button",
"Calendar",
"Card",
// "Carousel",
// "CascadeSelect",
"Chart",
"Checkbox",
"Chip",
"Chips",
"ColorPicker",
"Column",
"ColumnGroup",
// 'ConfirmDialog',
// 'ConfirmPopup',
// These must be registered globally in order for the confirm service to work properly
// "ContextMenu",
"DataTable",
"DataView",
"DataViewLayoutOptions",
"DeferredContent",
"Dialog",
"Dock",
"Dropdown",
// "Editor",
// "Fieldset",
"FileUpload",
"FloatLabel",
// "FullCalendar",
// "Galleria",
// "Image",
// "InlineMessage",
"Inplace",
"InputMask",
"InputNumber",
"InputSwitch",
"InputText",
"Knob",
"Listbox",
"MegaMenu",
"Menu",
"Message",
"MultiSelect",
"OrderList",
"OrganizationChart",
"OverlayPanel",
"Paginator",
"Panel",
"PanelMenu",
"Password",
"PickList",
"ProgressBar",
"ProgressSpinner",
"RadioButton",
"Rating",
// "ScrollPanel",
"ScrollTop",
"SelectButton",
"Sidebar",
"Skeleton",
"Slider",
"SpeedDial",
"SplitButton",
"Splitter",
"SplitterPanel",
"Steps",
"TabPanel",
"TabView",
"Tag",
"Terminal",
"TerminalService",
"Textarea",
"TieredMenu",
"Timeline",
"Timelist",
// 'Toast',
// Toast must be registered globally in order for the toast service to work properly
"ToggleButton",
// 'Tooltip',
// Tooltip must be registered globally in order for the tooltip service to work properly
"TieredMenu",
"Tree",
"TreeSelect",
"TreeTable",
"TriStateCheckbox",
"VirtualScroller",
])
const componentWithStyles = new Set<string>(["Toolbar", "Menubar", "Button", "TabMenu"])
function PrimeVueResolver(): ComponentResolver {
const styleDir = path.join(__dirname, "dashboard/new-dashboard/src/primevue-theme")
return {
type: "component",
resolve(name: string) {
if (componentWithStyles.has(name)) {
return {
from: `primevue/${name.toLowerCase()}/${name}.vue`,
sideEffects: [path.join(styleDir, `${name.toLowerCase()}.css`).replaceAll("\\", "/")],
}
} else if (components.has(name)) {
return {
from: `primevue/${name.toLowerCase()}/${name}.vue`,
}
} else {
return null
}
},
}
}