vaderjs 1.4.5 → 1.4.7

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/README.MD CHANGED
@@ -45,7 +45,7 @@ Keyword folders - all files are passed from these folders to the build folder
45
45
  # Define your config
46
46
 
47
47
  ```ts
48
- import defineConfig from "../config";
48
+ import defineConfig from "vaderjs/config";
49
49
 
50
50
  export default defineConfig({
51
51
  port: 3000,
package/document/index.ts CHANGED
@@ -21,7 +21,7 @@ export const document = (element: any) => {
21
21
  continue;
22
22
  }
23
23
  //@ts-ignore
24
- if (key.includes("on")) {
24
+ if (key.startsWith("on")){
25
25
  continue;
26
26
  }
27
27
  el += ` ${key}="${attributes[key]}"`;
package/index.ts CHANGED
@@ -14,6 +14,9 @@ declare global {
14
14
  state: any;
15
15
  }
16
16
  const genKey: any;
17
+ /**
18
+ * @description Allows you to check if current session is server or client
19
+ */
17
20
  let isServer: boolean;
18
21
  /**
19
22
  * @description - The params object is used to store the query parameters of the current URL
@@ -27,6 +30,7 @@ declare global {
27
30
  * console.log(params.age) // 20
28
31
  */
29
32
  let params: { [key: string]: string };
33
+ let localStorage : []
30
34
  }
31
35
  //@ts-ignore
32
36
  globalThis.isServer = typeof window === "undefined";
@@ -39,6 +43,7 @@ globalThis.params = {
39
43
  },
40
44
  };
41
45
 
46
+
42
47
 
43
48
  /**
44
49
  * @description useFetch allows you to make POST - GET - PUT - DELETE requests then returns the data, loading state and error
@@ -130,6 +135,8 @@ export class Component {
130
135
  this.effect = [];
131
136
  this.Mounted = false;
132
137
  this.element = null;
138
+
139
+
133
140
  }
134
141
  setState(newState: any, Element: string) {
135
142
  globalThis.window.state[this.key] = { ...this.state, ...newState };
@@ -149,19 +156,32 @@ export class Component {
149
156
  }
150
157
  }
151
158
  }
152
- useState <T> (key: string, defaultValue: T) {
153
- let value = sessionStorage.getItem("state_" + key) || defaultValue;
154
- typeof value === "string" ? value = JSON.parse(value) : value;
155
-
156
- window.onbeforeunload = function() {
157
- sessionStorage.removeItem("state_" + key);
158
- }
159
- const setValue = (newValue: T ,element: string) => {
160
- value = newValue;
161
- sessionStorage.setItem("state_" + key, JSON.stringify(value));
162
- this.state[key] = value;
163
- this.forceUpdate(element)
159
+ useState<T>(key: string, defaultValue: T) {
160
+ let value = sessionStorage.getItem("state_" + key) ? JSON.parse(sessionStorage.getItem("state_" + key)).value : defaultValue;
161
+
162
+ // Parse value if it's a stringified object or number
163
+ if (typeof value === 'string') {
164
+ try {
165
+ value = JSON.parse(value);
166
+ } catch (error) {
167
+ // Not a valid JSON, keep it as is
168
+ }
169
+ }
170
+
171
+ // Add listener for unload event to save state
172
+ if (!window['listener' + key]) {
173
+ window['listener' + key] = true;
174
+ window.addEventListener('beforeunload', () => {
175
+ sessionStorage.removeItem('state_' + key)
176
+ });
164
177
  }
178
+
179
+ const setValue = (newValue: T) => {
180
+ value = newValue;
181
+ sessionStorage.setItem("state_" + key, JSON.stringify({ type: typeof newValue, value: newValue }));
182
+ this.forceUpdate(this.key)
183
+ };
184
+
165
185
  return [value, setValue];
166
186
  }
167
187
 
@@ -206,38 +226,39 @@ export class Component {
206
226
  forceUpdate(key) {
207
227
  //@ts-ignore
208
228
  let el = Array.from(document.querySelectorAll("*")).filter((el2: any) =>{ return el2.key === key})[0];
209
- let newl = this.parseToElement(this.render());
229
+ let newl = this.toElement();
210
230
  if(newl.key !== key){
211
231
  //@ts-ignore
212
232
  newl = Array.from(newl.children).filter((el2) => el2.key === key)[0];
213
- }
214
- if (this.Reconciler.shouldUpdate(el, newl)) {
215
- el.replaceWith(newl);
216
- }
233
+ }
234
+ this.Reconciler.update(el, newl);
217
235
  }
218
236
  Reconciler = {
219
- shouldUpdate(oldElement, newElement) {
220
- if(oldElement.outerHTML === newElement.outerHTML){
221
- return false;
222
- }
223
- let attributes = oldElement.attributes;
224
- let newAttributes = newElement.attributes;
225
- for (let i = 0; i < attributes.length; i++) {
226
- let attribute = attributes[i];
227
- if (attribute.name === "key") {
228
- continue;
229
- }
230
- if (attribute.name === "class") {
231
- if (attribute.value !== newElement.className) {
232
- return true;
233
- }
234
- continue;
235
- }
236
- if (attribute.value !== newAttributes[attribute.name]) {
237
- return true;
237
+ update: (oldElement, newElement) => {
238
+ if(!oldElement || !newElement) return;
239
+ if (this.Reconciler.shouldUpdate(oldElement, newElement) && oldElement.tagName == newElement.tagName){
240
+ oldElement.replaceWith(newElement)
241
+ } else {
242
+ let children = oldElement.childNodes;
243
+ for (let i = 0; i < children.length; i++) {
244
+ this.Reconciler.update(children[i], newElement.childNodes[i]);
238
245
  }
239
246
  }
240
- return true;
247
+ },
248
+ shouldUpdate(oldElement, newElement) {
249
+ if (oldElement.nodeType !== newElement.nodeType) {
250
+ return true;
251
+ }
252
+ if (oldElement.nodeType === 3 && newElement.nodeType === 3) {
253
+ return oldElement.textContent !== newElement.textContent;
254
+ }
255
+ if (oldElement.nodeName !== newElement.nodeName) {
256
+ return true;
257
+ }
258
+ if (oldElement.childNodes.length !== newElement.childNodes.length) {
259
+ return true;
260
+ }
261
+ return false;
241
262
  }
242
263
  };
243
264
 
@@ -301,7 +322,12 @@ export class Component {
301
322
  }
302
323
  toElement() {
303
324
  let children = this.render();
325
+ //@ts-ignore
326
+ if(children.props['key']){
327
+ this.key = children.props['key'];
328
+ }
304
329
  let el = this.parseToElement(children);
330
+ el.key = this.key;
305
331
  return el;
306
332
  }
307
333
  render() {
@@ -335,7 +361,7 @@ export function render(element: any, container) {
335
361
  let memoizedInstance = memoizeClassComponent(Component);
336
362
  memoizedInstance.Mounted = true;
337
363
  memoizedInstance.render = element.bind(memoizedInstance);
338
- let el = memoizedInstance.toElement();
364
+ let el = memoizedInstance.toElement();
339
365
  container.innerHTML = "";
340
366
  container.replaceWith(el);
341
367
 
package/main.js CHANGED
@@ -12,12 +12,12 @@ if (!fs.existsSync(process.cwd() + '/app')) {
12
12
  if (!fs.existsSync(process.cwd() + '/public')) {
13
13
  fs.mkdirSync(process.cwd() + '/public')
14
14
  }
15
- const mode = args.includes('dev') ? 'development' : args.includes('prod') ? 'production' : null
15
+ const mode = args.includes('dev') ? 'development' : args.includes('prod') || args.includes('build') ? 'production' : null
16
16
  if(!mode){
17
17
  console.log(`
