-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvite.config.ts
80 lines (71 loc) · 2.21 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
import { defineConfig } from 'vitest/config';
import { enhancedImages } from '@sveltejs/enhanced-img';
import { sveltekit } from '@sveltejs/kit/vite';
import Icons from 'unplugin-icons/vite';
import type { PluginOption, ModuleNode } from 'vite';
import { svelteTesting } from '@testing-library/svelte/vite';
// Custom plugin for HMR optimization
// This is my first plugin, so I'm not sure if it's the best way to do this
// You can always disable it by removing the plugin from the plugins array
const asyncComponentHMR = (): PluginOption => ({
name: 'async-component-hmr',
enforce: 'post',
handleHotUpdate({ file, server, modules }) {
// Only handle Svelte components in the components directory
if (file.endsWith('.svelte') && file.includes('/components/')) {
// Get all importers of this module
const updatedComponent = modules.find((m) => m.file === file);
if (!updatedComponent) return;
const importers = Array.from(updatedComponent.importers);
// Check if this component is loaded through async-component-loader
const isAsyncLoaded = importers.some((imp: ModuleNode) => {
const importerFile = imp.file || '';
return (
importerFile.includes('async-component-loader.svelte') ||
Array.from(imp.importers).some((m) => m.file?.includes('async-component-loader.svelte'))
);
});
// Only handle HMR for async-loaded components
if (isAsyncLoaded) {
return [updatedComponent];
}
}
}
});
export default defineConfig({
plugins: [
enhancedImages(),
sveltekit(),
svelteTesting(),
Icons({
compiler: 'svelte'
}),
asyncComponentHMR()
],
build: {
rollupOptions: {
onwarn(warning, warn) {
if (
warning.code === 'PLUGIN_WARNING' &&
warning.message.includes('/ui/button.svelte is dynamically imported')
) {
warn(
'Here would be a warning that "button.svelte" is dynamically imported. It is a Core component tho and should be imported statically.'
);
return;
}
warn(warning);
}
}
},
test: {
include: ['src/**/*.{test,test.svelte,spec}.{js,ts}'],
environment: 'jsdom',
includeSource: ['src/**/*.{js,ts,svelte}'],
setupFiles: ['./setupTest.ts'],
globals: true,
coverage: {
exclude: ['./setupTest.ts']
}
}
});