vaderjs 1.3.3-alpha-28 → 1.3.3-alpha-30

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.
Files changed (3) hide show
  1. package/package.json +1 -1
  2. package/runtime/router.js +57 -26
  3. package/vader.js +26 -14
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-28",
5
+ "version": "1.3.3-alpha-30",
6
6
  "bin": {
7
7
  "vader": "./vader.js"
8
8
  },
package/runtime/router.js CHANGED
@@ -101,9 +101,11 @@ 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
- // remove queries from this par
106
- params[0] = hashParts.slice(index).join('/').split('?')[0];
104
+ }else if(part.startsWith('*')){
105
+ let array = hashParts.slice(index)
106
+ array.forEach((i, index)=>{
107
+ params[index] = i
108
+ })
107
109
  }
108
110
  });
109
111
  return params;
@@ -131,38 +133,56 @@ class VaderRouter{
131
133
  handleRoute(hash) {
132
134
  hash = hash.slice(1);
133
135
  let status = 200;
136
+ let paramsCatchall = {}
134
137
  let route = this.routes.find((route) => {
135
-
136
138
  if (route.path === hash) {
137
139
  return true;
138
140
  }
139
- const routePathParts = route.path.split('/');
140
- const hashParts = hash.split('/');
141
- if (routePathParts.length !== hashParts.length) {
142
- return false;
143
- }else if(routePathParts[routePathParts.length-1].startsWith('*')){
141
+
142
+ if (route.path.includes('*') || route.path.includes(':')) {
143
+ const routeParts = route.path.split('/');
144
+ const hashParts = hash.split('/');
145
+
146
+ if (routeParts.length !== hashParts.length && !route.path.endsWith('*')) {
147
+ return false;
148
+ }
149
+
150
+ for (let index = 0; index < routeParts.length; index++) {
151
+ const routePart = routeParts[index];
152
+ const hashPart = hashParts[index];
153
+
154
+ if (routePart.startsWith(':') || routePart.startsWith('*')) {
155
+
156
+ continue;
157
+ }
158
+
159
+ if (routePart !== hashPart) {
160
+
161
+ return false;
162
+ }
163
+ }
164
+
144
165
  return true;
145
166
  }
146
- const params = this.extractParams( route.path, hash);
167
+
168
+ const params = this.extractParams(route.path, hash);
147
169
  return Object.keys(params).length > 0;
148
170
  });
149
171
 
172
+
150
173
  if (!route) {
151
- route = this.routes.find((route) => {
174
+ route = this.routes.find((errorRoute) => {
175
+ if (errorRoute.path === '/404') {
176
+ this.error = true;
177
+ return true;
178
+ } else if (!this.error && errorRoute.path !== '/404') {
152
179
 
153
- if(route.path === '/404'){
154
- return true;
155
- }else{
156
- window.location.hash = this.basePath
157
- }
180
+ }
158
181
  });
159
-
160
- route ? status = 200 :
161
-
162
- status = 404;
182
+
183
+ status = route ? 200 : 404;
163
184
  }
164
-
165
-
185
+
166
186
  const queryParams = this.extractQueryParams(hash);
167
187
  const params = route && route.path ? this.extractParams(route.path, hash) : {};
168
188
  const req = {
@@ -204,8 +224,8 @@ class VaderRouter{
204
224
  },
205
225
  render: async (/**@type {Component} */ Component, req, res) => {
206
226
 
207
- if(!Component.default || !Component.constructor){
208
- let message = !Component.default ? 'default' : 'constructor';
227
+ if(!Component || !Component.default || !Component.constructor){
228
+ let message = !Component || !Component.default ? 'default' : 'constructor';
209
229
  switch(message){
210
230
  case 'default':
211
231
  throw new Error(`Component must have a default export ex: return {default: Component}`);
@@ -225,6 +245,7 @@ class VaderRouter{
225
245
  if(!document.querySelector('#root')){
226
246
  throw new Error('Root element not found, please add an element with id root');
227
247
  }
248
+ Component.router.use = this.use.bind(this)
228
249
  Component.state = {}
229
250
  Component.reset();
230
251
  Component.components = {}
@@ -234,6 +255,16 @@ class VaderRouter{
234
255
  Component.bindMount();
235
256
  Component.onMount()
236
257
 
258
+
259
+ },
260
+ setQuery: (query) => {
261
+ let queryString = '';
262
+ Object.keys(query).forEach((key, index) => {
263
+ queryString += `${index === 0 ? '?' : '&'}${key}=${query[key]}`;
264
+ });
265
+ let route = window.location.hash.split('?')[0];
266
+ queryString = queryString.replace('/', '-').replaceAll('/', '-')
267
+ window.location.hash = `${route}${queryString}`;
237
268
  },
238
269
  send: (data) => {
239
270
  document.querySelector('#root').innerHTML = data;
@@ -250,16 +281,16 @@ class VaderRouter{
250
281
  }else{
251
282
  throw new Error('Selector must be a string');
252
283
  }
253
- },
284
+ }
254
285
  };
255
286
  this.middlewares.forEach((middleware) => {
256
287
  middleware(req, res);
257
288
  });
258
289
 
259
290
  route ? route.handler(req, res) : null;
260
-
261
291
  }
262
292
 
293
+
263
294
  }
264
295
 
265
296
  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
- .replaceAll(/=([A-z])/g, ":$1")
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 URL-related information from the file path
656
- let aburl = origin.split('pages')[1].split('.jsx')[0].replace('.jsx', '').replace('/index', '').replace('/_', '/:').replace('/[', '/:').replace(']', '');
657
- aburl.includes('[') ? aburl = '/' + aburl.split('[')[0].replace('/', '') : null;
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
- // Update 'app.js' file with routing logic
681
- let before = fs.existsSync(process.cwd() + "/dist/app.js") ? await reader(process.cwd() + "/dist/app.js") : '';
682
- let newfile = before + '\n' + js;
683
- if (!before.includes(`//@desc ${obj.pathname}`)) {
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}", {