vaderjs 1.5.9 → 1.6.0
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/README.MD +2 -1
- package/bundler/index.js +5 -0
- package/examples/counter/index.jsx +10 -0
- package/main.js +83 -66
- package/package.json +1 -1
package/README.MD
CHANGED
|
@@ -39,6 +39,7 @@ Keyword folders - all files are passed from these folders to the build folder
|
|
|
39
39
|
```md
|
|
40
40
|
1. app - used for jsx route files
|
|
41
41
|
2. public - used for anything / css / json etc
|
|
42
|
+
3. Src - components utilities etc
|
|
42
43
|
```
|
|
43
44
|
|
|
44
45
|
|
|
@@ -53,4 +54,4 @@ export default defineConfig({
|
|
|
53
54
|
hot_reload: true,
|
|
54
55
|
})
|
|
55
56
|
|
|
56
|
-
```
|
|
57
|
+
```
|
package/bundler/index.js
CHANGED
|
@@ -73,6 +73,7 @@ builtCode = handleReplacements(builtCode)
|
|
|
73
73
|
fs.writeFileSync(path.join(process.cwd(), 'dist', process.env.filePath), builtCode)
|
|
74
74
|
}
|
|
75
75
|
let isClass = function (element) {
|
|
76
|
+
if (!element) return false;
|
|
76
77
|
return element.toString().startsWith("class");
|
|
77
78
|
};
|
|
78
79
|
const generatePage = async (
|
|
@@ -84,6 +85,10 @@ const generatePage = async (
|
|
|
84
85
|
let { head } = await import(path).then((m) => m);
|
|
85
86
|
let isFunction = false;
|
|
86
87
|
globalThis.isServer = true;
|
|
88
|
+
if(!html) {
|
|
89
|
+
console.log(ansiColors.red(`No default export found in ${path}`))
|
|
90
|
+
process.exit(0)
|
|
91
|
+
}
|
|
87
92
|
if (isClass(html)) {
|
|
88
93
|
html = new html();
|
|
89
94
|
html.Mounted = true;
|
package/main.js
CHANGED
|
@@ -5,35 +5,48 @@ import { Glob } from 'bun'
|
|
|
5
5
|
const args = Bun.argv.slice(2)
|
|
6
6
|
import fs from 'fs'
|
|
7
7
|
import path from 'path'
|
|
8
|
-
if (!fs.existsSync(process.cwd() + '/app')) {
|
|
8
|
+
if (!fs.existsSync(process.cwd() + '/app') && !args.includes('init')) {
|
|
9
9
|
console.error(`App directory not found in ${process.cwd()}/app`)
|
|
10
10
|
process.exit(1)
|
|
11
11
|
}
|
|
12
12
|
if (!fs.existsSync(process.cwd() + '/public')) {
|
|
13
13
|
fs.mkdirSync(process.cwd() + '/public')
|
|
14
14
|
}
|
|
15
|
-
if(!fs.existsSync(process.cwd() + '/src')){
|
|
15
|
+
if (!fs.existsSync(process.cwd() + '/src')) {
|
|
16
16
|
fs.mkdirSync(process.cwd() + '/src')
|
|
17
17
|
}
|
|
18
|
-
if(!fs.existsSync(process.cwd() + '/vader.config.ts')){
|
|
19
|
-
fs.writeFileSync(process.cwd() + '/vader.config.ts',
|
|
20
|
-
`
|
|
18
|
+
if (!fs.existsSync(process.cwd() + '/vader.config.ts')) {
|
|
19
|
+
fs.writeFileSync(process.cwd() + '/vader.config.ts',
|
|
20
|
+
`
|
|
21
21
|
import defineConfig from 'vaderjs/config'
|
|
22
22
|
export default defineConfig({
|
|
23
23
|
port: 8080,
|
|
24
24
|
host_provider: 'apache'
|
|
25
25
|
})`)
|
|
26
26
|
}
|
|
27
|
-
const mode = args.includes('dev') ? 'development' : args.includes('prod') || args.includes('build') ? 'production' : null
|
|
27
|
+
const mode = args.includes('dev') ? 'development' : args.includes('prod') || args.includes('build') ? 'production' : args.includes('init') ? 'init' : null
|
|
28
28
|
if (!mode) {
|
|
29
29
|
console.log(`
|
|
30
30
|
Usage:
|
|
31
31
|
bun vaderjs dev - Start development server output in dist/
|
|
32
32
|
bun vaderjs prod - Build for production output in dist/
|
|
33
|
+
bun vaderjs init - Initialize a new vaderjs project
|
|
33
34
|
`)
|
|
34
35
|
process.exit(1)
|
|
35
36
|
}
|
|
36
37
|
|
|
38
|
+
if (mode === 'init') {
|
|
39
|
+
if (fs.existsSync(process.cwd() + '/app')) {
|
|
40
|
+
console.error('App directory already exists: just run `bun vaderjs dev` to start the development server')
|
|
41
|
+
process.exit(1)
|
|
42
|
+
}
|
|
43
|
+
fs.mkdirSync(process.cwd() + '/app')
|
|
44
|
+
fs.copyFileSync(path.join(process.cwd(), "/node_modules/vaderjs/example/counter/index.jsx"), path.join(process.cwd(), "/app/index.jsx"))
|
|
45
|
+
|
|
46
|
+
console.log('Initialized new vaderjs project: run `bun vaderjs dev` to start the development server')
|
|
47
|
+
procss.exit(1)
|
|
48
|
+
}
|
|
49
|
+
|
|
37
50
|
console.log(
|
|
38
51
|
`VaderJS - v${require(process.cwd() + '/node_modules/vaderjs/package.json').version} 🚀
|
|
39
52
|
Mode: ${mode}
|
|
@@ -42,6 +55,8 @@ console.log(
|
|
|
42
55
|
`
|
|
43
56
|
)
|
|
44
57
|
|
|
58
|
+
|
|
59
|
+
console.log(ansiColors.green('Building...'))
|
|
45
60
|
let start = Date.now()
|
|
46
61
|
console.log(`Starting build at ${new Date().toLocaleTimeString()}`)
|
|
47
62
|
let { port, host, host_provider } = require(process.cwd() + '/vader.config.ts').default
|
|
@@ -61,31 +76,31 @@ if (!fs.existsSync(process.cwd() + '/jsconfig.json')) {
|
|
|
61
76
|
|
|
62
77
|
var bindes = []
|
|
63
78
|
|
|
64
|
-
const handleReplacements = (code
|
|
79
|
+
const handleReplacements = (code) => {
|
|
65
80
|
let lines = code.split('\n')
|
|
66
81
|
let newLines = []
|
|
67
82
|
for (let line of lines) {
|
|
68
83
|
let hasImport = line.includes('import')
|
|
69
|
-
|
|
84
|
+
|
|
70
85
|
if (hasImport && line.includes('.css')) {
|
|
71
86
|
try {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
87
|
+
let isSmallColon = line.includes("'")
|
|
88
|
+
let url = isSmallColon ? line.split("'")[1] : line.split('"')[1]
|
|
89
|
+
// start from "/" not "/app"
|
|
90
|
+
// remvoe all ./ and ../
|
|
91
|
+
url = url.replaceAll('./', '/').replaceAll('../', '/')
|
|
92
|
+
|
|
93
|
+
let p = path.join(process.cwd(), '/', url)
|
|
94
|
+
line = '';
|
|
95
|
+
url = url.replace(process.cwd() + '/app', '')
|
|
96
|
+
url = url.replace(/\\/g, '/')
|
|
97
|
+
if (!bindes.includes(`<link rel="stylesheet" href="${url}">`)) {
|
|
98
|
+
bindes.push(`
|
|
84
99
|
<style>
|
|
85
100
|
${fs.readFileSync(p, 'utf-8')}
|
|
86
101
|
</style>
|
|
87
102
|
`)
|
|
88
|
-
|
|
103
|
+
}
|
|
89
104
|
} catch (error) {
|
|
90
105
|
console.error(error)
|
|
91
106
|
}
|
|
@@ -139,7 +154,7 @@ async function generateApp() {
|
|
|
139
154
|
style: 'nextjs'
|
|
140
155
|
})
|
|
141
156
|
routes.reload()
|
|
142
|
-
globalThis.routes = routes.routes
|
|
157
|
+
globalThis.routes = routes.routes
|
|
143
158
|
Object.keys(routes.routes).forEach(async (route) => {
|
|
144
159
|
|
|
145
160
|
let r = routes.routes[route]
|
|
@@ -153,7 +168,7 @@ async function generateApp() {
|
|
|
153
168
|
let route = window.location.pathname.split('/').filter(v => v !== '')
|
|
154
169
|
let params = {
|
|
155
170
|
${Object.keys(routes.match(route).params || {}).length > 0 ? Object.keys(routes.match(route).params || {}).map(p => {
|
|
156
|
-
return `${p}: route[${Object.keys(routes.match(route).params).indexOf(p) +
|
|
171
|
+
return `${p}: route[${Object.keys(routes.match(route).params).indexOf(p) + Object.keys(routes.match(route).params).length}]`
|
|
157
172
|
}).join(',') : ""}
|
|
158
173
|
}
|
|
159
174
|
\n${code}
|
|
@@ -205,8 +220,8 @@ async function generateApp() {
|
|
|
205
220
|
rewrites: []
|
|
206
221
|
}
|
|
207
222
|
|
|
208
|
-
for (let route in routes.routes) {
|
|
209
|
-
let { filePath, kind, name, params, pathname, query } = routes.match(route)
|
|
223
|
+
for (let route in routes.routes) {
|
|
224
|
+
let { filePath, kind, name, params, pathname, query } = routes.match(route)
|
|
210
225
|
let r = route
|
|
211
226
|
|
|
212
227
|
if (r.includes('[')) {
|
|
@@ -214,8 +229,8 @@ async function generateApp() {
|
|
|
214
229
|
}
|
|
215
230
|
if (r === '/') {
|
|
216
231
|
continue
|
|
217
|
-
}
|
|
218
|
-
|
|
232
|
+
}
|
|
233
|
+
|
|
219
234
|
vercelData.rewrites.push({
|
|
220
235
|
source: r,
|
|
221
236
|
destination: `${path.dirname(routes.routes[route]).replace(process.cwd().replace(/\\/g, '/') + '/app', '')}/index.html`
|
|
@@ -245,13 +260,13 @@ function handleFiles() {
|
|
|
245
260
|
}
|
|
246
261
|
let glob2 = new Glob('src/**/*')
|
|
247
262
|
for await (var i of glob2.scan()) {
|
|
248
|
-
|
|
263
|
+
var file = i
|
|
249
264
|
fs.mkdirSync(path.join(process.cwd() + '/dist', path.dirname(file)), { recursive: true })
|
|
250
|
-
|
|
251
|
-
if (file.includes('.jsx')) {
|
|
265
|
+
// turn jsx to js
|
|
266
|
+
if (file.includes('.jsx')) {
|
|
252
267
|
let code = await Bun.file(file).text()
|
|
253
268
|
code = handleReplacements(code)
|
|
254
|
-
|
|
269
|
+
|
|
255
270
|
file = file.replace('.jsx', '.js')
|
|
256
271
|
fs.writeFileSync(path.join(process.cwd() + '/dist', file.replace('.jsx', '.js')), code)
|
|
257
272
|
await Bun.spawn({
|
|
@@ -266,7 +281,7 @@ function handleFiles() {
|
|
|
266
281
|
DEV: mode === 'development',
|
|
267
282
|
size: code.length / 1024,
|
|
268
283
|
filePath: file.replace('.jsx', '.js'),
|
|
269
|
-
INPUT:
|
|
284
|
+
INPUT: path.join(process.cwd(), file.replace('.js', '.jsx')),
|
|
270
285
|
},
|
|
271
286
|
onExit({ exitCode: code }) {
|
|
272
287
|
if (code === 0) {
|
|
@@ -276,7 +291,7 @@ function handleFiles() {
|
|
|
276
291
|
}
|
|
277
292
|
}
|
|
278
293
|
})
|
|
279
|
-
}else if(file.includes('.ts')){
|
|
294
|
+
} else if (file.includes('.ts')) {
|
|
280
295
|
let code = await Bun.file(file).text()
|
|
281
296
|
code = handleReplacements(code)
|
|
282
297
|
file = file.replace('.ts', '.js')
|
|
@@ -294,7 +309,7 @@ function handleFiles() {
|
|
|
294
309
|
isTS: true,
|
|
295
310
|
size: code.length / 1024,
|
|
296
311
|
filePath: file.replace('.ts', '.js'),
|
|
297
|
-
INPUT:
|
|
312
|
+
INPUT: path.join(process.cwd(), file.replace('.js', '.jsx')),
|
|
298
313
|
},
|
|
299
314
|
onExit({ exitCode: code }) {
|
|
300
315
|
if (code === 0) {
|
|
@@ -305,9 +320,9 @@ function handleFiles() {
|
|
|
305
320
|
}
|
|
306
321
|
})
|
|
307
322
|
}
|
|
308
|
-
|
|
323
|
+
|
|
309
324
|
}
|
|
310
|
-
|
|
325
|
+
|
|
311
326
|
resolve()
|
|
312
327
|
} catch (error) {
|
|
313
328
|
reject(error)
|
|
@@ -318,7 +333,7 @@ await handleFiles()
|
|
|
318
333
|
globalThis.clients = []
|
|
319
334
|
|
|
320
335
|
if (mode === 'development') {
|
|
321
|
-
const watcher = fs.watch(path.join(process.cwd() + '/'), { recursive: true })
|
|
336
|
+
const watcher = fs.watch(path.join(process.cwd() + '/'), { recursive: true })
|
|
322
337
|
let isBuilding = false; // Flag to track build status
|
|
323
338
|
|
|
324
339
|
// Initialize a variable to hold the timeout ID
|
|
@@ -326,33 +341,33 @@ if (mode === 'development') {
|
|
|
326
341
|
|
|
327
342
|
// Function to handle file changes with debounce
|
|
328
343
|
const handleFileChangeDebounced = async (change, file) => {
|
|
329
|
-
if(file.endsWith('.tsx') || file.endsWith('.jsx') || file.endsWith('.css') || file.endsWith('.ts')
|
|
330
|
-
if(file.includes('dist')) return
|
|
344
|
+
if (file.endsWith('.tsx') || file.endsWith('.jsx') || file.endsWith('.css') || file.endsWith('.ts')) {
|
|
345
|
+
if (file.includes('dist')) return
|
|
331
346
|
clearTimeout(debounceTimeout);
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
347
|
+
debounceTimeout = setTimeout(async () => {
|
|
348
|
+
if (!isBuilding) { // Check if not already building
|
|
349
|
+
isBuilding = true; // Set build flag to true
|
|
350
|
+
try {
|
|
351
|
+
await generateApp();
|
|
352
|
+
await handleFiles();
|
|
353
|
+
let t = setTimeout(() => {
|
|
354
|
+
clients.forEach(c => {
|
|
355
|
+
c.send('reload');
|
|
356
|
+
});
|
|
357
|
+
clearTimeout(t)
|
|
358
|
+
}, 1000)
|
|
359
|
+
} catch (error) {
|
|
360
|
+
console.error(error);
|
|
361
|
+
} finally {
|
|
362
|
+
isBuilding = false; // Reset build flag
|
|
363
|
+
}
|
|
348
364
|
}
|
|
349
|
-
}
|
|
350
|
-
}, 500);
|
|
365
|
+
}, 500);
|
|
351
366
|
}
|
|
352
367
|
};
|
|
353
368
|
|
|
354
369
|
// Event listeners with debounced handling
|
|
355
|
-
watcher.on('change', handleFileChangeDebounced);
|
|
370
|
+
watcher.on('change', handleFileChangeDebounced);
|
|
356
371
|
let server = Bun.serve({
|
|
357
372
|
port: port || 8080,
|
|
358
373
|
websocket: {
|
|
@@ -374,11 +389,11 @@ if (mode === 'development') {
|
|
|
374
389
|
|
|
375
390
|
let url = new URL(req.url)
|
|
376
391
|
if (url.pathname.includes('.')) {
|
|
377
|
-
let p = url.pathname.replaceAll("%5B", "[").replaceAll("%5D", "]")
|
|
392
|
+
let p = url.pathname.replaceAll("%5B", "[").replaceAll("%5D", "]")
|
|
378
393
|
let file = await Bun.file(path.join(process.cwd() + '/dist' + p))
|
|
379
394
|
if (!await file.exists()) return new Response('Not found', { status: 404 })
|
|
380
395
|
let imageTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/svg+xml', 'image/webp', 'image/tiff', 'image/bmp', 'image/ico', 'image/cur', 'image/jxr', 'image/jpg']
|
|
381
|
-
|
|
396
|
+
|
|
382
397
|
return new Response(imageTypes.includes(file.type) ? await file.arrayBuffer() : await file.text(), {
|
|
383
398
|
headers: {
|
|
384
399
|
'Content-Type': file.type,
|
|
@@ -399,9 +414,9 @@ if (mode === 'development') {
|
|
|
399
414
|
let p = route.pathname;
|
|
400
415
|
let base = path.dirname(route.filePath)
|
|
401
416
|
base = base.replace(/\\/g, '/')
|
|
402
|
-
base = base.replace(path.join(process.cwd() + '/app').replace(/\\/g, '/'), '')
|
|
417
|
+
base = base.replace(path.join(process.cwd() + '/app').replace(/\\/g, '/'), '')
|
|
403
418
|
base = base.replace(/\\/g, '/').replace('/app', '/dist')
|
|
404
|
-
base = process.cwd() + "/dist/" + base
|
|
419
|
+
base = process.cwd() + "/dist/" + base
|
|
405
420
|
let data = await Bun.file(path.join(base, 'index.html')).text()
|
|
406
421
|
if (mode == "development") {
|
|
407
422
|
return new Response(data + `
|
|
@@ -414,9 +429,9 @@ if (mode === 'development') {
|
|
|
414
429
|
}
|
|
415
430
|
</script>
|
|
416
431
|
`, {
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
432
|
+
headers: {
|
|
433
|
+
'Content-Type': 'text/html'
|
|
434
|
+
}
|
|
420
435
|
})
|
|
421
436
|
} else {
|
|
422
437
|
return new Response(data, {
|
|
@@ -431,4 +446,6 @@ if (mode === 'development') {
|
|
|
431
446
|
} else {
|
|
432
447
|
console.log(`Build complete in ${Date.now() - start}ms at ${new Date().toLocaleTimeString()}`)
|
|
433
448
|
process.exit(0)
|
|
434
|
-
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
console.log(ansiColors.green('Server started at http://localhost:' + port || 8080))
|