-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
548 lines (445 loc) Β· 19.2 KB
/
build.js
File metadata and controls
548 lines (445 loc) Β· 19.2 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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
const { minify } = require('terser');
const fs = require('fs');
const path = require('path');
const outputDir = './Dist/';
// Parse command line arguments
const args = process.argv.slice(2);
const versionIndex = args.indexOf('--as-version');
const version = versionIndex !== -1 && args[versionIndex + 1] ? args[versionIndex + 1] : null;
const hasFullBuild = args.includes('--full-build') || args.includes('-fb');
const hasGithub = args.includes('--github');
const dramaticBuild = args.includes('--dramatic-build') || args.includes('-db');
// Entry point file - specify your main file here
const entryPoint = path.resolve('./DynamicGrid/DynamicGrid.js');
// Base directories to search for files
const searchDirectories = [
'./DynamicGrid',
'./DynamicGrid/exportConnectors',
'./DynamicGrid/typePlugins',
'./DynamicGrid/libs'
];
// File extensions to consider
const fileExtensions = ['.js'];
const config = {
compress: {
dead_code: true,
drop_console: ['log', 'info'],
drop_debugger: true,
keep_classnames: false,
keep_fargs: true,
keep_fnames: false,
keep_infinity: false,
passes: 3,
},
mangle: {
eval: false,
keep_classnames: false,
keep_fnames: false,
toplevel: false,
safari10: false
},
module: false,
output: {
comments: false,
}
};
hasFullBuild ? config.sourceMap = { filename: 'DynamicGrid.min.js', url: 'DynamicGrid.min.js.map' } : null;
const separateFiles = [
"./DynamicGrid/libs/ContextMenu.js",
"./DynamicGrid/libs/EventEmitter.js",
"./DynamicGrid/libs/KeyboardShortcuts.js",
"./DynamicGrid/libs/APIConnector.js",
];
const cssFiles = [
"./DynamicGrid.css",
];
const eventRegex = /eventEmitter\.emit\('([^']*)',/g;
const events = [];
const shortcutRegex = /\.addShortcut\('([^']*)',.*?'([^']*)'/g;
const shortcuts = [];
// Helper function to get relative path from project root
function getRelativePath(absolutePath) {
return path.relative(process.cwd(), absolutePath);
}
function intToFileSize(bytes) {
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes === 0) return '0 Bytes';
const i = Math.floor(Math.log(bytes) / Math.log(1024));
return `${(bytes / Math.pow(1024, i)).toFixed(2)} ${sizes[i]}`;
}
/**
* Scans directories recursively to find all JavaScript files
*/
function findAllFiles(directories, extensions) {
const allFiles = new Set();
function scanDirectory(dir) {
if (!fs.existsSync(dir)) return;
const items = fs.readdirSync(dir);
for (const item of items) {
const fullPath = path.join(dir, item);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
scanDirectory(fullPath);
} else if (stat.isFile()) {
const ext = path.extname(item);
if (extensions.includes(ext)) {
// Normalize to absolute path for consistent comparison
allFiles.add(path.resolve(fullPath));
}
}
}
}
directories.forEach(dir => scanDirectory(dir));
return Array.from(allFiles);
}
/**
* Parses a JavaScript file to extract import/export dependencies
*/
function parseFileDependencies(filePath) {
const content = fs.readFileSync(filePath, 'utf8');
const dependencies = [];
// Common import patterns to match
const importPatterns = [
// ES6 imports
{ pattern: /import\s+.*?from\s+['"](.+)['"];?/g, name: 'ES6 import from' },
{ pattern: /import\s+['"](.+)['"];?/g, name: 'ES6 import' },
// CommonJS requires
{ pattern: /require\s*\(\s*['"](.+)['"]\s*\)/g, name: 'CommonJS require' },
// Custom patterns you might use
{ pattern: /\/\/\s*@requires?\s+['"]?(.+)['"]?/g, name: 'Comment @requires' },
{ pattern: /\/\*\s*@requires?\s+(['"]?)(.+)\1\s*\*\//g, name: 'Block comment @requires' },
// JSDoc-style dependencies
{ pattern: /\/\*\*[\s\S]*?@requires?\s+(['"]?)(.+)\1[\s\S]*?\*\//g, name: 'JSDoc @requires' },
// Additional patterns for more flexibility
{ pattern: /\/\/\s*@dependency\s+['"]?(.+)['"]?/g, name: 'Comment @dependency' },
{ pattern: /\/\/\s*@import\s+['"]?(.+)['"]?/g, name: 'Comment @import' },
{ pattern: /\/\/\s*@include\s+['"]?(.+)['"]?/g, name: 'Comment @include' }
];
importPatterns.forEach(({ pattern, name }) => {
let match;
while ((match = pattern.exec(content)) !== null) {
const importPath = match[1] || match[2];
if (!importPath) continue;
// Skip external modules (those not starting with . or /)
if (!importPath.startsWith('./') && !importPath.startsWith('../') && !importPath.startsWith('/')) {
continue;
}
// Resolve relative paths
const resolvedPath = resolveImportPath(filePath, importPath);
if (resolvedPath) {
dependencies.push(resolvedPath);
}
}
});
// if (dependencies.length > 0) {
// console.log(` π¦ Found (${dependencies.length}) dependencies`);
// }
return dependencies;
}
/**
* Resolves import paths relative to the importing file
*/
function resolveImportPath(fromFile, importPath) {
if (!importPath.startsWith('./') && !importPath.startsWith('../') && !importPath.startsWith('/')) {
return null; // External module
}
const fromDir = path.dirname(fromFile);
let resolved = path.resolve(fromDir, importPath);
// Try different extensions if no extension provided
if (!path.extname(resolved)) {
for (const ext of fileExtensions) {
const withExt = resolved + ext;
if (fs.existsSync(withExt)) {
// Return absolute path for consistent comparison
return path.resolve(withExt);
}
}
} else if (fs.existsSync(resolved)) {
return path.resolve(resolved);
}
console.log(` β Could not resolve: ${importPath} -> ${getRelativePath(resolved)}`);
return null;
}
/**
* Performs topological sort to determine build order
*/
function topologicalSort(dependencies) {
const graph = new Map();
const inDegree = new Map();
const allFiles = new Set();
// Initialize graph
for (const [file, deps] of Object.entries(dependencies)) {
allFiles.add(file);
if (!graph.has(file)) {
graph.set(file, []);
}
if (!inDegree.has(file)) {
inDegree.set(file, 0);
}
deps.forEach(dep => {
allFiles.add(dep);
if (!graph.has(dep)) {
graph.set(dep, []);
}
if (!inDegree.has(dep)) {
inDegree.set(dep, 0);
}
graph.get(dep).push(file);
inDegree.set(file, inDegree.get(file) + 1);
});
}
// Kahn's algorithm for topological sorting
const queue = [];
const result = [];
// Find all nodes with no incoming edges
for (const [file, degree] of inDegree) {
if (degree === 0) {
queue.push(file);
}
}
while (queue.length > 0) {
const current = queue.shift();
result.push(current);
// For each neighbor of current
const neighbors = graph.get(current) || [];
for (const neighbor of neighbors) {
inDegree.set(neighbor, inDegree.get(neighbor) - 1);
if (inDegree.get(neighbor) === 0) {
queue.push(neighbor);
}
}
}
// Check for circular dependencies
if (result.length !== allFiles.size) {
const remaining = Array.from(allFiles).filter(file => !result.includes(file));
throw new Error(`Circular dependency detected. Files involved: ${remaining.map(f => getRelativePath(f)).join(', ')}`);
}
return result;
}
/**
* Builds the dependency tree starting from entry point
*/
function buildDependencyTree(entryPoint, allFiles) {
const dependencies = {};
const visited = new Set();
//console.log(`\nπ Building dependency tree...`);
//console.log(`π All available files (${allFiles.length}):`);
// allFiles.forEach((file, index) => {
// console.log(` ${index + 1}. ${getRelativePath(file)}`);
// });
function traverse(filePath) {
if (visited.has(filePath)) {
return;
}
//console.log(`π Traversing: ${getRelativePath(filePath)}`);
visited.add(filePath);
const deps = parseFileDependencies(filePath);
dependencies[filePath] = deps;
deps.forEach(dep => {
if (allFiles.includes(dep)) {
traverse(dep);
} else {
// Let's see what files are close to this path
const basename = path.basename(dep);
const matches = allFiles.filter(f => path.basename(f) === basename);
if (matches.length > 0) {
matches.forEach(match => console.log(` - ${getRelativePath(match)}`));
}
}
});
}
traverse(entryPoint);
return dependencies;
}
/**
* Creates the dynamic files array based on dependencies
*/
function createDynamicFilesArray() {
console.log('\nπ Scanning for JavaScript files...');
// Find all JavaScript files in the project
const allFiles = findAllFiles(searchDirectories, fileExtensions);
console.log(`π Found ${allFiles.length} JavaScript files`);
// Build dependency tree starting from entry point
console.log(`π³ Building dependency tree from entry point: ${getRelativePath(entryPoint)}`);
const dependencies = buildDependencyTree(entryPoint, allFiles);
// Perform topological sort to get correct build order
console.log('π Performing topological sort...');
const sortedFiles = topologicalSort(dependencies);
// Filter to only include files that are actually dependencies
const relevantFiles = sortedFiles.filter(file =>
Object.keys(dependencies).includes(file) ||
Object.values(dependencies).some(deps => deps.includes(file))
);
console.log(`π Build order determined (${relevantFiles.length} files):`);
// relevantFiles.forEach((file, index) => {
// console.log(` ${index + 1}. ${getRelativePath(file)}`);
// });
return relevantFiles;
}
// Helper function to inject version comment
function injectVersionComment(code, version) {
const comment =
"/**\n" +
" * DynamicGrid is a library for rendering data in a grid format with dynamic querying capabilities.\n" +
" * @author Matt ter Steege (Kronk)\n" +
" * @license MIT\n" +
(version ? ` * @version ${version}\n` : '') +
" */\n";
return comment + code;
}
// Helper function to add delay
const delay = (ms) => !dramaticBuild ? new Promise(resolve => setTimeout(resolve, 0)) : new Promise(resolve => setTimeout(resolve, ms));
(async () => {
console.clear();
console.log('π DynamicGrid Dynamic Build Script');
console.log('=====================================');
console.log(`π Output Directory: ${outputDir}`);
console.log(`π§ Full Build: ${hasFullBuild ? 'Enabled' : 'Disabled'}`);
console.log(`π·οΈ Version: ${version ? version : 'Not specified'}`);
console.log(`π― Entry Point: ${getRelativePath(entryPoint)}`);
console.log('=====================================');
// Remove output directory if it exists
if (fs.existsSync(outputDir)) {
fs.rmSync(outputDir, { recursive: true, force: true });
}
// Create output directory
fs.mkdirSync(outputDir, { recursive: true });
await delay(200);
try {
// Create dynamic files array
const files = createDynamicFilesArray();
if (files.length === 0) {
console.log('β οΈ No files found to build!');
return;
}
console.log('\nπ¦ Starting build process...');
// Ensure output directory exists
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
let combinedCode = '';
console.log('\nπ Combining files in dependency order...');
await delay(200);
for (const file of files) {
const relativePath = getRelativePath(file);
combinedCode += `// ${relativePath}\n`;
const currFile = fs.readFileSync(file, 'utf8') + '\n\n';
combinedCode += currFile;
//console.log(`β
Added: ${relativePath}`);
// Extract events from the current file
let match;
while ((match = eventRegex.exec(currFile)) !== null) {
const eventName = match[1];
const lineNumber = currFile.substring(0, match.index).split('\n').length;
events.push({ eventName, file: path.basename(file), line: lineNumber });
//console.log(` π‘ Found event: ${eventName} in ${path.basename(file)} at line ${lineNumber}`);
}
if (file.includes("KeyboardShortcuts.js")) continue;
// Extract shortcuts from the current file
while ((match = shortcutRegex.exec(currFile)) !== null) {
const eventName = match[1];
const description = match[2] || '';
const lineNumber = currFile.substring(0, match.index).split('\n').length;
//console.log(` β¨οΈ Found shortcut: ${eventName} in ${path.basename(file)} at line ${lineNumber}`);
if (!description || description.trim() === '') {
console.log(`\x1b[33m β οΈ No description found for shortcut: ${eventName} in ${path.basename(file)} at line ${lineNumber} (SKIPPING)\x1b[0m`);
continue;
}
shortcuts.push({ keybind: eventName, description: description, file: path.basename(file), line: lineNumber });
}
await delay(100);
}
// Inject version comment if version is provided
if (version) {
combinedCode = injectVersionComment(combinedCode, version);
console.log(`π·οΈ Version comment injected: ${version}`);
}
console.log('\nπΎ Saving combined file...');
await delay(300);
const combinedFilePath = outputDir + 'DynamicGrid.js';
fs.writeFileSync(combinedFilePath, combinedCode);
console.log(`β
Combined file saved: ${combinedFilePath}`);
// Save events to a file
const eventsFilePath = outputDir + 'events.json';
fs.writeFileSync(eventsFilePath, JSON.stringify(events, null, 2));
console.log(`π‘ Events saved: ${eventsFilePath} (${events.length})`);
// Save shortcuts to a file
const shortcutsFilePath = outputDir + 'shortcuts.json';
fs.writeFileSync(shortcutsFilePath, JSON.stringify(shortcuts, null, 2));
console.log(`β¨οΈ Shortcuts saved: ${shortcutsFilePath} (${shortcuts.length})`);
await delay(300);
console.log('\nποΈ Minifying combined file...');
await delay(300);
const minifiedCombined = await minify(combinedCode, {
...config,
});
minifiedCombined.code = injectVersionComment(minifiedCombined.code, version);
fs.writeFileSync(outputDir + 'DynamicGrid.min.js', minifiedCombined.code);
hasFullBuild ? fs.writeFileSync(outputDir + 'DynamicGrid.min.js.map', minifiedCombined.map) : null;
console.log('β
Minified combined file and source map saved.');
await delay(300);
console.log(`π¦ ${intToFileSize(Buffer.byteLength(combinedCode, 'utf8'))} combined vs ${intToFileSize(Buffer.byteLength(minifiedCombined.code, 'utf8'))} minified (${((Buffer.byteLength(combinedCode, 'utf8') - Buffer.byteLength(minifiedCombined.code, 'utf8')) / Buffer.byteLength(combinedCode, 'utf8') * 100).toFixed(2)}% reduction)\n`);
//console.log('\nπ§ Processing separate files...');
await delay(200);
for (const file of separateFiles) {
console.log(`π Processing: ${file}`);
await delay(100);
let fileCode = fs.readFileSync(file, 'utf8');
//get author and license comment from file as a whole
const authorComment = fileCode.match(/(\/\/[^\n]*$|\/(?!\\)\*[\s\S]*?\*(?!\\)\/)/);
const fileName = file.split('/').pop();
const unminifiedFilePath = `${outputDir}${fileName}`;
const minifiedFilePath = `${outputDir}${fileName.replace('.js', '.min.js')}`;
const sourceMapPath = `${minifiedFilePath}.map`;
hasFullBuild ? fs.writeFileSync(unminifiedFilePath, fileCode) : null;
//console.log(` πΎ Unminified file saved`);
await delay(100);
const separateConfig = {
compress: true,
mangle: true,
output: {
comments: false,
}
};
hasFullBuild ? separateConfig.sourceMap = { filename: `${fileName}.min.js`, url: `${fileName}.min.js.map` } : null;
const minifiedFile = await minify(fileCode, separateConfig);
minifiedFile.code = authorComment[0] + '\n' + minifiedFile.code;
fs.writeFileSync(minifiedFilePath, minifiedFile.code);
hasFullBuild ? fs.writeFileSync(sourceMapPath, minifiedFile.map) : null;
//console.log(` ποΈ Minified file and source map saved\n`);
await delay(200);
}
//console.log('\nπ¨ Processing CSS files...');
await delay(200);
for (const cssFile of cssFiles) {
console.log(`π Processing: ${cssFile}`);
await delay(100);
let cssCode = fs.readFileSync(cssFile, 'utf8');
const cssFileName = cssFile.split('/').pop();
const unminifiedCssPath = `${outputDir}${cssFileName}`;
const minifiedCssPath = `${outputDir}${cssFileName.replace('.css', '.min.css')}`;
hasFullBuild ? fs.writeFileSync(unminifiedCssPath, cssCode) : null;
//console.log(` πΎ Unminified CSS file saved: ${unminifiedCssPath}`);
var minifiedCss = cssCode.replace(/\s+/g, ' ').trim();
minifiedCss = minifiedCss.replace(/\/\*[\s\S]*?\*\//g, '').trim();
if (version) {
minifiedCss = `/* @version ${version} */\n` + minifiedCss;
}
fs.writeFileSync(minifiedCssPath, minifiedCss);
//console.log(` ποΈ Minified CSS file saved: ${minifiedCssPath}\n`);
}
console.log('\nπ === Build Process Completed Successfully ===');
console.log(`π¦ Total files processed: ${files.length}`);
console.log(`π‘ Events found: ${events.length}`);
console.log(`β¨οΈ Shortcuts found: ${shortcuts.length}`);
if (hasGithub) {
console.log('\nπ === Publishing to GitHub package repository ===');
console.log('π GitHub package repository updated');
console.log('π Full package is soon available at:');
console.log(`π https://cdn.jsdelivr.net/gh/matttersteege/dynamicgrid@${version ? version : 'latest'}/Dist/`);
}
} catch (error) {
console.error('β Error during build process:', error);
process.exit(1);
}
})();