18
18
  Usage:
19
19
  bun vaderjs dev - Start development server output in dist/
20
- bun vaderjs prod - Build for production output in dist/
20
+ bun vaderjs prod - Build for production output in dist/
21
21
  `)
22
22
  process.exit(1)
23
23
  }
@@ -98,7 +98,11 @@ const handleReplacements = (code, file) => {
98
98
 
99
99
  async function generateApp() {
100
100
  // remove files from dist
101
+ if(mode === 'development'){
101
102
  fs.rmdirSync(process.cwd() + '/dist', { recursive: true })
103
+ }else{
104
+ fs.mkdirSync(process.cwd() + '/dist', { recursive: true })
105
+ }
102
106
  return new Promise(async (resolve, reject) => {
103
107
  let routes = new Bun.FileSystemRouter({
104
108
  dir: process.cwd() + '/app',
@@ -196,7 +200,6 @@ async function generateApp() {
196
200
  fs.mkdirSync(process.cwd() + '/dist/src/vader', { recursive: true })
197
201
  fs.writeFileSync(process.cwd() + '/dist/src/vader/index.js', await new Bun.Transpiler({
198
202
  loader: 'ts',
199
- minify: true,
200
203
  }).transformSync(await Bun.file(require.resolve('vaderjs')).text()))
201
204
 
202
205
  const spawn = Bun.spawn({
@@ -266,7 +269,7 @@ async function generateApp() {
266
269
 
267
270
  }
268
271
 
269
- generateApp()
272
+ await generateApp()
270
273
  function handleFiles(){
271
274
  return new Promise(async (resolve, reject) => {
272
275
  try {
@@ -274,6 +277,9 @@ function handleFiles(){
274
277
  for await (var i of glob.scan()){
275
278
  let file = i
276
279
  fs.mkdirSync(path.join(process.cwd() + '/dist', path.dirname(file)), { recursive: true })
280
+ if(fs.existsSync(path.join(process.cwd() + '/dist', file))){
281
+ fs.rmSync(path.join(process.cwd() + '/dist', file))
282
+ }
277
283
  fs.copyFileSync(file, path.join(process.cwd() + '/dist', file))
278
284
  }
279
285
  resolve()
@@ -282,14 +288,14 @@ function handleFiles(){
282
288
  }
283
289
  })
284
290
  }
285
- handleFiles()
291
+ await handleFiles()
286
292
  let isBuilding = false;
287
293
  let timeout = null
288
294
  if(mode === 'development'){
289
295
  const watcher = fs.watch(path.join(process.cwd() + '/app'), { recursive: true })
290
296
  const publicWatcher = fs.watch(path.join(process.cwd() + '/public'), { recursive: true })
291
297
  publicWatcher.on('change', async (event, filename) => {
292
- try {
298
+ try {
293
299
  await handleFiles()
294
300
  clients.forEach(c => {
295
301
  c.send('reload')
@@ -300,8 +306,7 @@ if(mode === 'development'){
300
306
  })
301
307
  watcher.on('change', async (event, filename) => {
302
308
  try {
303
- await generateApp()
304
-
309
+ await generateApp()
305
310
  handleFiles()
306
311
  clients.forEach(c => {
307
312
  c.send('reload')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vaderjs",
3
- "version": "1.4.5",
3
+ "version": "1.4.7",
4
4
  "description": "A simple and powerful JavaScript library for building modern web applications.",
5
5
  "main":"./index.js",
6
6
  "bin": {