forked from SmythOS/sre
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrollup.config.js
More file actions
350 lines (327 loc) · 12.1 KB
/
rollup.config.js
File metadata and controls
350 lines (327 loc) · 12.1 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
import json from '@rollup/plugin-json';
import path from 'path';
import esbuild from 'rollup-plugin-esbuild';
import sourcemaps from 'rollup-plugin-sourcemaps';
import { terser } from 'rollup-plugin-terser';
import { typescriptPaths } from 'rollup-plugin-typescript-paths';
//import { visualizer } from 'rollup-plugin-visualizer';
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import fs from 'fs';
const isProduction = process.env.BUILD === 'prod';
// Function to automatically mark all non-local imports as external
const isExternal = (id) => {
return !id.startsWith('.') && !path.isAbsolute(id);
};
const config = {
input: {
index: 'src/index.ts',
'commands/agent': 'src/commands/agent/agent.index.ts',
'commands/create': 'src/commands/create/create.index.ts',
'commands/update': 'src/commands/update.ts',
'hooks/preparse': 'src/hooks/preparse.ts',
help: 'src/help.ts',
},
output: {
dir: 'dist',
format: 'es',
sourcemap: true,
banner: '#!/usr/bin/env node',
entryFileNames: '[name].js',
chunkFileNames: 'shared.js',
},
external: isExternal,
plugins: [
colorfulLogs('CLI Builder'),
json(),
typescriptPaths({
tsconfig: './tsconfig.json',
preserveExtensions: true,
nonRelative: false,
}),
sourcemaps(),
esbuild({
sourceMap: true,
minify: isProduction,
treeShaking: isProduction,
sourcesContent: true,
}),
...(isProduction ? [terser()] : []),
],
};
const isCJS = true;
// Zero-dependency standalone bundle configuration (No optimizations)
const zeroDepConfig = {
input: {
index: 'src/index.ts',
'commands/agent': 'src/commands/agent/agent.index.ts',
'commands/create': 'src/commands/create/create.index.ts',
'commands/update': 'src/commands/update.ts',
'hooks/preparse': 'src/hooks/preparse.ts',
},
output: {
dir: 'dist',
format: isCJS ? 'cjs' : 'es',
sourcemap: false, // Enable sourcemaps for debugging
banner: '#!/usr/bin/env node',
entryFileNames: isCJS ? '[name].cjs' : '[name].js',
chunkFileNames: isCJS ? 'chunks/[name].cjs' : 'chunks/[name].js', // Use predictable chunk names
inlineDynamicImports: false, // Keep separate files
exports: 'auto', // Handle mixed exports
// manualChunks: (id) => {
// if (id.includes('node_modules')) {
// return 'vendor';
// }
// },
},
// External function to exclude Node.js built-ins and dev-only packages
external: (id) => {
const builtins = [
'fs',
'fs/promises',
'path',
'path/posix',
'path/win32',
'os',
'util',
'crypto',
'events',
'stream',
'url',
'querystring',
'http',
'https',
'net',
'tls',
'zlib',
'buffer',
'child_process',
'cluster',
'dgram',
'dns',
'domain',
'module',
'readline',
'repl',
'string_decoder',
'timers',
'tty',
'vm',
'worker_threads',
'perf_hooks',
'async_hooks',
'inspector',
'v8',
'constants',
'assert',
'process',
];
// Common dev-only packages that should never be bundled
const devOnlyPackages = [
'typescript',
'esbuild',
'rollup',
'webpack',
'vite',
'vitest',
'jest',
'@types/',
'eslint',
'prettier',
'ts-node',
'nodemon',
'@rollup/',
'@vitejs/',
'@jest/',
'babel',
'typedoc',
'tslint',
'mocha',
'nyc',
'husky',
'lint-staged',
'commitlint',
];
// Exclude Node.js built-ins
if (builtins.includes(id)) return true;
// Exclude dev-only packages (match package name boundaries)
const normalizedId = id.replace(/\\/g, '/'); // Normalize Windows paths
for (const pkg of devOnlyPackages) {
// Match at package name boundaries to avoid false positives
// e.g., "chai" shouldn't match "chain" in a path
if (
normalizedId.includes(`node_modules/${pkg}`) ||
normalizedId.includes(`/${pkg}/`) ||
normalizedId.endsWith(`/${pkg}`) ||
normalizedId === pkg
) {
return true;
}
}
return false;
},
plugins: [
deleteFolder('dist'),
colorfulLogs('CLI Zero-Dep Builder'),
resolve({
browser: false,
preferBuiltins: true,
mainFields: ['module', 'main'],
extensions: ['.js', '.ts', '.json'],
exportConditions: isCJS ? ['node'] : ['node', 'import'],
}),
commonjs({
transformMixedEsModules: true,
ignore: ['electron'],
requireReturnsDefault: 'auto',
ignoreDynamicRequires: isCJS, // Handle dynamic requires properly
dynamicRequireTargets: ['node_modules/**/*.js'],
}),
json(),
typescriptPaths({
tsconfig: './tsconfig.json',
preserveExtensions: true,
nonRelative: false,
}),
esbuild({
sourceMap: false,
minify: true, // No minification
treeShaking: true, // No tree-shaking
target: 'node18',
platform: 'node',
format: isCJS ? undefined : 'esm',
define: {
'process.env.NODE_ENV': '"development"',
global: 'globalThis',
},
keepNames: true, // Keep all names
}),
// Bundle size visualization
// visualizer({
// filename: './bundle-stats.html',
// open: true, // automatically opens in browser
// gzipSize: true,
// brotliSize: true,
// template: 'treemap', // treemap, sunburst, or network
// title: 'CLI Bundle Size Report',
// }),
// No terser plugin - no compression at all
],
};
export default zeroDepConfig;
//#region [Custom Plugins] =====================================================
const colors = {
reset: '\x1b[0m',
bright: '\x1b[1m',
dim: '\x1b[2m',
black: '\x1b[30m',
red: '\x1b[31m',
green: '\x1b[32m',
orange: '\x1b[33m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
magenta: '\x1b[35m',
cyan: '\x1b[36m',
white: '\x1b[37m',
bgBlue: '\x1b[44m',
};
function deleteFolder(folderPath) {
if (fs.existsSync(folderPath)) {
fs.rmSync(folderPath, { recursive: true });
}
}
// Custom colorful logging plugin
function colorfulLogs(title = 'CLI Builder') {
function formatBytes(bytes, decimals = 2) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}
function getProgressBar(percent, length = 20) {
const completeChars = Math.round(percent * length);
const incompleteChars = length - completeChars;
const completeBar = completeChars > 0 ? '█'.repeat(completeChars) : '';
const incompleteBar = incompleteChars > 0 ? '░'.repeat(incompleteChars) : '';
return `${colors.green}${completeBar}${colors.dim}${incompleteBar}${colors.reset}`;
}
const spinnerFrames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
let spinnerIdx = 0;
let spinnerInterval;
let startTime;
let processedFiles = 0;
const totalFiles = new Set();
let currentFile = '';
let hasShownFinalMessage = false;
return {
name: 'colorful-logs',
buildStart() {
startTime = Date.now();
hasShownFinalMessage = false;
console.log(
`\n${colors.bright}${colors.magenta}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${colors.reset}`
);
console.log(`${colors.bright} ${colors.green} ${title}`);
console.log(`\n\n`);
console.log(`${colors.yellow}⚡ ${colors.green}Building ${isProduction ? 'production' : 'development'} CLI bundle...${colors.reset}\n`);
spinnerInterval = setInterval(() => {
const spinner = spinnerFrames[spinnerIdx];
spinnerIdx = (spinnerIdx + 1) % spinnerFrames.length;
if (currentFile && totalFiles.size > 0) {
const percent = processedFiles / totalFiles.size;
const progressBar = getProgressBar(percent);
const percentText = `${Math.round(percent * 100)}%`;
process.stdout.write(
`\r${colors.cyan}${spinner} ${colors.reset}[${progressBar}] ${colors.yellow}${percentText} ${colors.dim}${processedFiles}/${
totalFiles.size
} ${colors.reset}${currentFile.padEnd(50)}`
);
}
}, 80);
},
load(id) {
totalFiles.add(id);
return null;
},
transform(code, id) {
processedFiles++;
if (!id.includes('node_modules')) {
const relativePath = path.relative(process.cwd(), id);
currentFile = relativePath;
}
return null;
},
buildEnd(error) {
if (spinnerInterval) {
clearInterval(spinnerInterval);
}
if (error) {
console.log(`\n${colors.red}✗ ${colors.bright}Build failed with error:${colors.reset}`);
console.log(` ${colors.red}${error.message}${colors.reset}\n`);
}
},
generateBundle(outputOptions, bundle) {
if (spinnerInterval) {
clearInterval(spinnerInterval);
spinnerInterval = null;
}
if (!hasShownFinalMessage) {
const progressBar = getProgressBar(1);
process.stdout.write(
`\r${colors.green}✓ ${colors.reset}[${progressBar}] ${colors.yellow}100% ${colors.dim}${totalFiles.size}/${totalFiles.size} ${
colors.bright
}Complete!${colors.reset}${''.padEnd(50)}\n`
);
const duration = ((Date.now() - startTime) / 1000).toFixed(2);
console.log(`${colors.cyan}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${colors.reset}`);
console.log(`${colors.green}✓ ${colors.bright}CLI Build complete in ${colors.yellow}${duration}s${colors.reset}!`);
console.log(`${colors.magenta}➤ ${colors.white}Processed: ${colors.yellow}${totalFiles.size} files${colors.reset}`);
console.log(`${colors.magenta}➤ ${colors.white}Output: ${colors.yellow}dist/index.js${colors.reset}`);
console.log(`${colors.cyan}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${colors.reset}\n`);
hasShownFinalMessage = true;
}
},
};
}