vaderjs 2.0.4 → 2.0.5
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 +63 -126
- package/package.json +1 -1
package/main.js
CHANGED
|
@@ -5,16 +5,8 @@ import { Glob } from 'bun'
|
|
|
5
5
|
const args = Bun.argv.slice(2)
|
|
6
6
|
globalThis.isBuilding = false;
|
|
7
7
|
import fs from 'fs'
|
|
8
|
-
import { platform } from 'os'
|
|
9
8
|
import path from 'path'
|
|
10
9
|
|
|
11
|
-
let bunPath = 'bun'; // Default for Linux/Mac
|
|
12
|
-
if (platform() === 'win32') {
|
|
13
|
-
bunPath = 'bun'; // Bun path for Windows
|
|
14
|
-
} else {
|
|
15
|
-
bunPath = path.resolve(process.env.HOME || process.env.USERPROFILE, '.bun', 'bin', 'bun');
|
|
16
|
-
}
|
|
17
|
-
|
|
18
10
|
|
|
19
11
|
if (!fs.existsSync(process.cwd() + '/app') && !args.includes('init')) {
|
|
20
12
|
console.error(`App directory not found in ${process.cwd()}/app`)
|
|
@@ -29,21 +21,21 @@ if (!fs.existsSync(process.cwd() + '/src')) {
|
|
|
29
21
|
if (!fs.existsSync(process.cwd() + '/vader.config.ts')) {
|
|
30
22
|
fs.writeFileSync(process.cwd() + '/vader.config.ts',
|
|
31
23
|
`import defineConfig from 'vaderjs/config'
|
|
32
|
-
export default
|
|
33
|
-
|
|
34
|
-
|
|
24
|
+
export default defineConfig({
|
|
25
|
+
port: 8080,
|
|
26
|
+
host_provider: 'apache'
|
|
35
27
|
})`)
|
|
36
28
|
}
|
|
37
29
|
var config = require(process.cwd() + '/vader.config.ts').default
|
|
38
30
|
const mode = args.includes('dev') ? 'development' : args.includes('prod') || args.includes('build') ? 'production' : args.includes('init') ? 'init' : args.includes('serve') ? 'serve' : null;
|
|
39
31
|
if (!mode) {
|
|
40
32
|
console.log(`
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
33
|
+
Usage:
|
|
34
|
+
bun vaderjs serve - Start the server
|
|
35
|
+
bun vaderjs dev - Start development server output in dist/
|
|
36
|
+
bun vaderjs prod - Build for production output in dist/
|
|
37
|
+
bun vaderjs init - Initialize a new vaderjs project
|
|
38
|
+
`)
|
|
47
39
|
process.exit(1)
|
|
48
40
|
}
|
|
49
41
|
|
|
@@ -60,11 +52,11 @@ if (mode === 'init') {
|
|
|
60
52
|
|
|
61
53
|
console.log(
|
|
62
54
|
`VaderJS - v${require(process.cwd() + '/node_modules/vaderjs/package.json').version} 🚀
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
55
|
+
Mode: ${mode}
|
|
56
|
+
SSR: ${require(process.cwd() + '/vader.config.ts').default.ssr ? 'Enabled' : 'Disabled'}
|
|
57
|
+
PORT: ${require(process.cwd() + '/vader.config.ts').default.port || 8080}
|
|
58
|
+
${mode == 'serve' ? `SSL: ${require(process.cwd() + '/vader.config.ts').default?.ssl?.enabled ? 'Enabled' : 'Disabled'} ` : ``}
|
|
59
|
+
`
|
|
68
60
|
)
|
|
69
61
|
|
|
70
62
|
|
|
@@ -83,16 +75,15 @@ if (!fs.existsSync(process.cwd() + '/jsconfig.json')) {
|
|
|
83
75
|
await Bun.write(process.cwd() + '/jsconfig.json', JSON.stringify(json, null, 4))
|
|
84
76
|
}
|
|
85
77
|
|
|
86
|
-
|
|
78
|
+
var bindes = []
|
|
87
79
|
var fnmap = []
|
|
88
80
|
const vader = {
|
|
89
|
-
isDev: mode === 'development',
|
|
90
81
|
onFileChange: (file, cb) => {
|
|
91
82
|
fs.watch(file, cb)
|
|
92
83
|
},
|
|
93
84
|
runCommand: (cmd) => {
|
|
94
85
|
return new Promise((resolve, reject) => {
|
|
95
|
-
|
|
86
|
+
Bun.spawn(cmd, {
|
|
96
87
|
stdout: 'inherit',
|
|
97
88
|
cwd: process.cwd(),
|
|
98
89
|
onExit({ exitCode: code }) {
|
|
@@ -104,11 +95,6 @@ const vader = {
|
|
|
104
95
|
}
|
|
105
96
|
})
|
|
106
97
|
|
|
107
|
-
setTimeout(() => {
|
|
108
|
-
c.kill()
|
|
109
|
-
reject()
|
|
110
|
-
}, 5000)
|
|
111
|
-
|
|
112
98
|
|
|
113
99
|
})
|
|
114
100
|
},
|
|
@@ -119,7 +105,6 @@ const vader = {
|
|
|
119
105
|
},
|
|
120
106
|
injectHTML: (html) => {
|
|
121
107
|
bindes.push(html)
|
|
122
|
-
globalThis.bindes = bindes
|
|
123
108
|
},
|
|
124
109
|
}
|
|
125
110
|
const handleReplacements = (code) => {
|
|
@@ -142,10 +127,10 @@ const handleReplacements = (code) => {
|
|
|
142
127
|
url = url.replace(/\\/g, '/')
|
|
143
128
|
if (!bindes.includes(`<link rel="stylesheet" href="${url}">`)) {
|
|
144
129
|
bindes.push(`
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
130
|
+
<style>
|
|
131
|
+
${fs.readFileSync(p, 'utf-8')}
|
|
132
|
+
</style>
|
|
133
|
+
`)
|
|
149
134
|
}
|
|
150
135
|
} catch (error) {
|
|
151
136
|
console.error(error)
|
|
@@ -175,9 +160,8 @@ const handleReplacements = (code) => {
|
|
|
175
160
|
line = b4
|
|
176
161
|
}
|
|
177
162
|
if (!hasImport && line.includes('useRef')) {
|
|
178
|
-
line = line.replace(' ', '')
|
|
179
163
|
let b4 = line
|
|
180
|
-
let key = line.split('
|
|
164
|
+
let key = line.split(' ')[1].split('=')[0]
|
|
181
165
|
b4 = line.replace('useRef(', `this.useRef('${key}',`)
|
|
182
166
|
line = b4
|
|
183
167
|
}
|
|
@@ -188,14 +172,12 @@ const handleReplacements = (code) => {
|
|
|
188
172
|
return c
|
|
189
173
|
}
|
|
190
174
|
|
|
191
|
-
|
|
192
|
-
fs.mkdirSync(process.cwd() + '/dev', { recursive: true })
|
|
193
|
-
fs.copyFileSync(require.resolve('vaderjs/bundler/index.js'), process.cwd() + '/dev/bundler.js')
|
|
194
|
-
}
|
|
175
|
+
|
|
195
176
|
let start = Date.now()
|
|
196
177
|
async function generateApp() {
|
|
197
178
|
globalThis.isBuilding = true;
|
|
198
179
|
console.log(ansiColors.green('Building...'))
|
|
180
|
+
console.log(`Starting build at ${new Date().toLocaleTimeString()}`)
|
|
199
181
|
let plugins = config.plugins || []
|
|
200
182
|
for (let plugin of plugins) {
|
|
201
183
|
if (plugin.onBuildStart) {
|
|
@@ -222,40 +204,21 @@ async function generateApp() {
|
|
|
222
204
|
code = handleReplacements(code)
|
|
223
205
|
let size = code.length / 1024
|
|
224
206
|
r = r.replace(process.cwd().replace(/\\/g, '/') + '/app', '')
|
|
225
|
-
r = r.replace('.jsx', '.js').replace('.tsx', '.js')
|
|
207
|
+
r = r.replace('.jsx', '.js').replace('.tsx', '.js')
|
|
226
208
|
fs.mkdirSync(path.join(process.cwd() + '/dist', path.dirname(r)), { recursive: true })
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
// dont return
|
|
237
|
-
|
|
238
|
-
fs.writeFileSync(
|
|
239
|
-
process.cwd() + '/dist/' + path.dirname(r) + '/' + path.basename(r),
|
|
240
|
-
`
|
|
241
|
-
let route = window.location.pathname.split('/').filter(Boolean)
|
|
242
|
-
let params = {
|
|
243
|
-
// get index tehn do route[index]
|
|
244
|
-
${Object.keys(params).map((param, i) => {
|
|
245
|
-
if (paramIndexes[i] !== -1) {
|
|
246
|
-
var r_copy = r;
|
|
247
|
-
r_copy = r_copy.split('/').filter(Boolean)
|
|
248
|
-
var index = paramIndexes[i] - 1
|
|
249
|
-
return `${param}: route[${index}]`
|
|
250
|
-
}
|
|
251
|
-
}).join(',\n')}
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
\n${code}
|
|
255
|
-
`
|
|
256
|
-
);
|
|
209
|
+
fs.writeFileSync(process.cwd() + '/dist/' + path.dirname(r) + '/' + path.basename(r), `
|
|
210
|
+
let route = window.location.pathname.split('/').filter(v => v !== '')
|
|
211
|
+
let params = {
|
|
212
|
+
${Object.keys(routes.match(route).params || {}).length > 0 ? Object.keys(routes.match(route).params || {}).map(p => {
|
|
213
|
+
return `${p}: route[${Object.keys(routes.match(route).params).indexOf(p) + Object.keys(routes.match(route).params).length}]`
|
|
214
|
+
}).join(',') : ""}
|
|
215
|
+
}
|
|
216
|
+
\n${code}
|
|
217
|
+
`)
|
|
257
218
|
fs.mkdirSync(process.cwd() + '/dev', { recursive: true })
|
|
258
|
-
|
|
219
|
+
if (!fs.existsSync(process.cwd() + '/dev/bundler.js')) {
|
|
220
|
+
fs.copyFileSync(require.resolve('vaderjs/bundler/index.js'), process.cwd() + '/dev/bundler.js')
|
|
221
|
+
}
|
|
259
222
|
|
|
260
223
|
if (!fs.existsSync(process.cwd() + '/dev/readme.md')) {
|
|
261
224
|
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`)
|
|
@@ -265,14 +228,14 @@ async function generateApp() {
|
|
|
265
228
|
loader: 'ts',
|
|
266
229
|
}).transformSync(await Bun.file(require.resolve('vaderjs')).text()))
|
|
267
230
|
Bun.spawn({
|
|
268
|
-
cmd: [
|
|
231
|
+
cmd: ['bun', 'run', './dev/bundler.js'],
|
|
269
232
|
cwd: process.cwd(),
|
|
270
233
|
stdout: 'inherit',
|
|
271
234
|
env: {
|
|
272
|
-
ENTRYPOINT: path.join(process.cwd()
|
|
273
|
-
ROOT:
|
|
235
|
+
ENTRYPOINT: path.join(process.cwd() + '/dist/' + path.dirname(r) + '/' + path.basename(r)),
|
|
236
|
+
ROOT: process.cwd() + '/app/',
|
|
274
237
|
OUT: path.dirname(r),
|
|
275
|
-
file:
|
|
238
|
+
file: process.cwd() + '/dist/' + path.dirname(r) + '/' + path.basename(r),
|
|
276
239
|
DEV: mode === 'development',
|
|
277
240
|
size,
|
|
278
241
|
bindes: bindes.join('\n'),
|
|
@@ -283,13 +246,15 @@ async function generateApp() {
|
|
|
283
246
|
},
|
|
284
247
|
onExit({ exitCode: code }) {
|
|
285
248
|
if (code === 0) {
|
|
286
|
-
bindes = []
|
|
287
|
-
|
|
249
|
+
bindes = []
|
|
250
|
+
console.log(`Built ${r} in ${Date.now() - start}ms`)
|
|
251
|
+
resolve()
|
|
288
252
|
} else {
|
|
289
|
-
reject()
|
|
253
|
+
reject()
|
|
290
254
|
}
|
|
291
|
-
}
|
|
292
|
-
})
|
|
255
|
+
}
|
|
256
|
+
})
|
|
257
|
+
|
|
293
258
|
})
|
|
294
259
|
|
|
295
260
|
switch (host_provider) {
|
|
@@ -329,7 +294,6 @@ async function generateApp() {
|
|
|
329
294
|
await plugin.onBuildFinish(vader)
|
|
330
295
|
}
|
|
331
296
|
}
|
|
332
|
-
|
|
333
297
|
})
|
|
334
298
|
|
|
335
299
|
|
|
@@ -338,6 +302,7 @@ async function generateApp() {
|
|
|
338
302
|
function handleFiles() {
|
|
339
303
|
return new Promise(async (resolve, reject) => {
|
|
340
304
|
try {
|
|
305
|
+
console.log(Glob)
|
|
341
306
|
let glob = new Glob('public/**/*')
|
|
342
307
|
for await (var i of glob.scan()) {
|
|
343
308
|
let file = i
|
|
@@ -360,7 +325,7 @@ function handleFiles() {
|
|
|
360
325
|
file = file.replace('.jsx', '.js').replace('.tsx', '.js')
|
|
361
326
|
fs.writeFileSync(path.join(process.cwd() + '/dist', file.replace('.jsx', '.js').replace('.tsx', '.js')), code)
|
|
362
327
|
await Bun.spawn({
|
|
363
|
-
cmd: [
|
|
328
|
+
cmd: ['bun', 'run', './dev/bundler.js'],
|
|
364
329
|
cwd: process.cwd(),
|
|
365
330
|
stdout: 'inherit',
|
|
366
331
|
env: {
|
|
@@ -389,7 +354,7 @@ function handleFiles() {
|
|
|
389
354
|
file = file.replace('.ts', '.js')
|
|
390
355
|
fs.writeFileSync(path.join(process.cwd() + '/dist', file.replace('.ts', '.js')), code)
|
|
391
356
|
await Bun.spawn({
|
|
392
|
-
cmd: [
|
|
357
|
+
cmd: ['bun', 'run', './dev/bundler.js'],
|
|
393
358
|
cwd: process.cwd(),
|
|
394
359
|
stdout: 'inherit',
|
|
395
360
|
env: {
|
|
@@ -435,10 +400,10 @@ if (mode === 'development') {
|
|
|
435
400
|
// Function to handle file changes with debounce
|
|
436
401
|
const handleFileChangeDebounced = async (change, file) => {
|
|
437
402
|
if (file.endsWith('.tsx') || file.endsWith('.jsx') || file.endsWith('.css') || file.endsWith('.ts')
|
|
438
|
-
|
|
403
|
+
&& !file.includes('node_module')
|
|
439
404
|
) {
|
|
440
405
|
// delete files cache
|
|
441
|
-
if (file.endsWith('vader.config.ts'))
|
|
406
|
+
if (file.endsWith('vader.config.ts')){
|
|
442
407
|
delete require.cache[require.resolve(process.cwd() + '/vader.config.ts')]
|
|
443
408
|
|
|
444
409
|
config = require(process.cwd() + '/vader.config.ts').default
|
|
@@ -479,14 +444,13 @@ if (mode === 'development') {
|
|
|
479
444
|
else if (mode == 'production') {
|
|
480
445
|
await handleFiles()
|
|
481
446
|
await generateApp()
|
|
482
|
-
|
|
483
|
-
console.log(`Build complete in ${Date.now() - start}ms at ${new Date().toLocaleTimeString()}`);
|
|
484
447
|
}
|
|
485
448
|
else {
|
|
486
449
|
if (isBuilding) console.log(`Build complete in ${Date.now() - start}ms at ${new Date().toLocaleTimeString()}`);
|
|
487
450
|
|
|
488
451
|
}
|
|
489
452
|
|
|
453
|
+
|
|
490
454
|
if (mode == 'development' || mode == 'serve') {
|
|
491
455
|
let server = Bun.serve({
|
|
492
456
|
port: port || 8080,
|
|
@@ -537,46 +501,19 @@ if (mode == 'development' || mode == 'serve') {
|
|
|
537
501
|
base = base.replace(path.join(process.cwd() + '/app').replace(/\\/g, '/'), '')
|
|
538
502
|
base = base.replace(/\\/g, '/').replace('/app', '/dist')
|
|
539
503
|
base = process.cwd() + "/dist/" + base
|
|
540
|
-
if (!fs.existsSync(path.join(base, 'index.html'))) {
|
|
541
|
-
return new Response(`
|
|
542
|
-
<html>
|
|
543
|
-
<head>
|
|
544
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
545
|
-
<meta http-equiv="refresh" content="5">
|
|
546
|
-
</head>
|
|
547
|
-
<body>
|
|
548
|
-
<p>Rerouting to display changes from server</p>
|
|
549
|
-
</body>
|
|
550
|
-
`, {
|
|
551
|
-
headers: {
|
|
552
|
-
'Content-Type': 'text/html',
|
|
553
|
-
'Cache-Control': 'no-cache'
|
|
554
|
-
}
|
|
555
|
-
})
|
|
556
|
-
}
|
|
557
504
|
let data = await Bun.file(path.join(base, 'index.html')).text()
|
|
558
505
|
if (mode == "development") {
|
|
559
506
|
return new Response(data + `
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
}
|
|
571
|
-
|
|
572
|
-
ws.onclose = () => {
|
|
573
|
-
// try to reconnect
|
|
574
|
-
console.log('Reconnecting to hmr server')
|
|
575
|
-
ws = new WebSocket(\`\${location.protocol === 'https:' ? 'wss' : 'ws'}://\${location.host}\`)
|
|
576
|
-
}
|
|
577
|
-
|
|
578
|
-
</script>
|
|
579
|
-
`, {
|
|
507
|
+
<script>
|
|
508
|
+
let ws = new WebSocket('ws://localhost:${server.port}')
|
|
509
|
+
ws.onmessage = (e) => {
|
|
510
|
+
if(e.data === 'reload'){
|
|
511
|
+
console.log('Reloading to display changes from server')
|
|
512
|
+
window.location.reload()
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
</script>
|
|
516
|
+
`, {
|
|
580
517
|
headers: {
|
|
581
518
|
'Content-Type': 'text/html'
|
|
582
519
|
}
|