-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathvite.config.ts
More file actions
187 lines (173 loc) · 5.31 KB
/
vite.config.ts
File metadata and controls
187 lines (173 loc) · 5.31 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
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
import { globby } from 'globby';
import { resolve } from 'path';
import { minify } from 'terser';
import { defineConfig } from 'vite';
import dts from 'vite-plugin-dts';
import { hmrPlugin, presets } from 'vite-plugin-web-components-hmr';
// Custom plugin to minify the ES build
const full_reload_plugin = {
name: 'full-reload',
handleHotUpdate({ server }) {
server.ws.send({ type: 'full-reload' });
return [];
}
};
function minifyBundles() {
return {
name: 'minifyBundles',
async generateBundle(options, bundle) {
for (let key in bundle) {
if (bundle[key].type == 'chunk' && key.endsWith('.js')) {
const minifyCode = await minify(bundle[key].code, {
sourceMap: false,
format: {
comments: false
},
compress: {
passes: 2
},
mangle: {
toplevel: true,
module: true
}
});
bundle[key].code = minifyCode.code;
}
}
return bundle;
}
};
}
async function getLibraryEntryPoints() {
const files = await globby(resolve(__dirname, 'src/components/**/*.ts'));
files.sort();
return files.filter(file => !file.includes('app'));
}
export default defineConfig(async ({ command, mode }) => {
if (command === 'serve') {
// Development
return {
plugins: [
hmrPlugin({
include: ['./src/**/*.ts'],
presets: [presets.lit]
})
// full_reload_plugin
],
server: {}
};
} else if (command === 'build') {
switch (mode) {
case 'production': {
// Production: standard web page (default mode)
return {
build: {
outDir: 'dist',
rollupOptions: {
input: {
main: resolve(__dirname, 'index.html')
}
}
}
};
}
case 'github': {
// Production: github page (default mode)
return {
base: '/euphony/',
build: {
outDir: 'dist',
rollupOptions: {
input: {
main: resolve(__dirname, 'index.html')
}
}
},
plugins: []
};
}
case 'library': {
// Production: library that can be imported in other apps
return {
publicDir: false,
build: {
lib: {
// Could also be a dictionary or array of multiple entry points
entry: [
...(await getLibraryEntryPoints()),
resolve(__dirname, 'src/euphony.ts')
],
name: 'euphony',
formats: ['es']
},
outDir: 'lib',
rollupOptions: {
external: [],
output: {
generatedCode: {
constBindings: true
},
globals: {},
chunkFileNames: `chunks/[name].js`,
entryFileNames(chunkInfo) {
if (chunkInfo.facadeModuleId.includes('src/components/')) {
// Extract the folder name after 'src/components/' and use it for the output name
const folderName = chunkInfo.facadeModuleId
.split('src/components/')[1]
.split('/')[0];
return `components/${folderName}/[name].js`;
}
return `[name].js`;
},
manualChunks(id) {
// Check if the file path includes "@shoelace-style" to group into a single Shoelace chunk
if (id.includes('@shoelace-style')) {
return 'shoelace';
}
// CSS inline
if (id.includes('.css?inline')) {
if (id.includes('src/components/')) {
// Extract the folder name after 'src/components/' and use it for the output name
const folderName = id
.split('src/components/')[1]
.split('/')[0];
return `css/${folderName}`;
}
return 'css-inline';
}
// Larger third party libraries
if (id.includes('prismjs')) {
return 'prismjs';
}
if (id.includes('prismjs')) {
return 'prismjs';
}
if (id.includes('dompurify')) {
return 'dompurify';
}
if (id.includes('marked')) {
return 'marked';
}
// All other third party libraries
if (id.includes('node_modules')) {
// return id
// .toString()
// .split('node_modules/')[1]
// .split('/')[1]
// .toString();
return 'third-party';
}
}
}
}
},
plugins: [dts(), minifyBundles()]
};
}
default: {
console.error(`Error: unknown production mode ${mode}`);
return null;
}
}
}
});