vaderjs 1.3.3-alpha-23 → 1.3.3-alpha-25

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/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "vaderjs",
3
3
  "description": "A Reactive library aimed to helping you build reactive applications inspired by react.js",
4
4
  "module": "vader.js",
5
- "version": "1.3.3-alpha-23",
5
+ "version": "1.3.3-alpha-25",
6
6
  "bin": {
7
7
  "vader": "./vader.js"
8
8
  },
package/runtime/router.js CHANGED
@@ -225,6 +225,9 @@ class VaderRouter{
225
225
  if(!document.querySelector('#root')){
226
226
  throw new Error('Root element not found, please add an element with id root');
227
227
  }
228
+ Component.state = {}
229
+ Component.reset();
230
+ Component.components = {}
228
231
  Component.request = req;
229
232
  Component.response = res;
230
233
  document.querySelector('#root').innerHTML = Component.render()
package/runtime/vader.js CHANGED
@@ -257,10 +257,21 @@ export class Component {
257
257
  comp.response = this.response;
258
258
  comp.key = props.key || null;
259
259
 
260
+ if(!this.components[props.key]){
261
+ states[props.key] = {}
262
+ }
260
263
  this.components[props.key] = comp
261
264
  this.children.push(comp)
262
265
  return this.components[props.key]
263
266
  }
267
+ reset(){
268
+ Object.keys(this.components).forEach((key) => {
269
+ states[key] = {}
270
+ })
271
+ states[this.key] = {}
272
+ this.components = {}
273
+ this.children = []
274
+ }
264
275
  memoize(/**@type {Component}**/component){
265
276
  if(!component.key){
266
277
  throw new Error('Component must have a static key')
@@ -273,7 +284,7 @@ export class Component {
273
284
  }
274
285
 
275
286
  let comp = this.components[component.key];
276
- comp.props = component.props;
287
+ comp.props = component.props;
277
288
  let h = comp.render()
278
289
 
279
290
  if(h && h.split('>,').length > 1){
@@ -349,7 +360,7 @@ export class Component {
349
360
  } else {
350
361
  const targetElement = document.querySelector(`[key="${this.key}"]`);
351
362
  if (targetElement) {
352
- targetElement.innerHTML = this.render();
363
+ targetElement.outerHTML = this.render();
353
364
  } else {
354
365
  console.error('Target element not found.');
355
366
  }
@@ -542,9 +553,9 @@ export class Component {
542
553
  * @param {T} initialState - The initial state value.
543
554
  * @returns {[() => T, (newValue: T, hook: Function) => void]} - A tuple with getter and setter functions.
544
555
  */
545
- useState(key, initialState) {
546
- if (!this.state[key]) {
547
- this.state[key] = initialState;
556
+ useState(key, initialState) {
557
+ if (!states[this.key][key]) {
558
+ states[this.key][key] = initialState;
548
559
  }
549
560
 
550
561
  /**
@@ -552,7 +563,7 @@ export class Component {
552
563
  *
553
564
  * @returns {T} The current state value.
554
565
  */
555
- let updatedValue = () => this.state[key];
566
+ let updatedValue = () => states[this.key][key];
556
567
 
557
568
  const getValue = updatedValue();
558
569
 
@@ -563,13 +574,13 @@ export class Component {
563
574
  * @param {Function} hook - The hook to hydrate after setting the value.
564
575
  */
565
576
  const set = (newValue, hook) => {
566
- this.state[key] = newValue;
577
+ states[this.key][key] = newValue;
567
578
  this.hydrate(hook);
568
579
  };
569
580
 
570
581
 
571
582
 
572
- return [this.state[key], set];
583
+ return [getValue, set];
573
584
  }
574
585
 
575
586
 
@@ -648,6 +659,7 @@ export const require = async (path, noresolve = false) => {
648
659
  let file = ''
649
660
  try {
650
661
  file = await fetch(path).then((res) => res.text());
662
+ cache
651
663
  } catch (error) {
652
664
  console.error(error)
653
665
  }
@@ -672,9 +684,11 @@ export const require = async (path, noresolve = false) => {
672
684
  file = file.replaceAll(exports, "");
673
685
  }
674
686
 
675
- return new Function(`return (async () => { ${file} })()`)();
687
+ cache[path] = new Function(`return (async () => { ${file} })()`)();
688
+ return cache[path]
676
689
  case filetype === "jsx":
677
- return new Function(`return (async () => { ${file} })()`)()
690
+ cache[path] = new Function(`return (async () => { ${file} })()`)();
691
+ return cache[path]
678
692
 
679
693
  }
680
694
  };
package/vader.js CHANGED
@@ -226,11 +226,13 @@ function Compiler(func) {
226
226
 
227
227
  let paramString = params ? params.split(' ').map(param => param + ',').join('') : "";
228
228
 
229
+ paramString = paramString.replaceAll(',,', ',')
230
+ let jsxAttribute = `${attributeName}=function(${paramString}){${newvalue}},`
229
231
  let newatribute = `${attributeName}="\${this.bind(\`${newvalue}\`, ${isJSXComponent ? true : false}, '${ref}', "${paramString}", ${params || null})}",`
230
232
 
231
233
  attribute[attributeName] = {
232
234
  old: old,
233
- new: newatribute,
235
+ new: isJSXComponent ? jsxAttribute : newatribute,
234
236
  attribute: attributeName,
235
237
  };
236
238
  attributesList.push({
@@ -262,9 +264,11 @@ function Compiler(func) {
262
264
  // since js is all in one line split it
263
265
  newvalue = newvalue.split('\n').map(line => line.trim() ? line.trim() + ';' : line).join('\n');
264
266
  let paramString = params ? params.split(' ').map(param => param + ',').join('') : "";
267
+ paramString = paramString.replaceAll(',,', ',')
268
+ let jsxAttribute = `${attributeName}=function(${paramString}){${newvalue}},`
265
269
  let newattribute = `${attributeName}="\${this.bind(\`${newvalue}\`, ${isJSXComponent ? true : false}, '${ref}', "${paramString}", ${params || null})}",`
266
270
  newattribute = newattribute.replace(/\s+/g, " ")
267
- string = string.replace(old, newattribute);
271
+ string = string.replace(old, isJSXComponent ? jsxAttribute : newattribute);
268
272
  }
269
273
  }
270
274
  }
@@ -331,7 +335,7 @@ function Compiler(func) {
331
335
  if (value && value.includes("={")) {
332
336
  value = value.replace("=", "");
333
337
  value == "undefined" ? (value = '"') : (value = value);
334
- console.log(value)
338
+
335
339
  key == 'style' ? value = `{this.parseStyle({${value.split('{{')[1].split('}}')[0]}})}` : null
336
340
 
337
341
 
@@ -566,10 +570,10 @@ function Compiler(func) {
566
570
  .replaceAll('=`', ':`')
567
571
  .replaceAll(`={\``, ':`')
568
572
  .replaceAll('`}', '`')
573
+ .replaceAll(",,", ',')
569
574
 
570
575
  .replaceAll(/=([A-z])/g, ":$1")
571
- props = props.replace(/:('[^']*'|"[^"]*")/g, ':$1,');
572
- props = props.replace(/:('[^']*'|"[^"]*")/g, ':$1,');
576
+ props = props.replace(/:('[^']*'|"[^"]*")/g, ':$1,');
573
577
  // ANY VALUE NUMBER BOOLEAN OR STRING
574
578
  props = props.replace(/=(\d+)/g, ':$1,');
575
579
  props = props.replaceAll(',,', ',')
@@ -651,11 +655,12 @@ async function Build() {
651
655
  let obj = {
652
656
  url: isBasePath ? '/' : aburl,
653
657
  pathname: `/pages/${origin.split('pages/')[1].split('.jsx')[0].replace('.jsx', '')}.jsx`,
658
+ fullpath: origin,
654
659
  };
655
660
 
656
661
  // Read and compile file content
657
662
  let data = await fs.readFileSync(origin, "utf8");
658
- data = Compiler(data);
663
+ data = Compiler(data)
659
664
 
660
665
  await writer(process.cwd() + "/dist/pages/" + fileName, data);
661
666
 
@@ -673,6 +678,12 @@ async function Build() {
673
678
  if (!before.includes(`//@desc ${obj.pathname}`)) {
674
679
  await writer(process.cwd() + "/dist/app.js", newfile);
675
680
  }
681
+
682
+ let beforeHTML = fs.existsSync(process.cwd() + "/dist/index.html") ? await reader(process.cwd() + "/dist/index.html") : '';
683
+ if(!beforeHTML.includes(`<link rel="prefetch" href="/pages/${origin.split('pages/')[1] }" as="fetch">`)){
684
+ let newHTML = beforeHTML + `\n<link rel="prefetch" href="/pages/${origin.split('pages/')[1] }" as="fetch">`
685
+ await writer(process.cwd() + "/dist/index.html", newHTML);
686
+ }
676
687
  }
677
688
 
678
689
 
@@ -705,7 +716,7 @@ async function Build() {
705
716
 
706
717
  let data = await reader(process.cwd() + "/src/" + name)
707
718
  if (name.includes('.jsx')) {
708
- data = Compiler(data);
719
+ data = Compiler(data)
709
720
  }
710
721
  bundleSize += fs.statSync(process.cwd() + "/src/" + name).size;
711
722
  await writer(process.cwd() + "/dist/src/" + name, data);