vaderjs 2.0.6 → 2.0.7
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.
- package/main.js +120 -56
- package/package.json +1 -1
package/main.js
CHANGED
|
@@ -4,9 +4,18 @@ import ansiColors from 'ansi-colors'
|
|
|
4
4
|
import { Glob } from 'bun'
|
|
5
5
|
const args = Bun.argv.slice(2)
|
|
6
6
|
globalThis.isBuilding = false;
|
|
7
|
-
import fs from 'fs'
|
|
7
|
+
import fs from 'fs'
|
|
8
|
+
import { platform } from 'os'
|
|
8
9
|
import path from 'path'
|
|
9
10
|
|
|
11
|
+
let bunPath = 'bun'; // Default for Linux/Mac
|
|
12
|
+
|
|
13
|
+
if (platform() === 'win32') {
|
|
14
|
+
bunPath ='bun'; // Bun path for Windows
|
|
15
|
+
} else {
|
|
16
|
+
bunPath = path.resolve(process.env.HOME || process.env.USERPROFILE, '.bun', 'bin', 'bun');
|
|
17
|
+
}
|
|
18
|
+
|
|
10
19
|
|
|
11
20
|
if (!fs.existsSync(process.cwd() + '/app') && !args.includes('init')) {
|
|
12
21
|
console.error(`App directory not found in ${process.cwd()}/app`)
|
|
@@ -75,15 +84,16 @@ if (!fs.existsSync(process.cwd() + '/jsconfig.json')) {
|
|
|
75
84
|
await Bun.write(process.cwd() + '/jsconfig.json', JSON.stringify(json, null, 4))
|
|
76
85
|
}
|
|
77
86
|
|
|
78
|
-
|
|
87
|
+
globalThis.bindes = []
|
|
79
88
|
var fnmap = []
|
|
80
89
|
const vader = {
|
|
90
|
+
isDev: mode === 'development',
|
|
81
91
|
onFileChange: (file, cb) => {
|
|
82
92
|
fs.watch(file, cb)
|
|
83
93
|
},
|
|
84
94
|
runCommand: (cmd) => {
|
|
85
95
|
return new Promise((resolve, reject) => {
|
|
86
|
-
|
|
96
|
+
let c = Bun.spawn(cmd, {
|
|
87
97
|
stdout: 'inherit',
|
|
88
98
|
cwd: process.cwd(),
|
|
89
99
|
onExit({ exitCode: code }) {
|
|
@@ -95,6 +105,11 @@ const vader = {
|
|
|
95
105
|
}
|
|
96
106
|
})
|
|
97
107
|
|
|
108
|
+
setTimeout(() => {
|
|
109
|
+
c.kill()
|
|
110
|
+
reject()
|
|
111
|
+
}, 5000)
|
|
112
|
+
|
|
98
113
|
|
|
99
114
|
})
|
|
100
115
|
},
|
|
@@ -105,6 +120,7 @@ const vader = {
|
|
|
105
120
|
},
|
|
106
121
|
injectHTML: (html) => {
|
|
107
122
|
bindes.push(html)
|
|
123
|
+
globalThis.bindes = bindes
|
|
108
124
|
},
|
|
109
125
|
}
|
|
110
126
|
const handleReplacements = (code) => {
|
|
@@ -160,8 +176,9 @@ const handleReplacements = (code) => {
|
|
|
160
176
|
line = b4
|
|
161
177
|
}
|
|
162
178
|
if (!hasImport && line.includes('useRef')) {
|
|
179
|
+
line = line.replace(' ', '')
|
|
163
180
|
let b4 = line
|
|
164
|
-
let key = line.split('
|
|
181
|
+
let key = line.split('=')[0].split(' ').filter(Boolean)[1]
|
|
165
182
|
b4 = line.replace('useRef(', `this.useRef('${key}',`)
|
|
166
183
|
line = b4
|
|
167
184
|
}
|
|
@@ -172,12 +189,14 @@ const handleReplacements = (code) => {
|
|
|
172
189
|
return c
|
|
173
190
|
}
|
|
174
191
|
|
|
175
|
-
|
|
192
|
+
if (!fs.existsSync(process.cwd() + '/dev/bundler.js')) {
|
|
193
|
+
fs.mkdirSync(process.cwd() + '/dev', { recursive: true })
|
|
194
|
+
fs.copyFileSync(require.resolve('vaderjs/bundler/index.js'), process.cwd() + '/dev/bundler.js')
|
|
195
|
+
}
|
|
176
196
|
let start = Date.now()
|
|
177
197
|
async function generateApp() {
|
|
178
198
|
globalThis.isBuilding = true;
|
|
179
|
-
console.log(ansiColors.green('Building...'))
|
|
180
|
-
console.log(`Starting build at ${new Date().toLocaleTimeString()}`)
|
|
199
|
+
console.log(ansiColors.green('Building...'))
|
|
181
200
|
let plugins = config.plugins || []
|
|
182
201
|
for (let plugin of plugins) {
|
|
183
202
|
if (plugin.onBuildStart) {
|
|
@@ -201,24 +220,43 @@ async function generateApp() {
|
|
|
201
220
|
|
|
202
221
|
let r = routes.routes[route]
|
|
203
222
|
let code = await Bun.file(r).text()
|
|
223
|
+
code = handleReplacements(code)
|
|
204
224
|
let size = code.length / 1024
|
|
205
225
|
r = r.replace(process.cwd().replace(/\\/g, '/') + '/app', '')
|
|
206
|
-
var beforeR = r
|
|
207
226
|
r = r.replace('.jsx', '.js').replace('.tsx', '.js')
|
|
208
227
|
fs.mkdirSync(path.join(process.cwd() + '/dist', path.dirname(r)), { recursive: true })
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
228
|
+
let params = routes.match(route).params || {}
|
|
229
|
+
let base = routes.match(route)
|
|
230
|
+
let paramIndexes = []
|
|
231
|
+
for (let param in params) {
|
|
232
|
+
let routes = base.pathname.split('/')
|
|
233
|
+
let index = routes.indexOf('[' + param + ']')
|
|
234
|
+
paramIndexes.push(index)
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// dont return
|
|
238
|
+
|
|
239
|
+
fs.writeFileSync(
|
|
240
|
+
process.cwd() + '/dist/' + path.dirname(r) + '/' + path.basename(r),
|
|
241
|
+
`
|
|
242
|
+
let route = window.location.pathname.split('/').filter(Boolean)
|
|
243
|
+
let params = {
|
|
244
|
+
// get index tehn do route[index]
|
|
245
|
+
${Object.keys(params).map((param, i) => {
|
|
246
|
+
if (paramIndexes[i] !== -1) {
|
|
247
|
+
var r_copy = r;
|
|
248
|
+
r_copy = r_copy.split('/').filter(Boolean)
|
|
249
|
+
var index = paramIndexes[i] - 1
|
|
250
|
+
return `${param}: route[${index}]`
|
|
251
|
+
}
|
|
252
|
+
}).join(',\n')}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
\n${code}
|
|
256
|
+
`
|
|
257
|
+
);
|
|
218
258
|
fs.mkdirSync(process.cwd() + '/dev', { recursive: true })
|
|
219
|
-
|
|
220
|
-
fs.copyFileSync(require.resolve('vaderjs/bundler/index.js'), process.cwd() + '/dev/bundler.js')
|
|
221
|
-
}
|
|
259
|
+
|
|
222
260
|
|
|
223
261
|
if (!fs.existsSync(process.cwd() + '/dev/readme.md')) {
|
|
224
262
|
fs.writeFileSync(process.cwd() + '/dev/readme.md', `# Please do not edit the bundler.js file in the dev directory. This file is automatically generated by the bundler. \n\n`)
|
|
@@ -227,37 +265,32 @@ async function generateApp() {
|
|
|
227
265
|
fs.writeFileSync(process.cwd() + '/dist/src/vader/index.js', await new Bun.Transpiler({
|
|
228
266
|
loader: 'ts',
|
|
229
267
|
}).transformSync(await Bun.file(require.resolve('vaderjs')).text()))
|
|
230
|
-
|
|
231
268
|
Bun.spawn({
|
|
232
|
-
cmd: [
|
|
269
|
+
cmd: [bunPath, 'run', './dev/bundler.js'] ,
|
|
233
270
|
cwd: process.cwd(),
|
|
234
271
|
stdout: 'inherit',
|
|
235
272
|
env: {
|
|
236
|
-
ENTRYPOINT: path.join(process.cwd()
|
|
237
|
-
ROOT: process.cwd()
|
|
273
|
+
ENTRYPOINT: path.join(process.cwd(), 'dist', path.dirname(r), path.basename(r)),
|
|
274
|
+
ROOT: path.join(process.cwd(), 'app/'),
|
|
238
275
|
OUT: path.dirname(r),
|
|
239
|
-
file: process.cwd()
|
|
276
|
+
file: path.join(process.cwd(), 'dist', path.dirname(r), path.basename(r)),
|
|
240
277
|
DEV: mode === 'development',
|
|
241
278
|
size,
|
|
242
279
|
bindes: bindes.join('\n'),
|
|
243
|
-
isTs: beforeR.endsWith(".tsx"),
|
|
244
280
|
filePath: r,
|
|
245
|
-
|
|
246
|
-
isJsx: beforeR.endsWith('.tsx') || beforeR.endsWith(".jsx"),
|
|
247
281
|
isAppFile: true,
|
|
248
|
-
|
|
282
|
+
isJsx: true,
|
|
283
|
+
INPUT: `../app/${r.replace('.js', '.jsx').replace('.tsx', '.js')}`,
|
|
249
284
|
},
|
|
250
285
|
onExit({ exitCode: code }) {
|
|
251
286
|
if (code === 0) {
|
|
252
|
-
bindes = []
|
|
253
|
-
|
|
254
|
-
resolve()
|
|
287
|
+
bindes = [];
|
|
288
|
+
resolve();
|
|
255
289
|
} else {
|
|
256
|
-
reject()
|
|
290
|
+
reject();
|
|
257
291
|
}
|
|
258
|
-
}
|
|
259
|
-
})
|
|
260
|
-
|
|
292
|
+
},
|
|
293
|
+
});
|
|
261
294
|
})
|
|
262
295
|
|
|
263
296
|
switch (host_provider) {
|
|
@@ -297,6 +330,7 @@ async function generateApp() {
|
|
|
297
330
|
await plugin.onBuildFinish(vader)
|
|
298
331
|
}
|
|
299
332
|
}
|
|
333
|
+
|
|
300
334
|
})
|
|
301
335
|
|
|
302
336
|
|
|
@@ -304,7 +338,7 @@ async function generateApp() {
|
|
|
304
338
|
|
|
305
339
|
function handleFiles() {
|
|
306
340
|
return new Promise(async (resolve, reject) => {
|
|
307
|
-
try {
|
|
341
|
+
try {
|
|
308
342
|
let glob = new Glob('public/**/*')
|
|
309
343
|
for await (var i of glob.scan()) {
|
|
310
344
|
let file = i
|
|
@@ -319,15 +353,15 @@ function handleFiles() {
|
|
|
319
353
|
var file = i
|
|
320
354
|
fs.mkdirSync(path.join(process.cwd() + '/dist', path.dirname(file)), { recursive: true })
|
|
321
355
|
// turn jsx to js
|
|
322
|
-
if (file.
|
|
356
|
+
if (file.includes('.jsx') || file.includes('.tsx')) {
|
|
323
357
|
let code = await Bun.file(file).text()
|
|
324
358
|
|
|
325
359
|
code = handleReplacements(code)
|
|
326
|
-
|
|
360
|
+
|
|
327
361
|
file = file.replace('.jsx', '.js').replace('.tsx', '.js')
|
|
328
362
|
fs.writeFileSync(path.join(process.cwd() + '/dist', file.replace('.jsx', '.js').replace('.tsx', '.js')), code)
|
|
329
363
|
await Bun.spawn({
|
|
330
|
-
cmd: [
|
|
364
|
+
cmd: [bunPath, 'run', './dev/bundler.js'],
|
|
331
365
|
cwd: process.cwd(),
|
|
332
366
|
stdout: 'inherit',
|
|
333
367
|
env: {
|
|
@@ -339,9 +373,8 @@ function handleFiles() {
|
|
|
339
373
|
DEV: mode === 'development',
|
|
340
374
|
size: code.length / 1024,
|
|
341
375
|
filePath: file.replace('.jsx', '.js'),
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
INPUT: path.join(process.cwd(), url),
|
|
376
|
+
isTs: file.includes('.tsx'),
|
|
377
|
+
INPUT: path.join(process.cwd(), file.replace('.js', '.jsx').replace('.tsx', '.js')),
|
|
345
378
|
},
|
|
346
379
|
onExit({ exitCode: code }) {
|
|
347
380
|
if (code === 0) {
|
|
@@ -351,13 +384,13 @@ function handleFiles() {
|
|
|
351
384
|
}
|
|
352
385
|
}
|
|
353
386
|
})
|
|
354
|
-
} else if (file.
|
|
387
|
+
} else if (file.includes('.ts')) {
|
|
355
388
|
let code = await Bun.file(file).text()
|
|
356
389
|
code = handleReplacements(code)
|
|
357
390
|
file = file.replace('.ts', '.js')
|
|
358
391
|
fs.writeFileSync(path.join(process.cwd() + '/dist', file.replace('.ts', '.js')), code)
|
|
359
392
|
await Bun.spawn({
|
|
360
|
-
cmd: [
|
|
393
|
+
cmd: [bunPath, 'run', './dev/bundler.js'],
|
|
361
394
|
cwd: process.cwd(),
|
|
362
395
|
stdout: 'inherit',
|
|
363
396
|
env: {
|
|
@@ -392,8 +425,8 @@ function handleFiles() {
|
|
|
392
425
|
globalThis.clients = []
|
|
393
426
|
|
|
394
427
|
if (mode === 'development') {
|
|
395
|
-
await handleFiles()
|
|
396
428
|
await generateApp()
|
|
429
|
+
await handleFiles()
|
|
397
430
|
const watcher = fs.watch(path.join(process.cwd() + '/'), { recursive: true })
|
|
398
431
|
let isBuilding = false; // Flag to track build status
|
|
399
432
|
|
|
@@ -403,10 +436,10 @@ if (mode === 'development') {
|
|
|
403
436
|
// Function to handle file changes with debounce
|
|
404
437
|
const handleFileChangeDebounced = async (change, file) => {
|
|
405
438
|
if (file.endsWith('.tsx') || file.endsWith('.jsx') || file.endsWith('.css') || file.endsWith('.ts')
|
|
406
|
-
|
|
439
|
+
&& !file.includes('node_module')
|
|
407
440
|
) {
|
|
408
441
|
// delete files cache
|
|
409
|
-
if (file.endsWith('vader.config.ts'))
|
|
442
|
+
if (file.endsWith('vader.config.ts')){
|
|
410
443
|
delete require.cache[require.resolve(process.cwd() + '/vader.config.ts')]
|
|
411
444
|
|
|
412
445
|
config = require(process.cwd() + '/vader.config.ts').default
|
|
@@ -447,13 +480,14 @@ if (mode === 'development') {
|
|
|
447
480
|
else if (mode == 'production') {
|
|
448
481
|
await handleFiles()
|
|
449
482
|
await generateApp()
|
|
483
|
+
|
|
484
|
+
console.log(`Build complete in ${Date.now() - start}ms at ${new Date().toLocaleTimeString()}`);
|
|
450
485
|
}
|
|
451
486
|
else {
|
|
452
487
|
if (isBuilding) console.log(`Build complete in ${Date.now() - start}ms at ${new Date().toLocaleTimeString()}`);
|
|
453
|
-
|
|
488
|
+
|
|
454
489
|
}
|
|
455
490
|
|
|
456
|
-
|
|
457
491
|
if (mode == 'development' || mode == 'serve') {
|
|
458
492
|
let server = Bun.serve({
|
|
459
493
|
port: port || 8080,
|
|
@@ -494,27 +528,54 @@ if (mode == 'development' || mode == 'serve') {
|
|
|
494
528
|
style: 'nextjs'
|
|
495
529
|
})
|
|
496
530
|
router.reload()
|
|
497
|
-
let route = router.match(url.pathname)
|
|
531
|
+
let route = router.match(url.pathname)
|
|
498
532
|
if (!route) {
|
|
499
533
|
return new Response('Not found', { status: 404 })
|
|
500
534
|
}
|
|
501
535
|
let p = route.pathname;
|
|
502
536
|
let base = path.dirname(route.filePath)
|
|
503
537
|
base = base.replace(/\\/g, '/')
|
|
504
|
-
base = base.replace(path.join(process.cwd() + '/app').replace(/\\/g, '/'), '')
|
|
505
|
-
base = base.replace(/\\/g, '/').replace(
|
|
506
|
-
base = process.cwd() +
|
|
538
|
+
base = base.replace(path.join(process.cwd() + '/app').replace(/\\/g, '/'), '')
|
|
539
|
+
base = base.replace(/\\/g, '/').replace('/app', '/dist')
|
|
540
|
+
base = process.cwd() + "/dist/" + base
|
|
541
|
+
if(!fs.existsSync(path.join(base, 'index.html'))){
|
|
542
|
+
return new Response(`
|
|
543
|
+
<html>
|
|
544
|
+
<head>
|
|
545
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
546
|
+
<meta http-equiv="refresh" content="5">
|
|
547
|
+
</head>
|
|
548
|
+
<body>
|
|
549
|
+
<p>Rerouting to display changes from server</p>
|
|
550
|
+
</body>
|
|
551
|
+
`, {
|
|
552
|
+
headers: {
|
|
553
|
+
'Content-Type': 'text/html',
|
|
554
|
+
'Cache-Control': 'no-cache'
|
|
555
|
+
}
|
|
556
|
+
})
|
|
557
|
+
}
|
|
507
558
|
let data = await Bun.file(path.join(base, 'index.html')).text()
|
|
508
559
|
if (mode == "development") {
|
|
509
560
|
return new Response(data + `
|
|
510
561
|
<script>
|
|
511
|
-
let ws = new WebSocket('ws
|
|
562
|
+
let ws = new WebSocket(\`\${location.protocol === 'https:' ? 'wss' : 'ws'}://\${location.host}\`)
|
|
512
563
|
ws.onmessage = (e) => {
|
|
513
564
|
if(e.data === 'reload'){
|
|
514
565
|
console.log('Reloading to display changes from server')
|
|
515
566
|
window.location.reload()
|
|
516
567
|
}
|
|
517
568
|
}
|
|
569
|
+
ws.onopen = () => {
|
|
570
|
+
console.log('Connected to hmr server')
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
ws.onclose = () => {
|
|
574
|
+
// try to reconnect
|
|
575
|
+
console.log('Reconnecting to hmr server')
|
|
576
|
+
ws = new WebSocket(\`\${location.protocol === 'https:' ? 'wss' : 'ws'}://\${location.host}\`)
|
|
577
|
+
}
|
|
578
|
+
|
|
518
579
|
</script>
|
|
519
580
|
`, {
|
|
520
581
|
headers: {
|
|
@@ -533,4 +594,7 @@ if (mode == 'development' || mode == 'serve') {
|
|
|
533
594
|
})
|
|
534
595
|
|
|
535
596
|
console.log(ansiColors.green('Server started at http://localhost:' + port || 8080))
|
|
536
|
-
}
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
|
|
600
|
+
|