vaderjs 2.0.3 → 2.0.4
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 +100 -96
- package/package.json +1 -1
package/main.js
CHANGED
|
@@ -5,8 +5,16 @@ 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'
|
|
8
9
|
import path from 'path'
|
|
9
10
|
|
|
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
|
+
|
|
10
18
|
|
|
11
19
|
if (!fs.existsSync(process.cwd() + '/app') && !args.includes('init')) {
|
|
12
20
|
console.error(`App directory not found in ${process.cwd()}/app`)
|
|
@@ -21,21 +29,21 @@ if (!fs.existsSync(process.cwd() + '/src')) {
|
|
|
21
29
|
if (!fs.existsSync(process.cwd() + '/vader.config.ts')) {
|
|
22
30
|
fs.writeFileSync(process.cwd() + '/vader.config.ts',
|
|
23
31
|
`import defineConfig from 'vaderjs/config'
|
|
24
|
-
export default
|
|
25
|
-
|
|
26
|
-
|
|
32
|
+
export default defineConfig({
|
|
33
|
+
port: 8080,
|
|
34
|
+
host_provider: 'apache'
|
|
27
35
|
})`)
|
|
28
36
|
}
|
|
29
37
|
var config = require(process.cwd() + '/vader.config.ts').default
|
|
30
38
|
const mode = args.includes('dev') ? 'development' : args.includes('prod') || args.includes('build') ? 'production' : args.includes('init') ? 'init' : args.includes('serve') ? 'serve' : null;
|
|
31
39
|
if (!mode) {
|
|
32
40
|
console.log(`
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
41
|
+
Usage:
|
|
42
|
+
bun vaderjs serve - Start the server
|
|
43
|
+
bun vaderjs dev - Start development server output in dist/
|
|
44
|
+
bun vaderjs prod - Build for production output in dist/
|
|
45
|
+
bun vaderjs init - Initialize a new vaderjs project
|
|
46
|
+
`)
|
|
39
47
|
process.exit(1)
|
|
40
48
|
}
|
|
41
49
|
|
|
@@ -52,11 +60,11 @@ if (mode === 'init') {
|
|
|
52
60
|
|
|
53
61
|
console.log(
|
|
54
62
|
`VaderJS - v${require(process.cwd() + '/node_modules/vaderjs/package.json').version} 🚀
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
63
|
+
Mode: ${mode}
|
|
64
|
+
SSR: ${require(process.cwd() + '/vader.config.ts').default.ssr ? 'Enabled' : 'Disabled'}
|
|
65
|
+
PORT: ${require(process.cwd() + '/vader.config.ts').default.port || 8080}
|
|
66
|
+
${mode == 'serve' ? `SSL: ${require(process.cwd() + '/vader.config.ts').default?.ssl?.enabled ? 'Enabled' : 'Disabled'} ` : ``}
|
|
67
|
+
`
|
|
60
68
|
)
|
|
61
69
|
|
|
62
70
|
|
|
@@ -84,7 +92,7 @@ const vader = {
|
|
|
84
92
|
},
|
|
85
93
|
runCommand: (cmd) => {
|
|
86
94
|
return new Promise((resolve, reject) => {
|
|
87
|
-
|
|
95
|
+
let c = Bun.spawn(cmd, {
|
|
88
96
|
stdout: 'inherit',
|
|
89
97
|
cwd: process.cwd(),
|
|
90
98
|
onExit({ exitCode: code }) {
|
|
@@ -134,10 +142,10 @@ const handleReplacements = (code) => {
|
|
|
134
142
|
url = url.replace(/\\/g, '/')
|
|
135
143
|
if (!bindes.includes(`<link rel="stylesheet" href="${url}">`)) {
|
|
136
144
|
bindes.push(`
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
145
|
+
<style>
|
|
146
|
+
${fs.readFileSync(p, 'utf-8')}
|
|
147
|
+
</style>
|
|
148
|
+
`)
|
|
141
149
|
}
|
|
142
150
|
} catch (error) {
|
|
143
151
|
console.error(error)
|
|
@@ -169,7 +177,7 @@ const handleReplacements = (code) => {
|
|
|
169
177
|
if (!hasImport && line.includes('useRef')) {
|
|
170
178
|
line = line.replace(' ', '')
|
|
171
179
|
let b4 = line
|
|
172
|
-
let key = line.split('=')[0].split(' ').filter(Boolean)[1]
|
|
180
|
+
let key = line.split('=')[0].split(' ').filter(Boolean)[1]
|
|
173
181
|
b4 = line.replace('useRef(', `this.useRef('${key}',`)
|
|
174
182
|
line = b4
|
|
175
183
|
}
|
|
@@ -180,14 +188,14 @@ const handleReplacements = (code) => {
|
|
|
180
188
|
return c
|
|
181
189
|
}
|
|
182
190
|
|
|
183
|
-
if (!fs.existsSync(process.cwd() + '/dev/bundler.js')) {
|
|
191
|
+
if (!fs.existsSync(process.cwd() + '/dev/bundler.js')) {
|
|
184
192
|
fs.mkdirSync(process.cwd() + '/dev', { recursive: true })
|
|
185
193
|
fs.copyFileSync(require.resolve('vaderjs/bundler/index.js'), process.cwd() + '/dev/bundler.js')
|
|
186
194
|
}
|
|
187
195
|
let start = Date.now()
|
|
188
196
|
async function generateApp() {
|
|
189
197
|
globalThis.isBuilding = true;
|
|
190
|
-
console.log(ansiColors.green('Building...'))
|
|
198
|
+
console.log(ansiColors.green('Building...'))
|
|
191
199
|
let plugins = config.plugins || []
|
|
192
200
|
for (let plugin of plugins) {
|
|
193
201
|
if (plugin.onBuildStart) {
|
|
@@ -214,40 +222,40 @@ async function generateApp() {
|
|
|
214
222
|
code = handleReplacements(code)
|
|
215
223
|
let size = code.length / 1024
|
|
216
224
|
r = r.replace(process.cwd().replace(/\\/g, '/') + '/app', '')
|
|
217
|
-
r = r.replace('.jsx', '.js').replace('.tsx', '.js')
|
|
225
|
+
r = r.replace('.jsx', '.js').replace('.tsx', '.js')
|
|
218
226
|
fs.mkdirSync(path.join(process.cwd() + '/dist', path.dirname(r)), { recursive: true })
|
|
219
227
|
let params = routes.match(route).params || {}
|
|
220
|
-
let base = routes.match(route)
|
|
221
|
-
let paramIndexes = []
|
|
228
|
+
let base = routes.match(route)
|
|
229
|
+
let paramIndexes = []
|
|
222
230
|
for (let param in params) {
|
|
223
|
-
let
|
|
231
|
+
let routes = base.pathname.split('/')
|
|
224
232
|
let index = routes.indexOf('[' + param + ']')
|
|
225
233
|
paramIndexes.push(index)
|
|
226
|
-
}
|
|
227
|
-
|
|
234
|
+
}
|
|
235
|
+
|
|
228
236
|
// dont return
|
|
229
|
-
|
|
237
|
+
|
|
230
238
|
fs.writeFileSync(
|
|
231
239
|
process.cwd() + '/dist/' + path.dirname(r) + '/' + path.basename(r),
|
|
232
240
|
`
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
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
|
+
`
|
|
248
256
|
);
|
|
249
257
|
fs.mkdirSync(process.cwd() + '/dev', { recursive: true })
|
|
250
|
-
|
|
258
|
+
|
|
251
259
|
|
|
252
260
|
if (!fs.existsSync(process.cwd() + '/dev/readme.md')) {
|
|
253
261
|
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`)
|
|
@@ -257,14 +265,14 @@ async function generateApp() {
|
|
|
257
265
|
loader: 'ts',
|
|
258
266
|
}).transformSync(await Bun.file(require.resolve('vaderjs')).text()))
|
|
259
267
|
Bun.spawn({
|
|
260
|
-
cmd: [
|
|
268
|
+
cmd: [bunPath, 'run', './dev/bundler.js'],
|
|
261
269
|
cwd: process.cwd(),
|
|
262
270
|
stdout: 'inherit',
|
|
263
271
|
env: {
|
|
264
|
-
ENTRYPOINT: path.join(process.cwd()
|
|
265
|
-
ROOT: process.cwd()
|
|
272
|
+
ENTRYPOINT: path.join(process.cwd(), 'dist', path.dirname(r), path.basename(r)),
|
|
273
|
+
ROOT: path.join(process.cwd(), 'app/'),
|
|
266
274
|
OUT: path.dirname(r),
|
|
267
|
-
file: process.cwd()
|
|
275
|
+
file: path.join(process.cwd(), 'dist', path.dirname(r), path.basename(r)),
|
|
268
276
|
DEV: mode === 'development',
|
|
269
277
|
size,
|
|
270
278
|
bindes: bindes.join('\n'),
|
|
@@ -275,14 +283,13 @@ async function generateApp() {
|
|
|
275
283
|
},
|
|
276
284
|
onExit({ exitCode: code }) {
|
|
277
285
|
if (code === 0) {
|
|
278
|
-
bindes = []
|
|
279
|
-
resolve()
|
|
286
|
+
bindes = [];
|
|
287
|
+
resolve();
|
|
280
288
|
} else {
|
|
281
|
-
reject()
|
|
289
|
+
reject();
|
|
282
290
|
}
|
|
283
|
-
}
|
|
284
|
-
})
|
|
285
|
-
|
|
291
|
+
},
|
|
292
|
+
});
|
|
286
293
|
})
|
|
287
294
|
|
|
288
295
|
switch (host_provider) {
|
|
@@ -322,7 +329,7 @@ async function generateApp() {
|
|
|
322
329
|
await plugin.onBuildFinish(vader)
|
|
323
330
|
}
|
|
324
331
|
}
|
|
325
|
-
|
|
332
|
+
|
|
326
333
|
})
|
|
327
334
|
|
|
328
335
|
|
|
@@ -330,7 +337,7 @@ async function generateApp() {
|
|
|
330
337
|
|
|
331
338
|
function handleFiles() {
|
|
332
339
|
return new Promise(async (resolve, reject) => {
|
|
333
|
-
try {
|
|
340
|
+
try {
|
|
334
341
|
let glob = new Glob('public/**/*')
|
|
335
342
|
for await (var i of glob.scan()) {
|
|
336
343
|
let file = i
|
|
@@ -353,7 +360,7 @@ function handleFiles() {
|
|
|
353
360
|
file = file.replace('.jsx', '.js').replace('.tsx', '.js')
|
|
354
361
|
fs.writeFileSync(path.join(process.cwd() + '/dist', file.replace('.jsx', '.js').replace('.tsx', '.js')), code)
|
|
355
362
|
await Bun.spawn({
|
|
356
|
-
cmd: [
|
|
363
|
+
cmd: [bunPath, 'run', './dev/bundler.js'],
|
|
357
364
|
cwd: process.cwd(),
|
|
358
365
|
stdout: 'inherit',
|
|
359
366
|
env: {
|
|
@@ -382,7 +389,7 @@ function handleFiles() {
|
|
|
382
389
|
file = file.replace('.ts', '.js')
|
|
383
390
|
fs.writeFileSync(path.join(process.cwd() + '/dist', file.replace('.ts', '.js')), code)
|
|
384
391
|
await Bun.spawn({
|
|
385
|
-
cmd: [
|
|
392
|
+
cmd: [bunPath, 'run', './dev/bundler.js'],
|
|
386
393
|
cwd: process.cwd(),
|
|
387
394
|
stdout: 'inherit',
|
|
388
395
|
env: {
|
|
@@ -428,10 +435,10 @@ if (mode === 'development') {
|
|
|
428
435
|
// Function to handle file changes with debounce
|
|
429
436
|
const handleFileChangeDebounced = async (change, file) => {
|
|
430
437
|
if (file.endsWith('.tsx') || file.endsWith('.jsx') || file.endsWith('.css') || file.endsWith('.ts')
|
|
431
|
-
|
|
438
|
+
&& !file.includes('node_module')
|
|
432
439
|
) {
|
|
433
440
|
// delete files cache
|
|
434
|
-
if (file.endsWith('vader.config.ts')){
|
|
441
|
+
if (file.endsWith('vader.config.ts')) {
|
|
435
442
|
delete require.cache[require.resolve(process.cwd() + '/vader.config.ts')]
|
|
436
443
|
|
|
437
444
|
config = require(process.cwd() + '/vader.config.ts').default
|
|
@@ -472,12 +479,12 @@ if (mode === 'development') {
|
|
|
472
479
|
else if (mode == 'production') {
|
|
473
480
|
await handleFiles()
|
|
474
481
|
await generateApp()
|
|
475
|
-
|
|
482
|
+
|
|
476
483
|
console.log(`Build complete in ${Date.now() - start}ms at ${new Date().toLocaleTimeString()}`);
|
|
477
484
|
}
|
|
478
485
|
else {
|
|
479
486
|
if (isBuilding) console.log(`Build complete in ${Date.now() - start}ms at ${new Date().toLocaleTimeString()}`);
|
|
480
|
-
|
|
487
|
+
|
|
481
488
|
}
|
|
482
489
|
|
|
483
490
|
if (mode == 'development' || mode == 'serve') {
|
|
@@ -530,17 +537,17 @@ if (mode == 'development' || mode == 'serve') {
|
|
|
530
537
|
base = base.replace(path.join(process.cwd() + '/app').replace(/\\/g, '/'), '')
|
|
531
538
|
base = base.replace(/\\/g, '/').replace('/app', '/dist')
|
|
532
539
|
base = process.cwd() + "/dist/" + base
|
|
533
|
-
if(!fs.existsSync(path.join(base, 'index.html'))){
|
|
540
|
+
if (!fs.existsSync(path.join(base, 'index.html'))) {
|
|
534
541
|
return new Response(`
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
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
|
+
`, {
|
|
544
551
|
headers: {
|
|
545
552
|
'Content-Type': 'text/html',
|
|
546
553
|
'Cache-Control': 'no-cache'
|
|
@@ -550,26 +557,26 @@ if (mode == 'development' || mode == 'serve') {
|
|
|
550
557
|
let data = await Bun.file(path.join(base, 'index.html')).text()
|
|
551
558
|
if (mode == "development") {
|
|
552
559
|
return new Response(data + `
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
560
|
+
<script>
|
|
561
|
+
let ws = new WebSocket(\`\${location.protocol === 'https:' ? 'wss' : 'ws'}://\${location.host}\`)
|
|
562
|
+
ws.onmessage = (e) => {
|
|
563
|
+
if(e.data === 'reload'){
|
|
564
|
+
console.log('Reloading to display changes from server')
|
|
565
|
+
window.location.reload()
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
ws.onopen = () => {
|
|
569
|
+
console.log('Connected to hmr server')
|
|
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
|
+
`, {
|
|
573
580
|
headers: {
|
|
574
581
|
'Content-Type': 'text/html'
|
|
575
582
|
}
|
|
@@ -587,6 +594,3 @@ if (mode == 'development' || mode == 'serve') {
|
|
|
587
594
|
|
|
588
595
|
console.log(ansiColors.green('Server started at http://localhost:' + port || 8080))
|
|
589
596
|
}
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|