vaderjs 2.2.0 → 2.2.2

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/bundler/index.js CHANGED
@@ -97,9 +97,9 @@ const handleReplacements = (code) => {
97
97
  }
98
98
  if (hasImport && line.includes('react')) {
99
99
  line = line.replace('react', '/src/vader/index.js')
100
- }
100
+ }
101
101
  if(hasImport && line.includes('public') && line.includes('.png') ||
102
- line.includes('.jpg') || line.includes('.jpeg') || line.includes('.gif') || line.includes('.svg')) {
102
+ hasImport && line.includes('.jpg') || hasImport && line.includes('.jpeg') || hasImport && line.includes('.gif') || hasImport && line.includes('.svg')) {
103
103
  let url = line.split('"')[1]
104
104
  // replace ../../
105
105
  var b4 = url
@@ -182,25 +182,17 @@ const handleReplacements = (code) => {
182
182
  return c
183
183
  }
184
184
  builtCode = handleReplacements(builtCode)
185
-
186
- builtCode += `
187
- var process = {
188
- env: {
189
- ${Object.keys(process.env)
190
- .filter((key) =>
191
- !['bindes', 'ENTRYPOINT', 'ROOT', 'OUT', 'file', 'DEV',
192
- 'size', 'filePath', 'isAppFile', 'isJsx', 'INPUT'].includes(key)
193
- )
194
- .map((key) => `${key}: "${process.env[key]}"`)
195
- .join(',\n')}
185
+ builtCode = await new Bun.Transpiler({
186
+ loader: 'tsx',
187
+ tsconfig:{
188
+ "compilerOptions": {
189
+ "jsx": "react",
190
+ "jsxFactory": "e",
191
+ "jsxFragmentFactory": "Fragment"
196
192
  }
197
- }
198
- `;
199
- let rename = process.env.filePath.split('.js')[0] + Math.random().toString().split('.')[1] + '.js'
200
- fs.writeFileSync(path.join(process.cwd(), 'dist', process.env.filePath), builtCode)
201
- var before = process.env.filePath
202
- process.env.filePath = rename
203
- fs.renameSync(path.join(process.cwd(), 'dist', before), path.join(process.cwd(), 'dist', process.env.filePath))
193
+ }
194
+ }).transformSync(builtCode)
195
+ fs.writeFileSync(path.join(process.cwd(), 'dist', process.env.filePath), builtCode)
204
196
  let isClass = function (element) {
205
197
  return element && element.toString().startsWith("class");
206
198
  };
