vaderjs 1.4.0 → 1.4.1-hyguy47

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1455 @@
1
+ import { Glob } from "bun";
2
+
3
+ import fs from "fs";
4
+
5
+ import * as Bun from "bun";
6
+
7
+ import WebSocket from "ws";
8
+
9
+ let config = await import(process.cwd() + '/vader.config.js').then((m) => m ? m.default : {}).catch((e) => {
10
+
11
+ console.error(e)
12
+
13
+ return {}
14
+
15
+ })
16
+
17
+ import IPCServer from "vaderjs/binaries/IPC/index.js"
18
+
19
+ import { exec } from "child_process";
20
+
21
+ const IPC = IPCServer
22
+
23
+
24
+
25
+ globalThis.isVader = true
26
+
27
+ /**
28
+
29
+ * @description - Call functions when the integration is triggered
30
+
31
+ * @param {function} fn
32
+
33
+ * @param {args} args
34
+
35
+ */
36
+
37
+ globalThis.call = async (fn, args) => {
38
+
39
+ return await fn(args) || void 0
40
+
41
+ }
42
+
43
+ /**@description - Used to store hmr websocket clients */
44
+
45
+ globalThis.clients = []
46
+
47
+ /**@description - Used to keep track of routes */
48
+
49
+ globalThis.routes = []
50
+
51
+ /**
52
+
53
+ * @description - Used to keep track of the mode
54
+
55
+ */
56
+
57
+ globalThis.mode = ''
58
+
59
+ /**@usedby @transForm */
60
+
61
+ globalThis.isBuilding = false
62
+
63
+ globalThis.hasGenerated = []
64
+
65
+ let currentState = ''
66
+
67
+ /**
68
+
69
+ * @description - Used to keep track of the bundle size
70
+
71
+ */
72
+
73
+ let bundleSize = 0
74
+
75
+
76
+
77
+
78
+
79
+ /**
80
+
81
+ * @description - variables used to generate arrays of paths recursively
82
+
83
+ */
84
+
85
+ const glob = new Glob("/pages/**/**/*.{,tsx,js,jsx}", { absolute: true });
86
+
87
+ const vaderGlob = new Glob("/node_modules/vaderjs/runtime/**/**/*.{,tsx,js}", { absolute: true });
88
+
89
+ const srcGlob = new Glob("/src/**/**/*.{jsx,ts,tsx,js}", { absolute: true });
90
+
91
+ const publicGlob = new Glob("/public/**/**/*.{css,js,html,jpg,png,gif,svg,ico,video,webm,mp4,jpeg}", { absolute: true });
92
+
93
+ const distPages = new Glob("/dist/pages/**/**/*.{tsx,js,jsx}", { absolute: true })
94
+
95
+ const distSrc = new Glob("/dist/src/**/**/*.{tsx,js,jsx}", { absolute: true })
96
+
97
+ const distPublic = new Glob("/dist/public/**/**/*.{css,js,html,jpg,png,gif,svg,ico,video,webm,mp4,jpeg}", { absolute: true });
98
+
99
+
100
+
101
+ const router = new Bun.FileSystemRouter({
102
+
103
+ style: "nextjs",
104
+
105
+ dir: process.cwd() + '/pages',
106
+
107
+ origin: process.env.ORIGIN || "http://localhost:3000",
108
+
109
+ assetPrefix: "_next/static/"
110
+
111
+ });
112
+
113
+ /**
114
+
115
+ * @function handleReplaceMents
116
+
117
+ * @description - replaces data to be compatible with Vader.js
118
+
119
+ * @param {*} data
120
+
121
+ * @returns
122
+
123
+ */
124
+
125
+ function handleReplaceMents(data) {
126
+
127
+ data.split('\n').forEach((line, index) => {
128
+
129
+ switch (true) {
130
+
131
+
132
+
133
+ case line.includes('useReducer') && !line.includes('import'):
134
+
135
+ line = line.replaceAll(/\s+/g, " ");
136
+
137
+
138
+
139
+ let varTypereducer = line.split("=")[0].trim().split("[")[0].trim();
140
+
141
+ let keyreducer = line.split("=")[0].trim().split("[")[1].trim().split(",")[0].trim();
142
+
143
+ let setKeyreducer = line.split("=")[0].trim().split(",")[1].trim().replace("]", "");
144
+
145
+ let reducer = line.split("=")[1].split("useReducer(")[1];
146
+
147
+
148
+
149
+ let newStatereducer = `${varTypereducer} [${keyreducer}, ${setKeyreducer}] = this.useReducer('${keyreducer}', ${line.includes('=>') ? reducer + '=>{' : reducer}`;
150
+
151
+
152
+
153
+ data = data.replace(line, newStatereducer);
154
+
155
+ break
156
+
157
+
158
+
159
+ case line.includes('useState') && !line.includes('import'):
160
+
161
+ let varType = line.split("[")[0]
162
+
163
+ if (!line.split("=")[0].split(",")[1]) {
164
+
165
+ throw new Error('You forgot to value selector (useState) ' + ' at ' + `${file}:${string.split(line)[0].split('\n').length}`)
166
+
167
+ }
168
+
169
+ let key = line.split("=")[0].split(",")[0].trim().split('[')[1];
170
+
171
+
172
+
173
+ if (!line.split("=")[0].split(",")[1]) {
174
+
175
+ throw new Error('You forgot to add a setter (useState) ' + ' at ' + `${file}:${string.split(line)[0].split('\n').length}`)
176
+
177
+ }
178
+
179
+ let setKey = line.split("=")[0].split(",")[1].trim().replace("]", "");
180
+
181
+ key = key.replace("[", "").replace(",", "");
182
+
183
+ let valuestate = line.split("=")[1].split("useState(")[1];
184
+
185
+
186
+
187
+ let regex = /useState\((.*)\)/gs;
188
+
189
+ valuestate = valuestate.match(regex) ? valuestate.match(regex)[0].split("useState(")[1].split(")")[0].trim() : valuestate
190
+
191
+ let newState = `${varType} [${key}, ${setKey}] = this.useState('${key}', ${valuestate}`;
192
+
193
+ data = data.replace(line, newState);
194
+
195
+ break;
196
+
197
+
198
+
199
+ case line.includes("useRef") && !line.includes("import"):
200
+
201
+ line = line.trim();
202
+
203
+ let typeref = line.split(" ")[0]
204
+
205
+
206
+
207
+ let keyref = line.split(typeref)[1].split("=")[0].trim().replace("[", "").replace(",", "");
208
+
209
+
210
+
211
+
212
+
213
+ let valueref = line.split("=")[1].split("useRef(")[1];
214
+
215
+
216
+
217
+ let newStateref = `${typeref} ${keyref} = this.useRef('${keyref}', ${valueref}`;
218
+
219
+ data = data.replace(line, newStateref);
220
+
221
+ case line.includes('.jsx') && line.includes('import') || line.includes('.tsx') && line.includes('import'):
222
+
223
+ let old = line
224
+
225
+ line = line.replace('.jsx', '.js')
226
+
227
+ line = line.replace('.tsx', '.js')
228
+
229
+ data = data.replace(old, line)
230
+
231
+
232
+
233
+ break;
234
+
235
+ }
236
+
237
+ })
238
+
239
+
240
+
241
+ data = data.replaceAll('jsxDEV', 'Vader.createElement')
242
+
243
+ data = data.replaceAll('jsx', 'Vader.createElement')
244
+
245
+ data = data.replaceAll('vaderjs/client', '/vader.js')
246
+
247
+ data = data.replaceAll('.tsx', '.js')
248
+
249
+ let reactImportMatch = data.match(/import React/g);
250
+
251
+ if (reactImportMatch) {
252
+
253
+ let fullmatch = data.match(/import React from "react"/g);
254
+
255
+ if (fullmatch) {
256
+
257
+ data = data.replaceAll('import React from "react"', '');
258
+
259
+ }
260
+
261
+ }
262
+
263
+ data = data.replaceAll('.ts', '.js')
264
+
265
+
266
+
267
+ // check if Vader is imported
268
+
269
+ let vaderImport = data.match(/import Vader/g);
270
+
271
+ if (!vaderImport) {
272
+
273
+ data = `import Vader from "/vader.js";\n` + data;
274
+
275
+ }
276
+
277
+ return data;
278
+
279
+ }
280
+
281
+
282
+
283
+
284
+
285
+ /**
286
+
287
+ * @function ContentType
288
+
289
+ * @description - returns the content type based on the file extension
290
+
291
+ * @param {*} url
292
+
293
+ * @returns
294
+
295
+ */
296
+
297
+ const ContentType = (url) => {
298
+
299
+ switch (url.split('.').pop()) {
300
+
301
+ case 'css':
302
+
303
+ return 'text/css';
304
+
305
+ case 'js':
306
+
307
+ return 'text/javascript';
308
+
309
+ case 'json':
310
+
311
+ return 'application/json';
312
+
313
+ case 'html':
314
+
315
+ return 'text/html';
316
+
317
+ case 'jpg':
318
+
319
+ return 'image/jpg';
320
+
321
+ case 'png':
322
+
323
+ return 'image/png';
324
+
325
+ case 'gif':
326
+
327
+ return 'image/gif';
328
+
329
+ case 'svg':
330
+
331
+ return 'image/svg+xml';
332
+
333
+ case 'ico':
334
+
335
+ return 'image/x-icon';
336
+
337
+ default:
338
+
339
+ return 'text/html';
340
+
341
+
342
+
343
+ }
344
+
345
+ }
346
+
347
+
348
+
349
+
350
+
351
+ /**
352
+
353
+ * @function Server
354
+
355
+ * @description - Creates a hmr development server
356
+
357
+ * @param {Number} port
358
+
359
+ */
360
+
361
+ function Server(port) {
362
+
363
+ Bun.serve({
364
+
365
+ port: port,
366
+
367
+ async fetch(req, res) {
368
+
369
+ const url = new URL(req.url);
370
+
371
+ const success = res.upgrade(req);
372
+
373
+ if (success) {
374
+
375
+ return new Response('Connected', {
376
+
377
+ headers: {
378
+
379
+ "Content-Type": "text/html"
380
+
381
+ }
382
+
383
+ })
384
+
385
+ }
386
+
387
+ if (req.url.includes('.')) {
388
+
389
+ if (!fs.existsSync(process.cwd() + '/dist' + url.pathname)) {
390
+
391
+ return new Response('Not Found', {
392
+
393
+ status: 404,
394
+
395
+ headers: {
396
+
397
+ "Content-Type": "text/html"
398
+
399
+ }
400
+
401
+ })
402
+
403
+ }
404
+
405
+ console.log('Serving: ' + url.pathname)
406
+
407
+ return new Response(fs.readFileSync(process.cwd() + '/dist/' + url.pathname, 'utf-8'), {
408
+
409
+ headers: {
410
+
411
+ "Content-Type": ContentType(req.url)
412
+
413
+ }
414
+
415
+ })
416
+
417
+ }
418
+
419
+ let matchedRoute = router.match(url.pathname);
420
+
421
+ if (matchedRoute) {
422
+
423
+ let { filePath, kind, name, params, pathname, query, } = matchedRoute
424
+
425
+ let folder = url.pathname.split('/')[1]
426
+
427
+ let jsFile = filePath.split('pages/').pop().split('.').shift() + '.js'
428
+
429
+ let pageContent = fs.readFileSync(process.cwd() + '/dist/' + folder + '/index.html', 'utf8')
430
+
431
+ globalThis.mode === 'dev' ? pageContent += `
432
+
433
+ <script type="module">
434
+
435
+ let ws = new WebSocket('ws://localhost:${port}');
436
+
437
+ ws.onmessage = async (e) => {
438
+
439
+ if(e.data === 'reload'){
440
+
441
+ window.location.reload()
442
+
443
+ console.log('Reloading...')
444
+
445
+ }
446
+
447
+ }
448
+
449
+ </script>
450
+
451
+ ` : void 0
452
+
453
+ return new Response(pageContent, {
454
+
455
+ headers: {
456
+
457
+ "Content-Type": "text/html",
458
+
459
+ "X-Powered-By": "Vader.js v1.3.3"
460
+
461
+ }
462
+
463
+ })
464
+
465
+ }
466
+
467
+
468
+
469
+ return new Response('Not Found', {
470
+
471
+ status: 404,
472
+
473
+ headers: {
474
+
475
+ "Content-Type": "text/html"
476
+
477
+ }
478
+
479
+ })
480
+
481
+ },
482
+
483
+ websocket: {
484
+
485
+ open(ws) {
486
+
487
+ clients.push(ws)
488
+
489
+ },
490
+
491
+ }
492
+
493
+ })
494
+
495
+ }
496
+
497
+ /**
498
+
499
+ * @function write
500
+
501
+ * @description - Writes data to a file
502
+
503
+ * @returns {void} 0
504
+
505
+ * @param {string} file
506
+
507
+ * @param {any} data
508
+
509
+ * @returns
510
+
511
+ */
512
+
513
+ const write = (file, data) => {
514
+
515
+ try {
516
+
517
+ if (!fs.existsSync('./dist')) {
518
+
519
+ fs.mkdirSync('./dist')
520
+
521
+ }
522
+
523
+ Bun.write(file, data);
524
+
525
+ } catch (error) {
526
+
527
+ console.error(error)
528
+
529
+ }
530
+
531
+
532
+
533
+ }
534
+
535
+ /**
536
+
537
+ * @function read
538
+
539
+ * @param {path} file
540
+
541
+ * @returns {Promise<string>}
542
+
543
+ */
544
+
545
+ const read = async (file) => {
546
+
547
+ return await Bun.file(file).text();
548
+
549
+ }
550
+
551
+ /**
552
+
553
+ * @object integrationStates
554
+
555
+ * @description - Used to store integration states
556
+
557
+ */
558
+
559
+
560
+
561
+ globalThis.integrationStates = []
562
+
563
+ /**
564
+
565
+ * @description a boolean value that checks if vader is being used - useful for running toplevel code at integration start
566
+
567
+ */
568
+
569
+ globalThis.context = {
570
+
571
+ vader: true
572
+
573
+ }
574
+
575
+ /**
576
+
577
+ * @function handleIntegrations
578
+
579
+ * @description - Handles module integrations that add more functionality to Vader.js
580
+
581
+ * @returns {void} 0
582
+
583
+ */
584
+
585
+ function handleIntegrations() {
586
+
587
+ if (config?.integrations) {
588
+
589
+ config.integrations.forEach((integration) => {
590
+
591
+ let int = integration
592
+
593
+ globalThis.integrationStates.push(int)
594
+
595
+ })
596
+
597
+ }
598
+
599
+
600
+
601
+ return void 0;
602
+
603
+ }
604
+
605
+ handleIntegrations()
606
+
607
+ /**
608
+
609
+ * @function generateProviderRoutes
610
+
611
+ * @description - Generates routes for hosting provders ie: vercel, cloudflare
612
+
613
+ * @returns {void} 0
614
+
615
+ */
616
+
617
+ async function generateProviderRoutes() {
618
+
619
+ if (!config?.host?.provider) {
620
+
621
+ console.warn('No provider found in vader.config.js ignoring route generation...')
622
+
623
+ return void 0;
624
+
625
+ }
626
+
627
+ let providerType = [{
628
+
629
+ provider: 'vercel',
630
+
631
+ file: 'vercel.json',
632
+
633
+ out: process.cwd() + '/vercel.json',
634
+
635
+ obj: {
636
+
637
+ rewrites: []
638
+
639
+ }
640
+
641
+ }, {
642
+
643
+ provider: 'cloudflare',
644
+
645
+ file: '_redirects',
646
+
647
+ out: 'dist/_redirects'
648
+
649
+ }]
650
+
651
+
652
+
653
+ let provider = providerType.find((p) => p.provider === config.host.provider)
654
+
655
+ if (provider) {
656
+
657
+ let prev = null
658
+
659
+
660
+
661
+ switch (provider.provider) {
662
+
663
+ case 'vercel':
664
+
665
+ if (!fs.existsSync(provider.out)) {
666
+
667
+ fs.writeFileSync(provider.out, JSON.stringify({ rewrites: [] }))
668
+
669
+ }
670
+
671
+ prev = await read(provider.out);
672
+
673
+ if (!prev) {
674
+
675
+ prev = []
676
+
677
+ } else {
678
+
679
+ prev = JSON.parse(prev).rewrites
680
+
681
+ }
682
+
683
+ routes.forEach((r) => {
684
+
685
+ let previous = prev.find((p) => p.source.includes(r.path))
686
+
687
+ if (previous) {
688
+
689
+ return void 0;
690
+
691
+ }
692
+
693
+
694
+
695
+ prev.push({
696
+
697
+ source: '/' + r.path,
698
+
699
+ destination: '/' + r.path + '/index.html'
700
+
701
+ })
702
+
703
+
704
+
705
+ if (r.params.length > 0) {
706
+
707
+ r.params.forEach((param) => {
708
+
709
+ if (!param.paramData) {
710
+
711
+ return void 0;
712
+
713
+ }
714
+
715
+ let parampath = Object.keys(param.paramData).map((p) => `:${p}`).join('/')
716
+
717
+ prev.push({
718
+
719
+ source: '/' + r.path + '/' + parampath,
720
+
721
+ destination: '/' + r.path + '/index.html'
722
+
723
+ })
724
+
725
+ })
726
+
727
+ }
728
+
729
+
730
+
731
+ fs.writeFileSync(provider.out, JSON.stringify({ rewrites: prev }, null, 2))
732
+
733
+
734
+
735
+
736
+
737
+ })
738
+
739
+ provider.obj.rewrites = prev
740
+
741
+ write(provider.out, JSON.stringify(provider.obj, null, 2))
742
+
743
+ break;
744
+
745
+ case 'cloudflare':
746
+
747
+ console.warn('Cloudflare is not supported yet refer to their documentation for more information:https://developers.cloudflare.com/pages/configuration/redirects/')
748
+
749
+ break;
750
+
751
+ }
752
+
753
+
754
+
755
+
756
+
757
+
758
+
759
+
760
+
761
+ }
762
+
763
+ return void 0;
764
+
765
+ }
766
+
767
+
768
+
769
+ /**
770
+
771
+ * @function transform
772
+
773
+ * @description - Transforms the jsx files to js files based on file paths
774
+
775
+ */
776
+
777
+
778
+
779
+ async function transForm() {
780
+
781
+ return new Promise(async (resolve, reject) => {
782
+
783
+ globalThis.isBuilding = true
784
+
785
+ router.reload()
786
+
787
+ for await (var file of glob.scan('.')) {
788
+
789
+ file = file.replace(/\\/g, '/');
790
+
791
+ let isBasePath = file.split('pages/')?.[1]?.split('/').length === 1;
792
+
793
+
794
+
795
+ let folder = file.split('pages/')?.[1]?.split('/').slice(0, -1).join('/') || null;
796
+
797
+ if (isBasePath) {
798
+
799
+ folder = '/'
800
+
801
+ }
802
+
803
+ let route = isBasePath ? router.match('/') : router.match('/' + folder)
804
+
805
+
806
+
807
+ if (route) {
808
+
809
+ let { filePath, kind, name, params, pathname, query, } = route
810
+
811
+ let data = await read(filePath);
812
+
813
+ try {
814
+
815
+ data = new Bun.Transpiler({ loader: "tsx", target: "browser", }).transformSync(data);
816
+
817
+ } catch (e) {
818
+
819
+ console.error(e)
820
+
821
+ }
822
+
823
+ let out = `./dist/${isBasePath ? 'index.js' : folder + '/index.js'}`;
824
+
825
+ isBasePath ? folder = '/' : null;
826
+
827
+ data = handleReplaceMents(data);
828
+
829
+ let isAparam = null
830
+
831
+ if (folder === "") {
832
+
833
+ return
834
+
835
+ }
836
+
837
+ switch (true) {
838
+
839
+ case kind === 'dynamic':
840
+
841
+ isAparam = true
842
+
843
+ break;
844
+
845
+ case kind === 'catch-all':
846
+
847
+ isAparam = true
848
+
849
+ break;
850
+
851
+ case kind === 'optional-catch-all':
852
+
853
+ isAparam = true
854
+
855
+ break;
856
+
857
+ }
858
+
859
+ routes.push({ path: folder, file: out, isParam: isAparam, params: params, query, pathname })
860
+
861
+ write(out, data);
862
+
863
+ bundleSize += data.length
864
+
865
+
866
+
867
+
868
+
869
+ }
870
+
871
+
872
+
873
+ }
874
+
875
+
876
+
877
+ for await (var file of srcGlob.scan('.')) {
878
+
879
+ if (!fs.existsSync(process.cwd() + '/dist/src/')) {
880
+
881
+ fs.mkdirSync(process.cwd() + '/dist/src/')
882
+
883
+ }
884
+
885
+ file = file.replace(/\\/g, '/');
886
+
887
+ switch (file.split('.').pop()) {
888
+
889
+ case 'ts':
890
+
891
+ let transpiler = new Bun.Transpiler({ loader: "ts", target: "browser", });
892
+
893
+ let data = await read(file);
894
+
895
+ try {
896
+
897
+ data = transpiler.transformSync(data);
898
+
899
+ } catch (error) {
900
+
901
+ console.error(error)
902
+
903
+ }
904
+
905
+ file = file.replace('.ts', '.js')
906
+
907
+ let path = process.cwd() + '/dist/src/' + file.split('src/').pop()
908
+
909
+ write(path, data);
910
+
911
+ bundleSize += data.length
912
+
913
+
914
+
915
+ break;
916
+
917
+
918
+
919
+ case 'tsx':
920
+
921
+ let transpilerx = new Bun.Transpiler({ loader: "tsx", target: "browser", });
922
+
923
+ let datax = await read(file);
924
+
925
+ try {
926
+
927
+ datax = transpilerx.transformSync(datax);
928
+
929
+ } catch (error) {
930
+
931
+ console.error(error)
932
+
933
+ }
934
+
935
+ datax = handleReplaceMents(datax);
936
+
937
+ file = file.replace('.tsx', '.js')
938
+
939
+ let pathx = process.cwd() + '/dist/src/' + file.split('src/').pop()
940
+
941
+ write(pathx, datax);
942
+
943
+
944
+
945
+
946
+
947
+ break;
948
+
949
+ case 'jsx':
950
+
951
+ let transpilerjx = new Bun.Transpiler({ loader: "jsx", target: "browser" });
952
+
953
+ let datajx = await read(file);
954
+
955
+
956
+
957
+ let source = transpilerjx.scan(datajx)
958
+
959
+ try {
960
+
961
+ datajx = transpilerjx.transformSync(datajx)
962
+
963
+ } catch (error) {
964
+
965
+ console.error(error)
966
+
967
+ }
968
+
969
+ datajx = handleReplaceMents(datajx);
970
+
971
+ file = file.replace('.jsx', '.js')
972
+
973
+ let pathjx = process.cwd() + '/dist/src/' + file.split('src/').pop()
974
+
975
+ write(pathjx, datajx);
976
+
977
+ bundleSize += datajx.length
978
+
979
+ break;
980
+
981
+ }
982
+
983
+ }
984
+
985
+
986
+
987
+
988
+
989
+ for await (var file of publicGlob.scan('.')) {
990
+
991
+ let data = await read(file);
992
+
993
+ file = file.replace(/\\/g, '/');
994
+
995
+ write(process.cwd() + '/dist/public/' + file.split('public/').pop(), data);
996
+
997
+ bundleSize += fs.statSync(process.cwd() + '/dist/public/' + file.split('public/').pop()).size
998
+
999
+ }
1000
+
1001
+ for await (var file of vaderGlob.scan('.')) {
1002
+
1003
+ let data = await read(file);
1004
+
1005
+ file = file.replace(/\\/g, '/');
1006
+
1007
+ write(process.cwd() + '/dist/' + file.split('node_modules/vaderjs/runtime/').pop(), data);
1008
+
1009
+ bundleSize += fs.statSync(process.cwd() + '/dist/' + file.split('node_modules/vaderjs/runtime/').pop()).size
1010
+
1011
+ }
1012
+
1013
+
1014
+
1015
+ // clean dist folder
1016
+
1017
+ for await (var file of distPages.scan('.')) {
1018
+
1019
+ file = file.replace(/\\/g, '/');
1020
+
1021
+ let path = process.cwd() + '/pages/' + file.split('dist/pages/').pop()
1022
+
1023
+ path = path.replace('.js', config?.files?.mimeType || '.jsx')
1024
+
1025
+
1026
+
1027
+ if (!fs.existsSync(path)) {
1028
+
1029
+ fs.unlinkSync(file)
1030
+
1031
+ }
1032
+
1033
+
1034
+
1035
+ }
1036
+
1037
+
1038
+
1039
+
1040
+
1041
+
1042
+
1043
+
1044
+
1045
+
1046
+
1047
+
1048
+
1049
+ /**
1050
+
1051
+ * @function organizeRoutes
1052
+
1053
+ * @description - Organizes routes that have param paths
1054
+
1055
+ */
1056
+
1057
+
1058
+
1059
+ const organizeRoutes = () => {
1060
+
1061
+ // if path starts with the same path and is dynamic then they are the same route and push params to the same route
1062
+
1063
+ let newRoutes = []
1064
+
1065
+ routes.forEach((route) => {
1066
+
1067
+ let exists = routes.find((r) => {
1068
+
1069
+ if (r.path.includes('[')) {
1070
+
1071
+ r.path = r.path.split('[').shift()
1072
+
1073
+ }
1074
+
1075
+
1076
+
1077
+ r.path = r.path.split('/').filter((p) => p !== '').join('/')
1078
+
1079
+ if (r.isParam) {
1080
+
1081
+ return r.path === route.path && r.isParam
1082
+
1083
+ }
1084
+
1085
+
1086
+
1087
+ })
1088
+
1089
+ if (exists) {
1090
+
1091
+ let b4Params = route.params
1092
+
1093
+ route.params = []
1094
+
1095
+ route.params.push(b4Params)
1096
+
1097
+ route.params.push({
1098
+
1099
+ jsFile: '/' + exists.path + '/index.js',
1100
+
1101
+ folder: '/' + exists.path,
1102
+
1103
+ paramData: exists.params
1104
+
1105
+ }
1106
+
1107
+ )
1108
+
1109
+ route.query = exists.query
1110
+
1111
+ newRoutes.push(route)
1112
+
1113
+ }
1114
+
1115
+ else if (!exists && !route.isParam) {
1116
+
1117
+ newRoutes.push(route)
1118
+
1119
+
1120
+
1121
+ }
1122
+
1123
+ //remove param route that matched
1124
+
1125
+ routes = routes.filter((r) => exists ? r.path !== exists.path : true)
1126
+
1127
+
1128
+
1129
+ })
1130
+
1131
+ globalThis.routes = newRoutes
1132
+
1133
+ }
1134
+
1135
+ organizeRoutes()
1136
+
1137
+
1138
+
1139
+
1140
+
1141
+ generateProviderRoutes()
1142
+
1143
+ globalThis.isBuilding = false
1144
+
1145
+ if (!fs.existsSync(process.cwd() + '/_dev/meta')) {
1146
+
1147
+ fs.mkdirSync(process.cwd() + '/_dev/meta')
1148
+
1149
+ }
1150
+
1151
+ fs.writeFileSync(process.cwd() + '/_dev/meta/routes.json', JSON.stringify(routes, null, 2))
1152
+
1153
+ console.log(`Finished building ${Math.round(bundleSize / 1000)}kb`)
1154
+
1155
+
1156
+
1157
+ bundleSize = 0
1158
+
1159
+ resolve()
1160
+
1161
+ });
1162
+
1163
+
1164
+
1165
+ }
1166
+
1167
+ let port = 3000
1168
+
1169
+ switch (true) {
1170
+
1171
+ case process.argv.includes('dev') && !process.argv.includes('build') && !process.argv.includes('start'):
1172
+
1173
+
1174
+
1175
+ port = process.argv.includes('-p') ? process.argv[process.argv.indexOf('-p') + 1] : config?.dev?.port || 3000
1176
+
1177
+ globalThis.oneAndDone = false
1178
+
1179
+ console.log(`
1180
+
1181
+ Vader.js v${fs.readFileSync(process.cwd() + '/node_modules/vaderjs/package.json', 'utf8').split('"version": "')[1].split('"')[0]}
1182
+
1183
+ - Watching for changes in ./pages
1184
+
1185
+ - Watching for changes in ./src
1186
+
1187
+ - Watching for changes in ./public
1188
+
1189
+ - Serving on port ${port}
1190
+
1191
+ `)
1192
+
1193
+ globalThis.mode = 'dev'
1194
+
1195
+ Server(port)
1196
+
1197
+ transForm()
1198
+
1199
+
1200
+
1201
+ Bun.spawn({
1202
+
1203
+ cwd: process.cwd() + '/node_modules/vaderjs/binaries/',
1204
+
1205
+ env: {
1206
+
1207
+ PWD: process.cwd(),
1208
+
1209
+ IPC: IPC,
1210
+
1211
+ FOLDERS: 'pages,src,public',
1212
+
1213
+ onExit: (code) => {
1214
+
1215
+ globalThis.isBuilding = false
1216
+
1217
+ globalThis.oneAndDone = true
1218
+
1219
+ }
1220
+
1221
+ },
1222
+
1223
+ cmd: ['node', 'watcher.js'],
1224
+
1225
+ })
1226
+
1227
+ async function runOnChange() {
1228
+
1229
+ for (var ints in integrationStates) {
1230
+
1231
+ if (integrationStates && integrationStates[ints].on.includes('dev:change')) {
1232
+
1233
+ console.log('Starting integration...')
1234
+
1235
+ let int = integrationStates[ints]
1236
+
1237
+ let { name, version, useRuntime, entryPoint, onAction, doOn } = int
1238
+
1239
+ if (globalThis.isBuilding) {
1240
+
1241
+ return
1242
+
1243
+ }
1244
+
1245
+ globalThis.isBuilding = true
1246
+
1247
+ Bun.spawn({
1248
+
1249
+ cwd: process.cwd(),
1250
+
1251
+ isVader: true,
1252
+
1253
+ env: {
1254
+
1255
+ PWD: process.cwd(),
1256
+
1257
+ isVader: true,
1258
+
1259
+ FOLDERS: 'pages,src,public',
1260
+
1261
+ onExit: (code) => {
1262
+
1263
+ globalThis.isBuilding = false
1264
+
1265
+ }
1266
+
1267
+ },
1268
+
1269
+ cmd: [useRuntime || 'node', entryPoint],
1270
+
1271
+ })
1272
+
1273
+
1274
+
1275
+ console.log(`Using integration: ${name} v${version}`)
1276
+
1277
+
1278
+
1279
+ }
1280
+
1281
+ }
1282
+
1283
+ }
1284
+
1285
+
1286
+
1287
+
1288
+
1289
+ IPC.client({
1290
+
1291
+ use: IPCServer.typeEnums.WATCHER,
1292
+
1293
+ port: 3434
1294
+
1295
+ }).Console.read((message) => {
1296
+
1297
+ message = message.msg
1298
+
1299
+ switch (true) {
1300
+
1301
+ case message.data?.type === 'change':
1302
+
1303
+ console.log('File changed:', message.data.filename)
1304
+
1305
+ transForm()
1306
+
1307
+ clients.forEach((client) => {
1308
+
1309
+ client.send('reload')
1310
+
1311
+ })
1312
+
1313
+ runOnChange()
1314
+
1315
+
1316
+
1317
+ break;
1318
+
1319
+ case message.data?.type === 'add':
1320
+
1321
+ console.log('File added:', message.data.filename)
1322
+
1323
+ transForm()
1324
+
1325
+ break;
1326
+
1327
+ }
1328
+
1329
+ })
1330
+
1331
+
1332
+
1333
+
1334
+
1335
+
1336
+
1337
+ break;
1338
+
1339
+ case process.argv.includes('build') && !process.argv.includes('dev') && !process.argv.includes('start'):
1340
+
1341
+ globalThis.devMode = false
1342
+
1343
+
1344
+
1345
+
1346
+
1347
+
1348
+
1349
+ globalThis.mode = 'prod'
1350
+
1351
+ globalThis.isProduction = true
1352
+
1353
+ globalThis.routeStates = []
1354
+
1355
+ console.log(`
1356
+
1357
+ Vader.js v1.3.3
1358
+
1359
+ Building to ./dist
1360
+
1361
+ `)
1362
+
1363
+
1364
+
1365
+ await transForm()
1366
+
1367
+ for (var ints in integrationStates) {
1368
+
1369
+ console.log(integrationStates[ints], 'integrationStates[ints]')
1370
+
1371
+ if (integrationStates && integrationStates[ints].on.includes('build')) {
1372
+
1373
+ console.log('Starting integration...')
1374
+
1375
+ let int = integrationStates[ints]
1376
+
1377
+ let { name, version, useRuntime, entryPoint, onAction, doOn } = int
1378
+
1379
+ console.log(`Using integration: ${name} v${version} from ${entryPoint}`)
1380
+
1381
+
1382
+
1383
+ Bun.spawn({
1384
+
1385
+ cwd: process.cwd(),
1386
+
1387
+ isVader: true,
1388
+
1389
+ env: {
1390
+
1391
+ PWD: process.cwd(),
1392
+
1393
+ isVader: true,
1394
+
1395
+ FOLDERS: 'pages,src,public',
1396
+
1397
+ onExit: (code) => {
1398
+
1399
+ globalThis.isBuilding = false
1400
+
1401
+ }
1402
+
1403
+ },
1404
+
1405
+ cmd: ["node", entryPoint]
1406
+
1407
+ })
1408
+
1409
+
1410
+
1411
+
1412
+
1413
+
1414
+
1415
+ }
1416
+
1417
+ }
1418
+
1419
+
1420
+
1421
+ break;
1422
+
1423
+ case process.argv.includes('start') && !process.argv.includes('dev') && !process.argv.includes('build'):
1424
+
1425
+ port = process.argv.includes('-p') ? process.argv[process.argv.indexOf('-p') + 1] : config?.host?.prod?.port || 3000
1426
+
1427
+ console.log(`
1428
+
1429
+ Vader.js v1.3.3
1430
+
1431
+ Serving ./dist on port ${port}
1432
+
1433
+ url: http://localhost:${port}
1434
+
1435
+ `)
1436
+
1437
+ globalThis.devMode = false
1438
+
1439
+ globalThis.isProduction = true
1440
+
1441
+
1442
+
1443
+ Server(port)
1444
+
1445
+ break;
1446
+
1447
+ default:
1448
+
1449
+
1450
+
1451
+ break;
1452
+
1453
+
1454
+
1455
+ }