vaderjs 1.7.4 → 1.7.6

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
@@ -38,60 +38,108 @@ try {
38
38
  entrypoints: [process.env.ENTRYPOINT],
39
39
  minify: false,
40
40
  root: process.cwd() + "/dist/",
41
- outdir: process.cwd() + "/dist/",
42
-
41
+ outdir: process.cwd() + "/dist/",
43
42
  format: "esm",
44
43
  ...(process.env.DEV ? { sourcemap: "inline" } : {}),
45
- external: ['*.jsx', '*.js', '*.ts']
44
+ packages: "bundle",
46
45
  });
47
46
  } catch (error) {
48
47
  console.error(error)
49
48
  }
50
49
 
51
- let builtCode = fs.readFileSync(path.join(process.cwd(), 'dist', process.env.filePath), 'utf-8')
52
50
 
53
- function handleReplacements(code) {
54
- let lines = code.split("\n");
55
- let newLines = [];
51
+ let builtCode = fs.readFileSync(path.join(process.cwd(), 'dist', process.env.filePath), 'utf-8')
52
+ const handleReplacements = (code) => {
53
+ let lines = code.split('\n')
54
+ let newLines = []
56
55
  for (let line of lines) {
57
56
  let hasImport = line.includes('import')
58
- if (hasImport && line.includes('from') && !newLines.includes(line)) {
57
+
58
+ if (hasImport && line.includes('.css')) {
59
59
  try {
60
- let url = line.includes("'") ? line.split("'")[1] : line.split('"')[1]
61
- line = line.replace(url, url.replace('.jsx', '.js').replace('.tsx', '.js'))
62
- line = line.replace(url, url.replace('.ts', '.js').replace('.tsx', '.js'))
63
- newLines.push(line)
60
+ let isSmallColon = line.includes("'")
61
+ let url = isSmallColon ? line.split("'")[1] : line.split('"')[1]
62
+ // start from "/" not "/app"
63
+ // remvoe all ./ and ../
64
+ url = url.replaceAll('./', '/').replaceAll('../', '/')
65
+
66
+ let p = path.join(process.cwd(), '/', url)
67
+ line = '';
68
+ url = url.replace(process.cwd() + '/app', '')
69
+ url = url.replace(/\\/g, '/')
70
+ if (!bindes.includes(`<link rel="stylesheet" href="${url}">`)) {
71
+ bindes.push(`
72
+ <style>
73
+ ${fs.readFileSync(p, 'utf-8')}
74
+ </style>
75
+ `)
76
+ }
64
77
  } catch (error) {
65
- continue;
78
+ console.error(error)
66
79
  }
67
- } else {
68
- newLines.push(line)
69
80
  }
81
+ if (line.toLowerCase().includes('genkey()')) {
82
+ line = line.toLowerCase().replace('genkey()', `this.key = "${crypto.randomUUID()}"`)
83
+ }
84
+ if (!hasImport && line.includes('useFetch')) {
85
+ line = line.replace('useFetch', 'this.useFetch')
86
+ }
87
+ if (!hasImport && line.includes('useState') && line.includes('[')) {
88
+ let key = line.split(',')[0].split('[')[1].replace(' ', '')
89
+ let b4 = line
90
+ b4 = line.replace('useState(', `this.useState('${key}',`)
91
+ line = b4
92
+ }
93
+ if (!hasImport && line.includes('useAsyncState')) {
94
+ let key = line.split(',')[0].split('[')[1].replace(' ', '')
95
+ let b4 = line
96
+ b4 = line.replace('useAsyncState(', `this.useAsyncState('${key}',`)
97
+ line = b4
98
+ }
99
+ if (!hasImport && line.includes('useEffect')) {
100
+ let b4 = line
101
+ b4 = line.replace('useEffect(', `this.useEffect(`)
102
+ line = b4
103
+ }
104
+ if (!hasImport && line.includes('useRef')) {
105
+ let b4 = line
106
+ let key = line.split(' ')[1].split('=')[0]
107
+ b4 = line.replace('useRef(', `this.useRef('${key}',`)
108
+ line = b4
109
+ }
110
+
111
+ newLines.push(line)
70
112
  }
71
- return newLines.join("\n");
72
- }
113
+ let c = newLines.join('\n')
114
+ return c
115
+ }
73
116
  builtCode = handleReplacements(builtCode)
117
+
74
118
  fs.writeFileSync(path.join(process.cwd(), 'dist', process.env.filePath), builtCode)
75
119
 
76
- let isClass = function (element) {
77
- return element.toString().startsWith("class");
120
+ let isClass = function (element) {
121
+ return element && element.toString().startsWith("class");
78
122
  };
79
123
  const generatePage = async (
80
124
  data = { path: process.env.INPUT, route: process.env.OUT }
81
125
  ) => {
82
126
  const { path, route } = data;
83
- if (path.includes("root.js")) return;
127
+ if (path.includes("root.js")) return;
84
128
  let html = await import(path).then((m) => m.default);
129
+
85
130
  let { head } = await import(path).then((m) => m);
86
131
  let isFunction = false;
87
- globalThis.isServer = true;
88
- if (isClass(html)) {
132
+ globalThis.isServer = true;
133
+ if(!html){
134
+ return
135
+ }
136
+ if (isClass(html) ) {
89
137
  html = new html();
90
138
  html.Mounted = true;
91
139
  html = html.render();
92
140
  } else {
93
141
  isFunction = true;
94
- let instance = new Component();
142
+ let instance = new Component();
95
143
  html = html.bind(instance);
96
144
  instance.render = html;
97
145
  html = instance.render();
@@ -108,8 +156,12 @@ const generatePage = async (
108
156
  headHtml = document(head());
109
157
  }
110
158
 
159
+
160
+
111
161
  if (h.includes("<head>")) {
112
162
  h = h.replace("<head>", `<head>${process.env.bindes}`)
163
+ }else{
164
+ h += process.env.bindes
113
165
  }
114
166
 
115
167
  await Bun.write(
@@ -133,10 +185,9 @@ const generatePage = async (
133
185
  );
134
186
  process.exit(0);
135
187
  };
136
- try {
137
- if (process.env.isTs == undefined && process.env.isImport) {
138
- generatePage({ path: process.env.INPUT, route: process.env.OUT })
139
- } else if (process.env.isTs == undefined) {
188
+ console.log(process.env.isJsx, process.env.isAppFile == "true" )
189
+ try {
190
+ if (process.env.isJsx == "true" && process.env.isAppFile == "true" ) {
140
191
  generatePage({ path: process.env.INPUT, route: process.env.OUT })
141
192
  }
142
193
  } catch (error) {
package/index.ts CHANGED
@@ -124,7 +124,8 @@ globalThis.Fragment = Fragment;
124
124
  * @returns
125
125
  */
126
126
  export const e = (element, props, ...children) => {
127
- if(!element) return ""
127
+ if (!element)
128
+ return "";
128
129
  let instance;
129
130
  switch (true) {
130
131
  case isClassComponent(element):
@@ -136,13 +137,16 @@ export const e = (element, props, ...children) => {
136
137
  case typeof element === "function":
137
138
  instance = new Component;
138
139
  instance.render = element;
140
+ if(element.name.toLowerCase() == "default"){
141
+ throw new Error("Function name must be unique")
142
+ }
143
+ instance.key = element.name
139
144
  instance.Mounted = true;
140
145
  let firstEl = instance.render({ key: instance.key, children, ...props }, children);
141
- instance.children = children;
146
+ instance.children = children;
142
147
  if (!firstEl)
143
148
  firstEl = { type: "div", props: { key: instance.key, ...props }, children };
144
149
  firstEl.props = { key: instance.key, ...firstEl.props, ...props };
145
-
146
150
  return firstEl;
147
151
  default:
148
152
  return { type: element, props: props || {}, children: children || [] };
@@ -659,10 +663,14 @@ export function render(element, container) {
659
663
  } else {
660
664
  let memoizedInstance = memoizeClassComponent(Component);
661
665
  memoizedInstance.Mounted = true;
662
- memoizedInstance.render = element.bind(memoizedInstance);
663
- let el = memoizedInstance.toElement();
664
- el.key = memoizedInstance.key;
666
+ memoizedInstance.render = element.bind(memoizedInstance);
667
+ if(element.name == "default"){
668
+ throw new Error("Function name Must be a unique function name as it is used for a element key")
669
+ }
670
+ memoizedInstance.key = element.name
671
+ let el = memoizedInstance.toElement();
672
+ el.key = element.name
665
673
  container.innerHTML = "";
666
674
  container.replaceWith(el);
667
675
  }
668
- }
676
+ }
package/main.js CHANGED
@@ -200,10 +200,10 @@ async function generateApp() {
200
200
  Object.keys(routes.routes).forEach(async (route) => {
201
201
 
202
202
  let r = routes.routes[route]
203
- let code = await Bun.file(r).text()
204
- code = handleReplacements(code)
203
+ let code = await Bun.file(r).text()
205
204
  let size = code.length / 1024
206
205
  r = r.replace(process.cwd().replace(/\\/g, '/') + '/app', '')
206
+ var beforeR = r
207
207
  r = r.replace('.jsx', '.js').replace('.tsx', '.js')
208
208
  fs.mkdirSync(path.join(process.cwd() + '/dist', path.dirname(r)), { recursive: true })
209
209
  fs.writeFileSync(process.cwd() + '/dist/' + path.dirname(r) + '/' + path.basename(r), `
@@ -227,6 +227,7 @@ async function generateApp() {
227
227
  fs.writeFileSync(process.cwd() + '/dist/src/vader/index.js', await new Bun.Transpiler({
228
228
  loader: 'ts',
229
229
  }).transformSync(await Bun.file(require.resolve('vaderjs')).text()))
230
+
230
231
  Bun.spawn({
231
232
  cmd: ['bun', 'run', './dev/bundler.js'],
232
233
  cwd: process.cwd(),
@@ -239,8 +240,12 @@ async function generateApp() {
239
240
  DEV: mode === 'development',
240
241
  size,
241
242
  bindes: bindes.join('\n'),
243
+ isTs: beforeR.endsWith(".tsx"),
242
244
  filePath: r,
243
- INPUT: `../app/${r.replace('.js', '.jsx').replace('.tsx', '.js')}`,
245
+
246
+ isJsx: beforeR.endsWith('.tsx') || beforeR.endsWith(".jsx") ,
247
+ isAppFile: true,
248
+ INPUT: `../app/${beforeR}`,
244
249
  },
245
250
  onExit({ exitCode: code }) {
246
251
  if (code === 0) {
@@ -314,13 +319,13 @@ function handleFiles() {
314
319
  var file = i
315
320
  fs.mkdirSync(path.join(process.cwd() + '/dist', path.dirname(file)), { recursive: true })
316
321
  // turn jsx to js
317
- if (file.includes('.jsx') || file.includes('.tsx')) {
322
+ if (file.endsWith('.jsx') || file.endsWith('.tsx')) {
318
323
  let code = await Bun.file(file).text()
319
324
 
320
325
  code = handleReplacements(code)
321
-
326
+ var url = file
322
327
  file = file.replace('.jsx', '.js').replace('.tsx', '.js')
323
- fs.writeFileSync(path.join(process.cwd() + '/dist', file.replace('.jsx', '.js').replace('.tsx', '.js')), code)
328
+ fs.writeFileSync(path.join(process.cwd() + '/dist', file.replace('.jsx', '.js').replace('.tsx', '.js')), code)
324
329
  await Bun.spawn({
325
330
  cmd: ['bun', 'run', './dev/bundler.js'],
326
331
  cwd: process.cwd(),
@@ -334,8 +339,9 @@ function handleFiles() {
334
339
  DEV: mode === 'development',
335
340
  size: code.length / 1024,
336
341
  filePath: file.replace('.jsx', '.js'),
337
- isTs: file.includes('.tsx'),
338
- INPUT: path.join(process.cwd(), file.replace('.js', '.jsx').replace('.tsx', '.js')),
342
+ isJsx: url.endsWith('.tsx') || url.endsWith(".jsx") ,
343
+ isAppFile: false,
344
+ INPUT: path.join(process.cwd(), url),
339
345
  },
340
346
  onExit({ exitCode: code }) {
341
347
  if (code === 0) {
@@ -345,7 +351,7 @@ function handleFiles() {
345
351
  }
346
352
  }
347
353
  })
348
- } else if (file.includes('.ts')) {
354
+ } else if (file.endsWith('.ts')) {
349
355
  let code = await Bun.file(file).text()
350
356
  code = handleReplacements(code)
351
357
  file = file.replace('.ts', '.js')
@@ -385,9 +391,9 @@ function handleFiles() {
385
391
  }
386
392
  globalThis.clients = []
387
393
 
388
- if (mode === 'development') {
389
- await generateApp()
394
+ if (mode === 'development') {
390
395
  await handleFiles()
396
+ await generateApp()
391
397
  const watcher = fs.watch(path.join(process.cwd() + '/'), { recursive: true })
392
398
  let isBuilding = false; // Flag to track build status
393
399
 
@@ -527,4 +533,4 @@ if (mode == 'development' || mode == 'serve') {
527
533
  })
528
534
 
529
535
  console.log(ansiColors.green('Server started at http://localhost:' + port || 8080))
530
- }
536
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vaderjs",
3
- "version": "1.7.4",
3
+ "version": "1.7.6",
4
4
  "description": "A simple and powerful JavaScript library for building modern web applications.",
5
5
  "bin": {
6
6
  "vaderjs": "./main.js"