@@ -272,6 +264,7 @@ const generatePage = async (
272
264
  import {render, e} from '/src/vader/index.js'
273
265
  window.e = e
274
266
  render(c, document.body)
267
+ console.log(e)
275
268
  </script>
276
269
 
277
270
  ${process.env.bindes}
package/main.js CHANGED
@@ -4,18 +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
8
  import { platform } from 'os'
9
9
  import path from 'path'
10
10
 
11
11
  let bunPath = 'bun'; // Default for Linux/Mac
12
12
  if (platform() === 'win32') {
13
- bunPath ='bun'; // Bun path for Windows
13
+ bunPath = 'bun'; // Bun path for Windows
14
14
  } else {
15
15
  bunPath = path.resolve(process.env.HOME || process.env.USERPROFILE, '.bun', 'bin', 'bun');
16
16
  }
17
-
18
-
17
+
18
+
19
19
 
20
20
 
21
21
  if (!fs.existsSync(process.cwd() + '/app') && !args.includes('init')) {
@@ -94,7 +94,7 @@ const vader = {
94
94
  },
95
95
  runCommand: (cmd) => {
96
96
  return new Promise((resolve, reject) => {
97
- let c = Bun.spawn(cmd, {
97
+ let c = Bun.spawn(cmd, {
98
98
  stdout: 'inherit',
99
99
  cwd: process.cwd(),
100
100
  onExit({ exitCode: code }) {
@@ -106,10 +106,7 @@ const vader = {
106
106
  }
107
107
  })
108
108
 
109
- setTimeout(() => {
110
- c.kill()
111
- reject()
112
- }, 5000)
109
+
113
110
 
114
111
 
115
112
  })
@@ -129,8 +126,8 @@ const handleReplacements = (code) => {
129
126
  let newLines = []
130
127
  for (let line of lines) {
131
128
  let hasImport = line.includes('import')
132
-
133
- if(hasImport && line.includes('public')){
129
+
130
+ if (hasImport && line.includes('public')) {
134
131
  // remove ../ from path
135
132
 
136
133
  line = line.replaceAll('../', '').replaceAll('./', '')
@@ -187,7 +184,7 @@ const handleReplacements = (code) => {
187
184
  if (!hasImport && line.includes('useRef')) {
188
185
  line = line.replace(' ', '')
189
186
  let b4 = line
190
- let key = line.split('=')[0].split(' ').filter(Boolean)[1]
187
+ let key = line.split('=')[0].split(' ').filter(Boolean)[1]
191
188
  b4 = line.replace('useRef(', `this.useRef('${key}',`)
192
189
  line = b4
193
190
  }
@@ -198,27 +195,31 @@ const handleReplacements = (code) => {
198
195
  return c
199
196
  }
200
197
 
201
- if (!fs.existsSync(process.cwd() + '/dev/bundler.js')) {
198
+ if (!fs.existsSync(process.cwd() + '/dev/bundler.js')) {
202
199
  fs.mkdirSync(process.cwd() + '/dev', { recursive: true })
203
200
  fs.copyFileSync(require.resolve('vaderjs/bundler/index.js'), process.cwd() + '/dev/bundler.js')
204
201
  }
205
202
  let start = Date.now()
206
203
  async function generateApp() {
207
204
  globalThis.isBuilding = true;
208
- console.log(ansiColors.green('Building...'))
205
+ console.log(ansiColors.green('Building...'))
209
206
  if (mode === 'development') {
210
- fs.rmdirSync(process.cwd() + '/dist', { recursive: true })
207
+
211
208
  } else {
212
209
  fs.mkdirSync(process.cwd() + '/dist', { recursive: true })
213
210
  }
214
- let plugins = config.plugins || []
215
- for (let plugin of plugins) {
216
- if (plugin.onBuildStart) {
217
- await plugin.onBuildStart(vader)
211
+ try {
212
+ let plugins = config.plugins || []
213
+ for (let plugin of plugins) {
214
+ if (plugin.onBuildStart) {
215
+ await plugin.onBuildStart(vader)
216
+ }
218
217
  }
218
+ } catch (error) {
219
+ console.log(error)
219
220
  }
220
221
 
221
-
222
+
222
223
  return new Promise(async (resolve, reject) => {
223
224
  let routes = new Bun.FileSystemRouter({
224
225
  dir: path.join(process.cwd(), '/app'),
@@ -233,19 +234,29 @@ async function generateApp() {
233
234
  code = handleReplacements(code)
234
235
  let size = code.length / 1024
235
236
  r = r.replace(process.cwd().replace(/\\/g, '/') + '/app', '')
236
- r = r.replace('.jsx', '.js').replace('.tsx', '.js')
237
+ r = r.replace('.jsx', '.js').replace('.tsx', '.js')
237
238
  fs.mkdirSync(path.join(process.cwd() + '/dist', path.dirname(r)), { recursive: true })
238
239
  let params = routes.match(route).params || {}
239
- let base = routes.match(route)
240
- let paramIndexes = []
240
+ let base = routes.match(route)
241
+ let paramIndexes = []
241
242
  for (let param in params) {
242
- let routes = base.pathname.split('/')
243
+ let routes = base.pathname.split('/')
243
244
  let index = routes.indexOf('[' + param + ']')
244
245
  paramIndexes.push(index)
245
- }
246
-
246
+ }
247
+
247
248
  // dont return
248
-
249
+ code = await new Bun.Transpiler({
250
+ loader: 'tsx',
251
+ tsconfig: {
252
+ "compilerOptions": {
253
+ "jsx": "react",
254
+ "jsxFactory": "e",
255
+ "jsxFragmentFactory": "Fragment"
256
+ }
257
+ }
258
+ }).transformSync(code)
259
+
249
260
  fs.writeFileSync(
250
261
  process.cwd() + '/dist/' + path.dirname(r) + '/' + path.basename(r),
251
262
  `
@@ -253,30 +264,38 @@ async function generateApp() {
253
264
  let params = {
254
265
  // get index tehn do route[index]
255
266
  ${Object.keys(params).map((param, i) => {
256
- if (paramIndexes[i] !== -1) {
257
- var r_copy = r;
258
- r_copy = r_copy.split('/').filter(Boolean)
259
- var index = paramIndexes[i] - 1
260
- return `${param}: route[${index}]`
261
- }
262
- }).join(',\n')}
267
+ if (paramIndexes[i] !== -1) {
268
+ var r_copy = r;
269
+ r_copy = r_copy.split('/').filter(Boolean)
270
+ var index = paramIndexes[i] - 1
271
+ return `${param}: route[${index}]`
272
+ }
273
+ }).join(',\n')}
263
274
  }
264
275
 
265
276
  \n${code}
266
277
  `
267
278
  );
268
279
  fs.mkdirSync(process.cwd() + '/dev', { recursive: true })
269
-
280
+
270
281
 
271
282
  if (!fs.existsSync(process.cwd() + '/dev/readme.md')) {
272
283
  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`)
273
284
  }
274
- fs.mkdirSync(process.cwd() + '/dist/src/vader', { recursive: true })
275
- fs.writeFileSync(process.cwd() + '/dist/src/vader/index.js', await new Bun.Transpiler({
276
- loader: 'ts',
277
- }).transformSync(await Bun.file(require.resolve('vaderjs')).text()))
278
- Bun.spawn({
279
- cmd: [bunPath, 'run', './dev/bundler.js'] ,
285
+ async function runT() {
286
+ return await new Bun.Transpiler({
287
+ loader: 'ts',
288
+ }).transformSync(await Bun.file(require.resolve('vaderjs')).text())
289
+ }
290
+ if (!fs.existsSync(path.join(process.cwd(), '/dist/src/vader'))
291
+ || fs.readFileSync(path.join(process.cwd(), '/dist/src/vader/index.js')) != await runT()
292
+ ) {
293
+ fs.mkdirSync(process.cwd() + '/dist/src/vader', { recursive: true })
294
+ fs.writeFileSync(path.join(process.cwd(), '/dist/src/vader/index.js'), (await runT()))
295
+
296
+ }
297
+ await Bun.spawn({
298
+ cmd: [bunPath, 'run', './dev/bundler.js'],
280
299
  cwd: process.cwd(),
281
300
  stdout: 'inherit',
282
301
  env: {
@@ -334,13 +353,17 @@ async function generateApp() {
334
353
 
335
354
  }
336
355
  // run all plugins that have onBuildFinish
337
- let plugins = config.plugins || []
338
- for (let plugin of plugins) {
339
- if (plugin.onBuildFinish) {
340
- await plugin.onBuildFinish(vader)
356
+ try {
357
+ let plugins = config.plugins || []
358
+ for (let plugin of plugins) {
359
+ if (plugin.onBuildFinish) {
360
+ await plugin.onBuildFinish(vader)
361
+ }
341
362
  }
363
+ } catch (error) {
364
+ console.error(ansiColors.red(error))
342
365
  }
343
-
366
+
344
367
  })
345
368
 
346
369
 
@@ -348,7 +371,7 @@ async function generateApp() {
348
371
 
349
372
  function handleFiles() {
350
373
  return new Promise(async (resolve, reject) => {
351
- try {
374
+ try {
352
375
  let glob = new Glob('public/**/*')
353
376
  for await (var i of glob.scan()) {
354
377
  let file = i
@@ -367,6 +390,9 @@ function handleFiles() {
367
390
  let code = await Bun.file(file).text()
368
391
 
369
392
  code = handleReplacements(code)
393
+ code = await new Bun.Transpiler({
394
+ loader: 'ts',
395
+ }).transformSync(code)
370
396
 
371
397
  file = file.replace('.jsx', '.js').replace('.tsx', '.js')
372
398
  fs.writeFileSync(path.join(process.cwd() + '/dist', file.replace('.jsx', '.js').replace('.tsx', '.js')), code)
@@ -438,68 +464,73 @@ if (mode === 'development') {
438
464
  try {
439
465
  await generateApp()
440
466
  await handleFiles()
441
- const watcher = fs.watch(path.join(process.cwd() + '/'), { recursive: true })
442
- let isBuilding = false; // Flag to track build status
443
-
444
- // Initialize a variable to hold the timeout ID
467
+ let watcher;
468
+ let isBuilding = false;
445
469
  let debounceTimeout;
446
-
447
- // Function to handle file changes with debounce
448
- const handleFileChangeDebounced = async (change, file) => {
449
- if (file.endsWith('.tsx') || file.endsWith('.jsx') || file.endsWith('.css') || file.endsWith('.ts')
450
- && !file.includes('node_module')
451
- ) {
452
- // delete files cache
453
- if (file.endsWith('vader.config.ts')){
454
- delete require.cache[require.resolve(process.cwd() + '/vader.config.ts')]
455
-
456
- config = require(process.cwd() + '/vader.config.ts').default
457
- port = config.port;
458
- host_provider = config.host_provider
459
- host = config.host
460
-
461
- globalThis.config = config
470
+
471
+ const startWatcher = () => {
472
+ if (watcher) watcher.close(); // Close any existing watcher
473
+
474
+ watcher = fs.watch(path.join(process.cwd(), '/'), { recursive: true }, (eventType, file) => {
475
+ if (!file) return; // Ensure file name is valid
476
+ if (file.includes('node_modules')) return;
477
+ if (file.includes('dist')) return;
478
+ if (!fs.existsSync(path.join(process.cwd(), file))) {
479
+ fs.rmSync(path.join(process.cwd(), "dist", file))
462
480
  }
463
- if (file.includes('dist')) return
464
- clearTimeout(debounceTimeout);
465
- debounceTimeout = setTimeout(async () => {
466
- if (!isBuilding) { // Check if not already building
467
- isBuilding = true; // Set build flag to true
468
- try {
469
- await generateApp();
470
- await handleFiles();
471
- let t = setTimeout(() => {
472
- clients.forEach(c => {
473
- c.send('reload');
474
- });
475
- clearTimeout(t)
476
- }, 1000)
477
- } catch (error) {
478
- console.error(error);
479
- } finally {
480
- isBuilding = false; // Reset build flag
481
- }
481
+
482
+ if (
483
+ file.endsWith('.tsx') || file.endsWith('.jsx') || file.endsWith('.css') || file.endsWith('.ts')
484
+ ) {
485
+ // Reset config if needed
486
+ if (file.endsWith('vader.config.ts')) {
487
+ delete require.cache[require.resolve(process.cwd() + '/vader.config.ts')];
488
+ globalThis.config = require(process.cwd() + '/vader.config.ts').default;
482
489
  }
483
- }, 500);
484
- }
490
+
491
+ clearTimeout(debounceTimeout);
492
+ debounceTimeout = setTimeout(async () => {
493
+ if (!isBuilding) {
494
+ isBuilding = true;
495
+ try {
496
+ await generateApp();
497
+ await handleFiles();
498
+ setTimeout(() => {
499
+ clients.forEach(c => c.send('reload'));
500
+ }, 1000);
501
+ } catch (error) {
502
+ console.error(error);
503
+ } finally {
504
+ isBuilding = false;
505
+ }
506
+ }
507
+ }, 500);
508
+ }
509
+
510
+ // Restart watcher if a new directory is created
511
+ if (eventType === 'rename') {
512
+ setTimeout(startWatcher, 500); // Slight delay to allow the OS to recognize new files
513
+ }
514
+ });
485
515
  };
486
-
487
- // Event listeners with debounced handling
488
- watcher.on('change', handleFileChangeDebounced);
516
+
517
+ // Start the watcher and restart it periodically
518
+ setInterval(startWatcher, 500);
519
+ startWatcher(); // Run initially
489
520
  } catch (error) {
490
- console.error(error)
521
+ console.error(error)
491
522
  }
492
523
 
493
524
  }
494
525
  else if (mode == 'production') {
495
526
  await handleFiles()
496
527
  await generateApp()
497
-
528
+
498
529
  console.log(`Build complete in ${Date.now() - start}ms at ${new Date().toLocaleTimeString()}`);
499
530
  }
500
531
  else {
501
532
  if (isBuilding) console.log(`Build complete in ${Date.now() - start}ms at ${new Date().toLocaleTimeString()}`);
502
-
533
+
503
534
  }
504
535
 
505
536
  if (mode == 'development' || mode == 'serve') {
@@ -552,7 +583,7 @@ if (mode == 'development' || mode == 'serve') {
552
583
  base = base.replace(path.join(process.cwd() + '/app').replace(/\\/g, '/'), '')
553
584
  base = base.replace(/\\/g, '/').replace('/app', '/dist')
554
585
  base = process.cwd() + "/dist/" + base
555
- if(!fs.existsSync(path.join(base, 'index.html'))){
586
+ if (!fs.existsSync(path.join(base, 'index.html'))) {
556
587
  return new Response(`
557
588
  <html>
558
589
  <head>
@@ -608,7 +639,4 @@ if (mode == 'development' || mode == 'serve') {
608
639
  })
609
640
 
610
641
  console.log(ansiColors.green('Server started at http://localhost:' + port || 8080))
611
- }
612
-
613
-
614
-
642
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vaderjs",
3
- "version": "2.2.0",
3
+ "version": "2.2.2",
4
4
  "description": "A simple and powerful JavaScript library for building modern web applications.",
5
5
  "bin": {
6
6
  "vaderjs": "./main.js"
@@ -5,14 +5,14 @@ function checkIfTailwindInstalled() {
5
5
  try {
6
6
  //@ts-ignore
7
7
  require.resolve('tailwindcss')
8
+ require.resolve('postcss')
8
9
  return true
9
10
  } catch (e) {
10
11
  return false
11
12
  }
12
13
  }
13
14
 
14
- function initTailwind() {
15
- const tailwindConfig = path.resolve(process.cwd(), 'tailwind.config.js')
15
+ function initTailwind() {
16
16
  const postcssConfig = path.resolve(process.cwd(), 'postcss.config.mjs')
17
17
  const tailwindCssFile = path.join(process.cwd(), '/public/styles.css')
18
18
  if(!fs.existsSync(tailwindCssFile)){
@@ -48,4 +48,4 @@ export default {
48
48
  console.log('TailwindCSS plugin finished building')
49
49
  },
50
50
 
51
- }
51
+ }