-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvite.config.ts
265 lines (247 loc) · 6.76 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import {
defineConfig,
normalizePath,
build,
InlineConfig,
UserConfigExport,
Plugin,
} from 'vite';
import { fileURLToPath } from 'url';
import { viteStaticCopy } from 'vite-plugin-static-copy';
import { ViteMinifyPlugin } from 'vite-plugin-minify';
import fs from 'fs';
import legacy from '@vitejs/plugin-legacy'
import nunjucks from 'vite-plugin-nunjucks';
import path, { extname, resolve } from 'path';
/**
* Current file and directory path configuration
*/
const CURRENT_FILENAME = fileURLToPath(import.meta.url);
const CURRENT_DIRNAME = path.dirname(CURRENT_FILENAME);
const SOURCE_ROOT = resolve(CURRENT_DIRNAME, 'src');
/**
* Interface for module copy configuration
*/
interface ModuleCopyConfig {
[key: string]: boolean;
}
/**
* Interface for template variables
*/
interface TemplateVariables {
appName: string;
isDev: boolean;
year: number;
bootstrapClasses: string[];
}
/**
* Retrieves HTML files from the source directory
* @returns {Record<string, string>} Object containing filename-path pairs
* @description Scans the source directory for HTML files and creates a mapping of
* filenames (without extension) to their full file paths
*/
const getHtmlFiles = (): Record<string, string> => {
const htmlFiles: Record<string, string> = {};
fs.readdirSync(SOURCE_ROOT)
.filter((filename) => filename.endsWith('.html'))
.forEach((filename) => {
const baseFilename = filename.slice(0, -5);
htmlFiles[baseFilename] = resolve(SOURCE_ROOT, filename);
});
return htmlFiles;
};
/**
* Prepares template variables for Nunjucks rendering
* @param {string} buildMode - Current build mode ('development' or 'production')
* @returns {Record<string, TemplateVariables>} Variables object for each HTML file
*/
const prepareTemplateVariables = (
buildMode: string,
): Record<string, TemplateVariables> => {
const templateVars: Record<string, TemplateVariables> = {};
const htmlFiles = getHtmlFiles();
Object.keys(htmlFiles).forEach((filename) => {
const templatePath = filename.includes('layouts')
? `layouts/${filename}`
: filename;
templateVars[`${templatePath}.html`] = {
year: new Date().getFullYear(),
appName: 'NeoStrap Dashboard',
isDev: buildMode === 'development',
bootstrapClasses: [
'primary',
'secondary',
'success',
'danger',
'warning',
'info',
'light',
'dark',
'link'
]
};
});
return templateVars;
};
/**
* Configuration for vendor modules to be copied
*/
const VENDOR_MODULES: ModuleCopyConfig = {
apexcharts: true,
'perfect-scrollbar': true,
sweetalert2: true,
'toastify-js': false,
'datatables.net': false,
'datatables.net-bs5': false,
};
/**
* Prepares module copy configurations for the build process
* @returns {Array<{src: string; dest: string; rename: string}>} Array of copy configurations
*/
const prepareModuleCopyConfig = () => {
return Object.entries(VENDOR_MODULES).map(([moduleName, hasDistFolder]) => ({
src: normalizePath(
resolve(
CURRENT_DIRNAME,
`./node_modules/${moduleName}${hasDistFolder ? '/dist' : ''}`,
),
),
dest: 'assets/vendors',
rename: moduleName,
}));
};
/**
* Inline build configuration for application bundling
*/
const INLINE_BUILD_CONFIG: InlineConfig = {
configFile: false,
build: {
emptyOutDir: false,
outDir: resolve(CURRENT_DIRNAME, 'dist/assets/bundled/js'),
lib: {
name: 'app',
formats: ['iife'],
fileName: 'app',
entry: './src/assets/js/neostrap.ts',
},
rollupOptions: {
output: {
entryFileNames: '[name].js',
format: 'iife',
},
},
},
};
build(INLINE_BUILD_CONFIG);
/**
* Main Vite configuration
*/
const config: UserConfigExport = defineConfig((env) => ({
publicDir: 'static',
base: env.mode === 'production' ? './' : '/',
root: SOURCE_ROOT,
server: {
headers: {
'Access-Control-Allow-Origin': '*',
},
cors: true,
},
plugins: [
// legacy({
// targets: ['defaults', 'not IE 11'],
// renderLegacyChunks: true,
// modernPolyfills: true,
// }),
ViteMinifyPlugin({
html5: true,
minifyCSS: true,
minifyJS: {
compress: {
drop_console: true,
drop_debugger: true
}
},
noNewlinesBeforeTagClose: true,
keepClosingSlash: true,
}),
nunjucks({
templatesDir: SOURCE_ROOT,
variables: prepareTemplateVariables(env.mode),
nunjucksEnvironment: {
filters: {
containString: (str: string, searchStr: string): boolean => {
return str.length > 0 && str.includes(searchStr);
},
startsWith: (str: string, prefix: string): boolean => {
return str.length > 0 && str.startsWith(prefix);
},
},
},
}),
viteStaticCopy({
targets: [
{
src: normalizePath(resolve(CURRENT_DIRNAME, './src/assets/static')),
dest: 'assets',
},
{
src: normalizePath(
resolve(
CURRENT_DIRNAME,
'./node_modules/bootstrap-icons/bootstrap-icons.svg',
),
),
dest: 'assets/static/images',
},
...prepareModuleCopyConfig(),
],
watch: {
reloadPageOnChange: true,
},
}),
],
resolve: {
alias: {
'@': normalizePath(resolve(CURRENT_DIRNAME, 'src')),
'~bootstrap': resolve(CURRENT_DIRNAME, 'node_modules/bootstrap'),
'~bootstrap-icons': resolve(CURRENT_DIRNAME, 'node_modules/bootstrap-icons'),
'~perfect-scrollbar': resolve(CURRENT_DIRNAME, 'node_modules/perfect-scrollbar'),
'~@fontsource': resolve(CURRENT_DIRNAME, 'node_modules/@fontsource'),
},
},
build: {
emptyOutDir: true,
manifest: true,
minify: 'esbuild',
targets: 'es2015',
outDir: resolve(CURRENT_DIRNAME, 'dist'),
rollupOptions: {
input: getHtmlFiles(),
output: {
format: 'es',
entryFileNames: 'assets/bundled/js/[name].js',
chunkFileNames: 'assets/bundled/js/[name]-[hash].js',
assetFileNames: (assetInfo) => {
const fileName = assetInfo.name || 'default';
const extension = extname(fileName).slice(1);
let assetFolder = extension ? `${extension}/` : '';
if (['woff', 'woff2', 'ttf'].includes(extension)) {
assetFolder = 'fonts/';
}
return `assets/bundled/${assetFolder}[name][extname]`;
},
manualChunks: {
vendor: [
'bootstrap',
'perfect-scrollbar',
'@fortawesome/fontawesome-free',
'filepond',
'apexcharts',
'chart.js'
]
}
},
},
},
}));
export default config;