vaderjs 1.3.3-alpha-28 → 1.3.3-alpha-29
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 +54 -26
- package/vader.js +26 -14
package/package.json
CHANGED
package/runtime/router.js
CHANGED
|
@@ -101,9 +101,8 @@ class VaderRouter{
|
|
|
101
101
|
if (part.startsWith(':')) {
|
|
102
102
|
const paramName = part.slice(1);
|
|
103
103
|
params[paramName] = hashParts[index];
|
|
104
|
-
}else if(part.startsWith('*')){
|
|
105
|
-
|
|
106
|
-
params[0] = hashParts.slice(index).join('/').split('?')[0];
|
|
104
|
+
}else if(part.startsWith('*')){
|
|
105
|
+
params[0] = hashParts.slice(index)
|
|
107
106
|
}
|
|
108
107
|
});
|
|
109
108
|
return params;
|
|
@@ -131,38 +130,56 @@ class VaderRouter{
|
|
|
131
130
|
handleRoute(hash) {
|
|
132
131
|
hash = hash.slice(1);
|
|
133
132
|
let status = 200;
|
|
133
|
+
let paramsCatchall = {}
|
|
134
134
|
let route = this.routes.find((route) => {
|
|
135
|
-
|
|
136
135
|
if (route.path === hash) {
|
|
137
136
|
return true;
|
|
138
137
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
138
|
+
|
|
139
|
+
if (route.path.includes('*') || route.path.includes(':')) {
|
|
140
|
+
const routeParts = route.path.split('/');
|
|
141
|
+
const hashParts = hash.split('/');
|
|
142
|
+
|
|
143
|
+
if (routeParts.length !== hashParts.length && !route.path.endsWith('*')) {
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
for (let index = 0; index < routeParts.length; index++) {
|
|
148
|
+
const routePart = routeParts[index];
|
|
149
|
+
const hashPart = hashParts[index];
|
|
150
|
+
|
|
151
|
+
if (routePart.startsWith(':') || routePart.startsWith('*')) {
|
|
152
|
+
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (routePart !== hashPart) {
|
|
157
|
+
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
144
162
|
return true;
|
|
145
163
|
}
|
|
146
|
-
|
|
164
|
+
|
|
165
|
+
const params = this.extractParams(route.path, hash);
|
|
147
166
|
return Object.keys(params).length > 0;
|
|
148
167
|
});
|
|
149
168
|
|
|
169
|
+
|
|
150
170
|
if (!route) {
|
|
151
|
-
route = this.routes.find((
|
|
171
|
+
route = this.routes.find((errorRoute) => {
|
|
172
|
+
if (errorRoute.path === '/404') {
|
|
173
|
+
this.error = true;
|
|
174
|
+
return true;
|
|
175
|
+
} else if (!this.error && errorRoute.path !== '/404') {
|
|
152
176
|
|
|
153
|
-
|
|
154
|
-
return true;
|
|
155
|
-
}else{
|
|
156
|
-
window.location.hash = this.basePath
|
|
157
|
-
}
|
|
177
|
+
}
|
|
158
178
|
});
|
|
159
|
-
|
|
160
|
-
route ?
|
|
161
|
-
|
|
162
|
-
status = 404;
|
|
179
|
+
|
|
180
|
+
status = route ? 200 : 404;
|
|
163
181
|
}
|
|
164
|
-
|
|
165
|
-
|
|
182
|
+
|
|
166
183
|
const queryParams = this.extractQueryParams(hash);
|
|
167
184
|
const params = route && route.path ? this.extractParams(route.path, hash) : {};
|
|
168
185
|
const req = {
|
|
@@ -204,8 +221,8 @@ class VaderRouter{
|
|
|
204
221
|
},
|
|
205
222
|
render: async (/**@type {Component} */ Component, req, res) => {
|
|
206
223
|
|
|
207
|
-
if(!Component.default || !Component.constructor){
|
|
208
|
-
let message = !Component.default ? 'default' : 'constructor';
|
|
224
|
+
if(!Component || !Component.default || !Component.constructor){
|
|
225
|
+
let message = !Component || !Component.default ? 'default' : 'constructor';
|
|
209
226
|
switch(message){
|
|
210
227
|
case 'default':
|
|
211
228
|
throw new Error(`Component must have a default export ex: return {default: Component}`);
|
|
@@ -225,6 +242,7 @@ class VaderRouter{
|
|
|
225
242
|
if(!document.querySelector('#root')){
|
|
226
243
|
throw new Error('Root element not found, please add an element with id root');
|
|
227
244
|
}
|
|
245
|
+
Component.router.use = this.use.bind(this)
|
|
228
246
|
Component.state = {}
|
|
229
247
|
Component.reset();
|
|
230
248
|
Component.components = {}
|
|
@@ -234,6 +252,16 @@ class VaderRouter{
|
|
|
234
252
|
Component.bindMount();
|
|
235
253
|
Component.onMount()
|
|
236
254
|
|
|
255
|
+
|
|
256
|
+
},
|
|
257
|
+
setQuery: (query) => {
|
|
258
|
+
let queryString = '';
|
|
259
|
+
Object.keys(query).forEach((key, index) => {
|
|
260
|
+
queryString += `${index === 0 ? '?' : '&'}${key}=${query[key]}`;
|
|
261
|
+
});
|
|
262
|
+
let route = window.location.hash.split('?')[0];
|
|
263
|
+
queryString = queryString.replace('/', '-').replaceAll('/', '-')
|
|
264
|
+
window.location.hash = `${route}${queryString}`;
|
|
237
265
|
},
|
|
238
266
|
send: (data) => {
|
|
239
267
|
document.querySelector('#root').innerHTML = data;
|
|
@@ -250,16 +278,16 @@ class VaderRouter{
|
|
|
250
278
|
}else{
|
|
251
279
|
throw new Error('Selector must be a string');
|
|
252
280
|
}
|
|
253
|
-
}
|
|
281
|
+
}
|
|
254
282
|
};
|
|
255
283
|
this.middlewares.forEach((middleware) => {
|
|
256
284
|
middleware(req, res);
|
|
257
285
|
});
|
|
258
286
|
|
|
259
287
|
route ? route.handler(req, res) : null;
|
|
260
|
-
|
|
261
288
|
}
|
|
262
289
|
|
|
290
|
+
|
|
263
291
|
}
|
|
264
292
|
|
|
265
293
|
window.VaderRouter = VaderRouter;
|
package/vader.js
CHANGED
|
@@ -576,8 +576,8 @@ function Compiler(func) {
|
|
|
576
576
|
.replaceAll(`={\``, ':`')
|
|
577
577
|
.replaceAll('`}', '`')
|
|
578
578
|
.replaceAll(",,", ',')
|
|
579
|
-
|
|
580
|
-
|
|
579
|
+
.replaceAll(/=(?=(?:(?:[^"']*["'][^"']*['"])*[^"']*$))/g, ':');
|
|
580
|
+
|
|
581
581
|
props = props.replace(/:('[^']*'|"[^"]*")/g, ':$1,');
|
|
582
582
|
// ANY VALUE NUMBER BOOLEAN OR STRING
|
|
583
583
|
props = props.replace(/=(\d+)/g, ':$1,');
|
|
@@ -646,29 +646,42 @@ async function Build() {
|
|
|
646
646
|
});
|
|
647
647
|
|
|
648
648
|
// Process files in the 'pages' directory
|
|
649
|
+
let appjs = '';
|
|
650
|
+
let hasWritten = []
|
|
651
|
+
const writejs = () =>{
|
|
652
|
+
writer(process.cwd() + '/dist/app.js', appjs)
|
|
653
|
+
}
|
|
649
654
|
for await (let file of glb) {
|
|
650
655
|
// Normalize file paths
|
|
651
656
|
let origin = file.replace(/\\/g, '/');
|
|
652
657
|
let fileName = origin.split('/pages/')[1].split('.jsx')[0].replace('.jsx', '') + '.jsx';
|
|
653
658
|
let isBasePath = fileName === 'index.jsx';
|
|
654
659
|
|
|
655
|
-
// Extract
|
|
656
|
-
let aburl =
|
|
657
|
-
|
|
660
|
+
// Extract all dynamic parameters from the file path [param1]/[param2]/[param3
|
|
661
|
+
let aburl = origin.split('/pages')[1].split('.jsx')[0].replace('.jsx', '').split('[').join(':').split(']').join('');
|
|
662
|
+
|
|
663
|
+
if(aburl.includes('...')){
|
|
664
|
+
// this is a catch all route
|
|
665
|
+
// it should be /pages/[...]/index.jsx or /pages/[...].jsx
|
|
666
|
+
aburl = aburl.split('...').join('*').split(':*').join('*')
|
|
667
|
+
aburl = aburl.replaceAll('./index', '')
|
|
668
|
+
|
|
669
|
+
}
|
|
658
670
|
|
|
659
671
|
// Create an object with URL and pathname properties
|
|
660
672
|
let obj = {
|
|
661
|
-
url: isBasePath ? '/' : aburl,
|
|
673
|
+
url: isBasePath ? '/' : aburl.replaceAll('index', ''),
|
|
662
674
|
pathname: `/pages/${origin.split('pages/')[1].split('.jsx')[0].replace('.jsx', '')}.jsx`,
|
|
663
675
|
fullpath: origin,
|
|
664
676
|
};
|
|
665
677
|
|
|
678
|
+
|
|
666
679
|
// Read and compile file content
|
|
667
680
|
let data = await fs.readFileSync(origin, "utf8");
|
|
668
681
|
data = Compiler(data)
|
|
669
682
|
|
|
670
683
|
await writer(process.cwd() + "/dist/pages/" + fileName, data);
|
|
671
|
-
|
|
684
|
+
|
|
672
685
|
// Generate routing logic
|
|
673
686
|
let js = `
|
|
674
687
|
router.get('${obj.url}', async (req, res) => {
|
|
@@ -676,13 +689,11 @@ async function Build() {
|
|
|
676
689
|
})
|
|
677
690
|
//@desc ${obj.pathname}
|
|
678
691
|
` + '\n';
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
await writer(process.cwd() + "/dist/app.js", newfile);
|
|
685
|
-
}
|
|
692
|
+
appjs += js
|
|
693
|
+
|
|
694
|
+
writejs()
|
|
695
|
+
|
|
696
|
+
|
|
686
697
|
|
|
687
698
|
let beforeHTML = fs.existsSync(process.cwd() + "/dist/index.html") ? await reader(process.cwd() + "/dist/index.html") : '';
|
|
688
699
|
if(!beforeHTML.includes(`<link rel="prefetch" href="/pages/${origin.split('pages/')[1] }" as="fetch">`)){
|
|
@@ -690,6 +701,7 @@ async function Build() {
|
|
|
690
701
|
await writer(process.cwd() + "/dist/index.html", newHTML);
|
|
691
702
|
}
|
|
692
703
|
}
|
|
704
|
+
|
|
693
705
|
|
|
694
706
|
|
|
695
707
|
const scannedSourceFiles = await glob("**/**.{jsx,js}", {
|