vaderjs 1.3.3-alpha-37 → 1.3.3-alpha-39
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 +1 -1
- package/runtime/router.js +61 -36
- package/runtime/vader.js +20 -8
- package/vader.js +19 -13
package/package.json
CHANGED
package/runtime/router.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Component } from "./vader.js";
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
let middlewares = [];
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
|
|
@@ -55,6 +55,7 @@ class VaderRouter{
|
|
|
55
55
|
*/
|
|
56
56
|
|
|
57
57
|
use(/* path, */ middleware) {
|
|
58
|
+
console.log(middleware)
|
|
58
59
|
this.middlewares.push(middleware);
|
|
59
60
|
}
|
|
60
61
|
|
|
@@ -226,45 +227,69 @@ class VaderRouter{
|
|
|
226
227
|
});
|
|
227
228
|
}
|
|
228
229
|
},
|
|
230
|
+
refresh: () => {
|
|
231
|
+
this.handleRoute(window.location.hash);
|
|
232
|
+
},
|
|
229
233
|
redirect: (path) => {
|
|
230
234
|
!path.startsWith('/') ? path = `/${path}` : null;
|
|
231
235
|
window.location.hash = `#${path}`;
|
|
232
236
|
},
|
|
233
237
|
render: async (/**@type {Component} */ Component, req, res) => {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
238
|
+
try {
|
|
239
|
+
// Check if the component is valid
|
|
240
|
+
if (!Component || !Component.default || !Component.constructor) {
|
|
241
|
+
let message = !Component || !Component.default ? 'default' : 'constructor';
|
|
242
|
+
switch (message) {
|
|
243
|
+
case 'default':
|
|
244
|
+
throw new Error(`Component must have a default export ex: return {default: Component}`);
|
|
245
|
+
case 'constructor':
|
|
246
|
+
throw new Error(`Component is invalid, please check the constructor`);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// Create an instance of the component
|
|
251
|
+
Component = Component.default ? new Component.default() : Component.constructor ? new Component() : Component;
|
|
252
|
+
|
|
253
|
+
// Set the 'mounted' flag to true
|
|
254
|
+
Component.mounted = true;
|
|
255
|
+
|
|
256
|
+
// Check if the root element exists
|
|
257
|
+
if (!document.querySelector('#root')) {
|
|
258
|
+
throw new Error('Root element not found, please add an element with id root');
|
|
244
259
|
}
|
|
260
|
+
|
|
261
|
+
// Reset component state
|
|
262
|
+
Component.reset();
|
|
263
|
+
Component.components = {};
|
|
264
|
+
Component.request = req;
|
|
265
|
+
Component.response = res;
|
|
266
|
+
|
|
267
|
+
// Check if the component has a router and is not a child component
|
|
268
|
+
if (Component.router.use && !Component.isChild) {
|
|
269
|
+
// Allow pausing the route and run code before rendering
|
|
270
|
+
await new Promise(async (resolve) => {
|
|
271
|
+
await Component.router.use(req, res)
|
|
272
|
+
if(req.pause){
|
|
273
|
+
let timer = setInterval(() => {
|
|
274
|
+
if(!req.pause){
|
|
275
|
+
resolve();
|
|
276
|
+
clearInterval(timer);
|
|
277
|
+
}
|
|
278
|
+
}, 1000);
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
} else if (Component.router.use && Component.isChild) {
|
|
282
|
+
console.warn('Router.use() is not supported in child components');
|
|
283
|
+
}
|
|
284
|
+
const renderedContent = await Component.render();
|
|
285
|
+
document.querySelector('#root').innerHTML = renderedContent;
|
|
286
|
+
Component.bindMount();
|
|
287
|
+
Component.onMount();
|
|
245
288
|
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
Component.mounted = true;
|
|
252
|
-
|
|
253
|
-
if(!document.querySelector('#root')){
|
|
254
|
-
throw new Error('Root element not found, please add an element with id root');
|
|
255
|
-
}
|
|
256
|
-
Component.router.use = this.use.bind(this)
|
|
257
|
-
Component.state = {}
|
|
258
|
-
Component.reset();
|
|
259
|
-
Component.components = {}
|
|
260
|
-
Component.request = req;
|
|
261
|
-
Component.response = res;
|
|
262
|
-
document.querySelector('#root').innerHTML = Component.render()
|
|
263
|
-
Component.bindMount();
|
|
264
|
-
Component.onMount()
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
},
|
|
289
|
+
} catch (error) {
|
|
290
|
+
console.error(error);
|
|
291
|
+
}
|
|
292
|
+
},
|
|
268
293
|
setQuery: (query) => {
|
|
269
294
|
let queryString = '';
|
|
270
295
|
Object.keys(query).forEach((key, index) => {
|
|
@@ -290,8 +315,8 @@ class VaderRouter{
|
|
|
290
315
|
throw new Error('Selector must be a string');
|
|
291
316
|
}
|
|
292
317
|
}
|
|
293
|
-
};
|
|
294
|
-
|
|
318
|
+
};
|
|
319
|
+
middlewares.forEach((middleware) => {
|
|
295
320
|
middleware(req, res);
|
|
296
321
|
});
|
|
297
322
|
|
|
@@ -300,7 +325,7 @@ class VaderRouter{
|
|
|
300
325
|
|
|
301
326
|
|
|
302
327
|
}
|
|
303
|
-
|
|
328
|
+
|
|
304
329
|
window.VaderRouter = VaderRouter;
|
|
305
330
|
|
|
306
331
|
export default VaderRouter;
|
package/runtime/vader.js
CHANGED
|
@@ -165,7 +165,11 @@ export class Component {
|
|
|
165
165
|
this.freeMemoryFromFunctions();
|
|
166
166
|
this.checkIFMounted();
|
|
167
167
|
this.memoizes = []
|
|
168
|
-
|
|
168
|
+
/**
|
|
169
|
+
* @type {boolean}
|
|
170
|
+
* @description Indicates if the component is a child component
|
|
171
|
+
*/
|
|
172
|
+
this.isChild = false;
|
|
169
173
|
this.vdom = []
|
|
170
174
|
|
|
171
175
|
this.children = []
|
|
@@ -281,6 +285,8 @@ export class Component {
|
|
|
281
285
|
comp.parentNode = this;
|
|
282
286
|
comp.request = this.request;
|
|
283
287
|
comp.response = this.response;
|
|
288
|
+
comp.isChild = true;
|
|
289
|
+
delete comp.router
|
|
284
290
|
comp.key = props.key || null;
|
|
285
291
|
|
|
286
292
|
if(!this.components[props.key]){
|
|
@@ -615,7 +621,7 @@ export class Component {
|
|
|
615
621
|
if (!this.state[key]) {
|
|
616
622
|
this.state[key] = initialState;
|
|
617
623
|
}
|
|
618
|
-
const getValue = () => this.
|
|
624
|
+
const getValue = () => document.querySelector(`[ref="${key + this.key}"]`) || initialState;
|
|
619
625
|
const set = (newValue) => {
|
|
620
626
|
this.state[key] = newValue;
|
|
621
627
|
this.hydrate();
|
|
@@ -624,7 +630,7 @@ export class Component {
|
|
|
624
630
|
|
|
625
631
|
return {
|
|
626
632
|
bind: key + this.key,
|
|
627
|
-
current:
|
|
633
|
+
current: getValue(),
|
|
628
634
|
}
|
|
629
635
|
}
|
|
630
636
|
|
|
@@ -677,22 +683,23 @@ let cache = {};
|
|
|
677
683
|
* @param {Boolean} noresolve - used to tell if the path should be automatically handled or manually handled - this is false by default
|
|
678
684
|
* @returns
|
|
679
685
|
*/
|
|
680
|
-
export const require = async (path,
|
|
686
|
+
export const require = async (path, options) => {
|
|
681
687
|
|
|
682
688
|
if (cache[path]) {
|
|
683
689
|
return cache[path];
|
|
684
690
|
}
|
|
685
691
|
let file = ''
|
|
686
692
|
try {
|
|
687
|
-
file = await fetch(path).then((res) => res.text());
|
|
688
|
-
|
|
693
|
+
file = await fetch(path).then((res) => options && options.type === 'json' ? res.json() : res.text());
|
|
694
|
+
|
|
689
695
|
} catch (error) {
|
|
690
696
|
console.error(error)
|
|
691
697
|
}
|
|
692
698
|
|
|
693
|
-
|
|
699
|
+
|
|
694
700
|
|
|
695
701
|
let filetype = path.split(".").pop();
|
|
702
|
+
filetype !== "json" ? file = file + `\n//# sourceURL=${path}\n` : null;
|
|
696
703
|
switch (true) {
|
|
697
704
|
case filetype === "js":
|
|
698
705
|
let exports = file.match(/module.exports\s*=\s*{.*}/gs) || file.match(/exports\s*=\s*{.*}/gs);
|
|
@@ -712,6 +719,10 @@ export const require = async (path, noresolve = false) => {
|
|
|
712
719
|
|
|
713
720
|
cache[path] = new Function(`return (async () => { ${file} })()`)();
|
|
714
721
|
return cache[path]
|
|
722
|
+
case filetype === "json":
|
|
723
|
+
cache[path] = file
|
|
724
|
+
return cache[path]
|
|
725
|
+
|
|
715
726
|
case filetype === "jsx":
|
|
716
727
|
cache[path] = new Function(`return (async () => { ${file} })()`)();
|
|
717
728
|
return cache[path]
|
|
@@ -787,11 +798,12 @@ export const useRef = (initialState) => {
|
|
|
787
798
|
return {
|
|
788
799
|
/**
|
|
789
800
|
* @description The current value of the ref.
|
|
790
|
-
|
|
801
|
+
@type {*}
|
|
791
802
|
*/
|
|
792
803
|
current: initialState,
|
|
793
804
|
/**
|
|
794
805
|
* @description A unique string that can be used to bind the ref to an element.
|
|
806
|
+
* @type {HTMLElement|string}
|
|
795
807
|
*/
|
|
796
808
|
bind: '',
|
|
797
809
|
};
|
package/vader.js
CHANGED
|
@@ -662,9 +662,12 @@ function Compiler(func) {
|
|
|
662
662
|
let exportss = {}
|
|
663
663
|
string.split('\n').forEach(line => {
|
|
664
664
|
if(line.includes('import')){
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
665
|
+
// Regular expression for matching import() statements
|
|
666
|
+
let asyncimportMatch = line.match(/import\s*\((.*)\)/gs);
|
|
667
|
+
|
|
668
|
+
// Regular expression for matching regular import statements excluding lines with HTML elements
|
|
669
|
+
let regularimportMatch = line.match(/import\s+([\w\s{},]*)\s*from\s*(['"][^'"]+['"])(?![^<]*>)/gs);
|
|
670
|
+
|
|
668
671
|
if(asyncimportMatch){
|
|
669
672
|
asyncimportMatch.forEach(async (match) => {
|
|
670
673
|
let beforeimport = match
|
|
@@ -672,8 +675,8 @@ function Compiler(func) {
|
|
|
672
675
|
let newImport = ''
|
|
673
676
|
switch(true){
|
|
674
677
|
case path && path.includes('json'):
|
|
675
|
-
|
|
676
|
-
newImport = `
|
|
678
|
+
path = path.replace(';', '')
|
|
679
|
+
newImport = `JSON.parse(require(${path}))`
|
|
677
680
|
let htmlPrefetch = `<link rel="prefetch" href="${path}" as="fetch">`
|
|
678
681
|
let beforeHTML = fs.existsSync(process.cwd() + "/dist/index.html") ? fs.readFileSync(process.cwd() + "/dist/index.html", "utf8") : '';
|
|
679
682
|
if(!beforeHTML.includes(htmlPrefetch)){
|
|
@@ -691,14 +694,15 @@ function Compiler(func) {
|
|
|
691
694
|
|
|
692
695
|
if(regularimportMatch){
|
|
693
696
|
regularimportMatch.forEach(async (match) => {
|
|
697
|
+
|
|
694
698
|
let beforeimport = match
|
|
695
699
|
let path = match.split('from')[1].trim()
|
|
696
700
|
let newImport = ''
|
|
697
701
|
let name = match.split('import')[1].split('from')[0].trim()
|
|
698
702
|
switch(true){
|
|
699
703
|
case path && path.includes('json'):
|
|
700
|
-
|
|
701
|
-
newImport = `let ${name} = await
|
|
704
|
+
path = path.replace(';', '')
|
|
705
|
+
newImport = `let ${name} = await require(${path}, {type: 'json'})`
|
|
702
706
|
let htmlPrefetch = `<link rel="prefetch" href="${path.replace(/'/g, '')}" as="fetch">`
|
|
703
707
|
let beforeHTML = fs.existsSync(process.cwd() + "/dist/index.html") ? fs.readFileSync(process.cwd() + "/dist/index.html", "utf8") : '';
|
|
704
708
|
if(!beforeHTML.includes(htmlPrefetch)){
|
|
@@ -707,6 +711,7 @@ function Compiler(func) {
|
|
|
707
711
|
}
|
|
708
712
|
break;
|
|
709
713
|
case path && path.includes('.jsx'):
|
|
714
|
+
|
|
710
715
|
newImport = `let ${name} = await require(${path})`
|
|
711
716
|
break;
|
|
712
717
|
default:
|
|
@@ -718,15 +723,14 @@ function Compiler(func) {
|
|
|
718
723
|
}
|
|
719
724
|
})
|
|
720
725
|
}
|
|
721
|
-
}else if (line.includes('export')) {
|
|
726
|
+
}else if (line.includes('export') && !line.includes('>') && !line.includes('<')) {
|
|
722
727
|
let b4line = line;
|
|
723
728
|
let exports = line.split('export')[1].trim();
|
|
724
729
|
let isDefault = exports.includes('default');
|
|
725
730
|
|
|
726
731
|
let name = ''
|
|
727
732
|
switch (true) {
|
|
728
|
-
case exports && isDefault:
|
|
729
|
-
console.log('default')
|
|
733
|
+
case exports && isDefault:
|
|
730
734
|
|
|
731
735
|
let expt = exports.split('default')[1].trim();
|
|
732
736
|
// Check if it's a class definition
|
|
@@ -736,7 +740,7 @@ function Compiler(func) {
|
|
|
736
740
|
let className = match ? match[0].split('class')[1].split('extends')[0].trim() : expt.split('class')[1].split('{')[0].trim();
|
|
737
741
|
name = className
|
|
738
742
|
|
|
739
|
-
exportss['default'] = className
|
|
743
|
+
exportss[isDefault ? 'default' : className] = className
|
|
740
744
|
} else if (expt.includes('function')) {
|
|
741
745
|
let funcName = expt.split('function')[1].split('(')[0].trim();
|
|
742
746
|
name = funcName
|
|
@@ -767,8 +771,10 @@ function Compiler(func) {
|
|
|
767
771
|
|
|
768
772
|
|
|
769
773
|
})
|
|
770
|
-
if(exportss){
|
|
771
|
-
|
|
774
|
+
if(exportss){
|
|
775
|
+
let exports = Object.keys(exportss).map(key => key + ':' + exportss[key]).join(',')
|
|
776
|
+
|
|
777
|
+
Object.keys(exportss).length > 1 ? string += `\n\n return {${exports}}` : string += `\n\nreturn ${Object.keys(exportss) == 'default' ? `{default: ${Object.values(exportss)[0]}}` : `${Object.keys(exportss)[0]}`}`
|
|
772
778
|
}
|
|
773
779
|
|
|
774
780
|
return string;
|