-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvite.config.mts
More file actions
442 lines (396 loc) · 11.9 KB
/
Copy pathvite.config.mts
File metadata and controls
442 lines (396 loc) · 11.9 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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/**
* Vite Configuration for Bernova CSS Framework
*
* Modern build configuration with dual package support (ESM + CJS)
* Optimized for lightweight, versatile distribution
*/
import cssnano from 'cssnano';
import fs from 'fs';
import { glob } from 'glob';
import { fileURLToPath } from 'node:url';
import path from 'path';
import postcss from 'postcss';
import { minify } from 'terser';
import { defineConfig } from 'vite';
import dts from 'vite-plugin-dts';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// Constants
const EMPTY_LENGTH = 0;
const SECOND_INDEX = 1;
/**
* Plugin to copy and minify CSS files to dist/styles
*/
const copyCSSPlugin = (): any => {
const cssProcessor = postcss([
cssnano({
preset: [
'default',
{
discardComments: { removeAll: true },
minifyFontValues: true,
minifyGradients: true,
normalizeWhitespace: true,
reduceTransforms: true,
},
],
}),
]);
return {
name: 'copy-css-assets',
async closeBundle() {
const stylesPattern = path.resolve(__dirname, 'src/styles/**/*.css');
const stylesOut = path.resolve(__dirname, 'dist/styles');
const files = glob.sync(stylesPattern);
if (files.length > EMPTY_LENGTH) {
fs.mkdirSync(stylesOut, { recursive: true });
for (const file of files) {
const relPath = path.relative(
path.resolve(__dirname, 'src/styles'),
file
);
const destPath = path.join(stylesOut, relPath);
const destDir = path.dirname(destPath);
fs.mkdirSync(destDir, { recursive: true });
// Copy original CSS
fs.copyFileSync(file, destPath);
// Create minified version
const minifiedPath = destPath.replace('.css', '.min.css');
try {
const cssContent = fs.readFileSync(file, 'utf-8');
const result = await cssProcessor.process(cssContent, {
from: file,
to: minifiedPath,
});
fs.writeFileSync(minifiedPath, result.css);
} catch (error) {
// eslint-disable-next-line no-console
console.warn(
`[copy-css-assets] ⚠ Error minifying ${relPath}:`,
error
);
fs.copyFileSync(file, minifiedPath);
}
}
// eslint-disable-next-line no-console
console.log(
`[copy-css-assets] ✓ Copied and minified ${files.length} CSS files to ./dist/styles/`
);
}
},
};
};
/**
* Plugin to copy and minify CLI binaries
*/
const copyCLIBinariesPlugin = (): any => {
return {
name: 'copy-cli-binaries',
async closeBundle() {
const binDir = path.resolve(__dirname, 'bin');
const distBinDir = path.resolve(__dirname, 'dist/bin');
if (!fs.existsSync(binDir)) {
return;
}
fs.mkdirSync(distBinDir, { recursive: true });
const binFiles = fs
.readdirSync(binDir)
.filter((file) => file.endsWith('.js'));
for (const file of binFiles) {
const srcPath = path.join(binDir, file);
const destPath = path.join(distBinDir, file);
// Read the source file
let code = fs.readFileSync(srcPath, 'utf-8');
// Keep the shebang if present
const shebangMatch = code.match(/^#!.+\n/);
const shebang = shebangMatch ? shebangMatch[0] : '';
const codeWithoutShebang = shebang ? code.slice(shebang.length) : code;
try {
// Minify the code (excluding shebang)
const minified = await minify(codeWithoutShebang, {
compress: {
dead_code: true,
drop_console: false, // Keep console for CLI
drop_debugger: true,
pure_funcs: [],
},
format: {
comments: false,
},
mangle: {
toplevel: false, // Don't mangle top-level for CLI
},
});
// Combine shebang with minified code
const finalCode = shebang + (minified.code || codeWithoutShebang);
fs.writeFileSync(destPath, finalCode);
} catch (error) {
// eslint-disable-next-line no-console
console.warn(`[copy-cli-binaries] ⚠ Error minifying ${file}:`, error);
fs.copyFileSync(srcPath, destPath);
}
// Make executable
fs.chmodSync(destPath, 0o755);
}
// eslint-disable-next-line no-console
console.log(
`[copy-cli-binaries] ✓ Copied and minified ${binFiles.length} CLI binaries`
);
},
};
};
/**
* Plugin to copy and minify source files (for compatibility)
* Removes all comments including JSDoc for maximum compression
*/
const copySourcePlugin = (): any => {
return {
name: 'copy-source',
async closeBundle() {
const srcDir = path.resolve(__dirname, 'src');
const distSrcDir = path.resolve(__dirname, 'dist/src');
let minifiedCount = 0;
const copyAndMinifyRecursive = async (
src: string,
dest: string
): Promise<void> => {
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest, { recursive: true });
}
for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
const srcPath = path.join(src, entry.name);
const destPath = path.join(dest, entry.name);
if (entry.isDirectory()) {
// Skip test directories
if (
['__tests__', '__mocks__', '__fixtures__'].includes(entry.name)
) {
continue;
}
await copyAndMinifyRecursive(srcPath, destPath);
} else {
// Skip test files and markdown
if (entry.name.endsWith('.test.js') || entry.name.endsWith('.md')) {
continue;
}
// Minify .js files, copy others as-is
if (entry.name.endsWith('.js')) {
try {
const code = fs.readFileSync(srcPath, 'utf-8');
const minified = await minify(code, {
compress: {
dead_code: true,
drop_console: false, // Keep console for library
drop_debugger: true,
passes: 2,
pure_getters: true,
unsafe_arrows: true,
unsafe_comps: true,
},
mangle: false, // Don't mangle for source compatibility
format: {
comments: false, // Remove ALL comments including JSDoc
beautify: false,
},
});
fs.writeFileSync(destPath, minified.code || code);
minifiedCount++;
} catch (error) {
// eslint-disable-next-line no-console
console.warn(
`[copy-source] ⚠ Error minifying ${entry.name}:`,
error
);
fs.copyFileSync(srcPath, destPath);
}
} else {
fs.copyFileSync(srcPath, destPath);
}
}
}
};
await copyAndMinifyRecursive(srcDir, distSrcDir);
// eslint-disable-next-line no-console
console.log(
`[copy-source] ✓ Copied and minified ${minifiedCount} source files (comments removed)`
);
},
};
};
/**
* Plugin to remove test files from distribution
*/
const removeTestFilesPlugin = (): any => {
return {
name: 'remove-test-files',
closeBundle() {
const distDir = path.resolve(__dirname, 'dist');
// Remove test directories
const testDirs = glob.sync(`${distDir}/**/__tests__`, { absolute: true });
testDirs.forEach((dir) => {
if (fs.existsSync(dir)) {
fs.rmSync(dir, { recursive: true, force: true });
}
});
// Remove test files
const testFiles = glob.sync(`${distDir}/**/*.test.{js,d.ts}`, {
absolute: true,
});
testFiles.forEach((file) => {
if (fs.existsSync(file)) {
fs.unlinkSync(file);
}
});
const removed = testDirs.length + testFiles.length;
if (removed > EMPTY_LENGTH) {
// eslint-disable-next-line no-console
console.log(
`[remove-test-files] ✓ Removed ${removed} test files and directories`
);
}
},
};
};
/**
* Vite configuration for building the library
*/
export default defineConfig(({ mode }) => ({
build: {
target: 'node18',
outDir: 'dist',
minify: 'terser',
lib: {
entry: path.resolve(__dirname, 'src/index.js'),
name: 'Bernova',
formats: ['cjs', 'es'],
fileName: (format) => `index.${format === 'es' ? 'mjs' : 'cjs'}`,
},
rollupOptions: {
external: [
'fs',
'path',
'fs/promises',
'url',
'child_process',
'os',
'util',
'crypto',
'stream',
'events',
'postcss',
'autoprefixer',
'cssnano',
'postcss-preset-env',
'postcss-font-magician',
'ora',
'ts-node',
'typescript',
],
output: {
format: 'cjs',
exports: 'named',
preserveModules: false,
generatedCode: {
constBindings: true,
symbols: false,
},
},
// Tree shaking configuration
treeshake: {
moduleSideEffects: false,
propertyReadSideEffects: false,
unknownGlobalSideEffects: false,
},
},
// Terser options for maximum compression
// Eliminates ALL comments including JSDoc for CLI usage
terserOptions: {
compress: {
dead_code: true,
drop_console: false, // Keep console for CLI tools
drop_debugger: true,
keep_fargs: false,
unused: true,
collapse_vars: true,
reduce_vars: true,
inline: 2,
passes: 3, // Multiple passes for better compression
pure_getters: true,
unsafe_arrows: true,
unsafe_comps: true,
unsafe_methods: true,
unsafe_proto: true,
// Additional aggressive optimizations
booleans_as_integers: false,
ecma: 2020,
evaluate: true,
hoist_funs: true,
hoist_props: true,
hoist_vars: false,
join_vars: true,
loops: true,
negate_iife: true,
properties: true,
reduce_funcs: true,
sequences: true,
side_effects: true,
switches: true,
toplevel: true,
typeofs: true,
unsafe: false, // Keep false for CLI reliability
},
mangle: {
toplevel: true,
keep_fnames: false,
reserved: [
'require',
'module',
'exports',
'__dirname',
'__filename',
'process',
'global',
],
},
format: {
comments: false, // Remove ALL comments (JSDoc, inline, etc.)
beautify: false,
semicolons: true,
wrap_iife: false,
wrap_func_args: false,
},
// Remove all annotations and decorators
keep_classnames: false,
keep_fnames: false,
},
// Sourcemap only in development
sourcemap: mode !== 'production',
// Clear output directory
emptyOutDir: true,
// Additional optimizations
reportCompressedSize: true,
chunkSizeWarningLimit: 1000,
},
resolve: {
alias: {
'@': path.resolve(__dirname, 'src'),
},
},
plugins: [
dts({
exclude: [
'src/**/__tests__',
'src/**/*.test.*',
'src/**/__mocks__',
'src/**/__fixtures__',
],
insertTypesEntry: true,
outDir: 'dist/types',
rollupTypes: false,
tsconfigPath: './tsconfig.build.json',
}),
copyCSSPlugin(),
copyCLIBinariesPlugin(),
copySourcePlugin(),
removeTestFilesPlugin(),
],
}));