Repository navigation
Expand file tree
/
Copy pathvite.config.ts
More file actions
152 lines (150 loc) · 4.72 KB
/
Copy pathvite.config.ts
File metadata and controls
152 lines (150 loc) · 4.72 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
import { resolve } from 'path';
import type { RolldownLog } from 'rolldown';
import { defineConfig, type ConfigEnv, type ESBuildOptions } from 'vite';
import pkgjsn from './package.json' with { type: 'json' };
export default defineConfig(({ mode }: ConfigEnv) => {
const isDev = mode === 'development';
return {
build: {
rollupOptions: {
input: resolve(import.meta.dirname, 'src/index.ts'),
output: {
entryFileNames: `${pkgjsn.name}${isDev ? '.dev' : ''}.user.js`,
dir: resolve(import.meta.dirname, 'dist'),
manualChunks: undefined,
// Optimize output format
format: 'iife' as const,
// Remove unnecessary comments in production
banner: isDev ? undefined : '',
footer: isDev ? undefined : '',
},
// Prevent any external dependencies
external: [],
// Tree-shaking optimizations
treeshake: {
moduleSideEffects: false,
propertyReadSideEffects: false as const,
unknownGlobalSideEffects: false,
},
onwarn(warning: RolldownLog, warn: (warning: RolldownLog) => void) {
// Suppress import.meta warnings for IIFE output (UserScript)
if (warning.code === 'EMPTY_IMPORT_META') return;
warn(warning);
},
},
sourcemap: isDev ? ('inline' as const) : false,
minify: isDev ? false : ('terser' as const),
// Enhanced Terser options for maximum compression
terserOptions: isDev
? undefined
: {
compress: {
drop_console: false, // Keep console for UserScript debugging
drop_debugger: true,
pure_funcs: ['console.debug'],
passes: 2,
unsafe: true,
unsafe_arrows: true,
unsafe_comps: true,
unsafe_math: true,
unsafe_methods: true,
unsafe_proto: true,
unsafe_regexp: true,
unsafe_undefined: true,
hoist_funs: true,
hoist_props: true,
hoist_vars: false,
if_return: true,
join_vars: true,
sequences: true,
side_effects: true,
switches: true,
typeofs: true,
booleans: true,
collapse_vars: true,
comparisons: true,
computed_props: true,
conditionals: true,
dead_code: true,
directives: true,
evaluate: true,
expression: false,
global_defs: {},
keep_fargs: false,
keep_infinity: false,
loops: true,
negate_iife: true,
properties: true,
reduce_funcs: true,
reduce_vars: true,
toplevel: true,
unused: true,
},
mangle: {
toplevel: true,
safari10: false,
properties: {
regex: /^_/,
},
},
format: {
comments: false,
beautify: false,
},
ecma: 2025 as const,
toplevel: true,
safari10: false,
ie8: false,
},
// Ensure all assets are inlined
assetsInlineLimit: Number.MAX_SAFE_INTEGER,
// Disable CSS code splitting
cssCodeSplit: false,
// Keep syntax modern enough for selective bQuery integration
target: 'esnext',
// Disable module preload for UserScript IIFE output
modulePreload: false,
// Report compressed file sizes
reportCompressedSize: true,
// Chunk size warnings
chunkSizeWarningLimit: 500,
},
plugins: [],
resolve: {
tsconfigPaths: true,
extensions: ['.tsx', '.ts', '.js'],
alias: {
'@': resolve(import.meta.dirname, 'src'),
},
},
define: {
__DEV__: isDev,
__VERSION__: JSON.stringify(pkgjsn.version),
// Production optimizations
'process.env.NODE_ENV': JSON.stringify(isDev ? 'development' : 'production'),
// Remove debug code in production
__DEBUG__: isDev,
// UserScript environment flags
__USERSCRIPT__: true,
__BUILD_TIME__: JSON.stringify(new Date().toISOString()),
},
// Optimize dependencies
optimizeDeps: {
include: [],
exclude: [],
},
// Enable esbuild optimizations
esbuild: {
target: 'esnext',
legalComments: 'none',
...(isDev
? {}
: {
drop: ['debugger'],
minifyIdentifiers: true,
minifySyntax: true,
minifyWhitespace: true,
}),
} as ESBuildOptions,
};
});