olum-compiler 0.2.7 → 0.5.0

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 (59) hide show
  1. package/LICENSE.md +1 -1
  2. package/README.md +1 -1
  3. package/bin/build.js +9 -0
  4. package/bin/compiler.js +23 -0
  5. package/bin/copySrc.js +22 -0
  6. package/bin/dev.js +47 -0
  7. package/bin/index.js +1 -24
  8. package/bin/mimes.json +340 -0
  9. package/bin/server.js +77 -0
  10. package/bin/watcher.js +6 -0
  11. package/bin/ws.js +54 -0
  12. package/core/app.js +530 -0
  13. package/lib/attr.js +39 -0
  14. package/lib/clean.js +4 -0
  15. package/lib/colors.js +72 -0
  16. package/lib/compPlaceholder.js +10 -0
  17. package/lib/compiler.js +21 -0
  18. package/lib/cssParser.js +87 -0
  19. package/lib/domEvents.js +112 -0
  20. package/lib/events.js +48 -0
  21. package/lib/for.js +71 -0
  22. package/lib/getImports.js +48 -0
  23. package/lib/getTempPath.js +13 -0
  24. package/lib/helpers.js +509 -0
  25. package/lib/htmlParser.js +71 -0
  26. package/lib/if.js +65 -0
  27. package/lib/jsParser.js +31 -0
  28. package/lib/lineCounter.js +31 -0
  29. package/lib/log.txt +0 -0
  30. package/lib/mode.js +12 -0
  31. package/lib/models.js +51 -0
  32. package/lib/parser.js +170 -0
  33. package/lib/placeholder.js +76 -0
  34. package/lib/regex.js +12 -0
  35. package/lib/show.js +22 -0
  36. package/lib/sourcemap.js +34 -0
  37. package/lib/styles.js +32 -0
  38. package/lib/toJs.js +12 -0
  39. package/lib/tokens.js +14 -0
  40. package/package.json +7 -39
  41. package/babel.config.json +0 -4
  42. package/compiler.js +0 -486
  43. package/tasks/build.js +0 -24
  44. package/tasks/bundle.js +0 -33
  45. package/tasks/catchall.js +0 -39
  46. package/tasks/clean.js +0 -18
  47. package/tasks/compile.js +0 -32
  48. package/tasks/copy-build.js +0 -33
  49. package/tasks/desktop.js +0 -63
  50. package/tasks/dev.js +0 -18
  51. package/tasks/index.js +0 -12
  52. package/tasks/linux.js +0 -255
  53. package/tasks/logger.js +0 -19
  54. package/tasks/mac.js +0 -8
  55. package/tasks/reload.js +0 -7
  56. package/tasks/server.js +0 -26
  57. package/tasks/watch.js +0 -44
  58. package/tasks/windows.js +0 -8
  59. package/webpack.config.js +0 -152
package/core/app.js ADDED
@@ -0,0 +1,530 @@
1
+ window.olum = {
2
+ app: {}, // this is a must to access live changes
3
+ $emit(event, data) {
4
+ this.dispatchEvent(event, data);
5
+ },
6
+ dispatchEvent(event, data) {
7
+ window.dispatchEvent(new CustomEvent(event, { detail: data }));
8
+ },
9
+ mkElm(type, compName, compId) {
10
+ const el = document.createElement(type);
11
+ if (compName && compId) el.setAttribute("data-olum", JSON.stringify({ compName, compId }));
12
+ return el;
13
+ },
14
+ injectStyle(compName, cssContent) {
15
+ if (!cssContent || !cssContent.trim()) return;
16
+ const id = "olum-style-" + compName;
17
+ if (document.getElementById(id)) return;
18
+ const tag = document.createElement("style");
19
+ tag.id = id;
20
+ tag.textContent = cssContent;
21
+ document.head.appendChild(tag);
22
+ },
23
+ proxyHandler(obj, watcher, el) {
24
+ function mkHash(str) {
25
+ var hash = 0;
26
+ var i;
27
+ var char;
28
+ if (str.length === 0) return hash;
29
+ for (i = 0; i < str.length; i++) {
30
+ char = str.charCodeAt(i);
31
+ hash = (hash << 5) - hash + char;
32
+ hash |= 0; // Convert to 32bit integer
33
+ }
34
+ return hash;
35
+ }
36
+ var handler = {
37
+ get: function (obj, key) {
38
+ return obj[key];
39
+ },
40
+ set: function (obj, key, newVal) {
41
+ if (key === "__olum__") return false;
42
+ const oldVal = obj[key];
43
+ if (oldVal === newVal) return true;
44
+ obj[key] = newVal;
45
+ if (watcher && watcher[key] && typeof watcher[key] === "function") watcher[key](oldVal, newVal);
46
+ // todo prefix compName with instance e.g. you may have one component used twice so compName-1 & compName-2
47
+ const dataObj = { compName: obj.__olum__.compName, compId: obj.__olum__.compId };
48
+ const hash = mkHash(dataObj.compName + dataObj.compId);
49
+ dataObj.hash = hash;
50
+
51
+ window.olum.$emit("updateOlumComp", dataObj);
52
+ return true;
53
+ },
54
+ deleteProperty: function (obj, key) {
55
+ if (key === "__olum__") return false;
56
+ delete obj[key];
57
+ // todo prefix compName with instance e.g. you may have one component used twice so compName-1 & compName-2
58
+ const dataObj = { compName: obj.__olum__.compName, compId: obj.__olum__.compId };
59
+ const hash = mkHash(dataObj.compName + dataObj.compId);
60
+ dataObj.hash = hash;
61
+
62
+ window.olum.$emit("updateOlumComp", dataObj);
63
+ return true;
64
+ },
65
+ };
66
+ return new Proxy(obj, handler);
67
+ },
68
+ proxyHandlerForStore(obj, originalProxy) {
69
+ const handler = {
70
+ get: function (obj, key) {
71
+ return originalProxy[key];
72
+ },
73
+ set: function (obj, key, val) {
74
+ obj[key] = val;
75
+ originalProxy[key] = val;
76
+ return true;
77
+ },
78
+ deleteProperty: function (obj, key) {
79
+ delete obj[key];
80
+ delete originalProxy[key];
81
+ return true;
82
+ },
83
+ };
84
+ return new Proxy(obj, handler);
85
+ },
86
+ clean(fragment) {
87
+ const str = String(fragment).trim();
88
+ if (str === "null") return null;
89
+ return str;
90
+ },
91
+ // #2 (escape-by-default): HTML-escape a value before it is interpolated into a template.
92
+ // The compiler wraps every text/attribute interpolation `{expr}` in `olum.esc(expr)` so a
93
+ // user-supplied string (a todo title, a comment, anything) can't inject markup/scripts (XSS)
94
+ // or visually break rendering when it contains <, >, &, or quotes.
95
+ // - null/undefined render as "" (instead of the literal text "null"/"undefined").
96
+ // - To render trusted HTML on purpose, opt out explicitly with `olum.raw(html)` (below);
97
+ // esc() detects the marker it returns and passes the HTML through unescaped.
98
+ esc(value) {
99
+ if (value === null || value === undefined) return "";
100
+ if (value && value.__olumRaw === true) return value.html; // explicit raw-HTML opt-in
101
+ return String(value)
102
+ .replace(/&/g, "&amp;") // must run first so the entities below aren't double-escaped
103
+ .replace(/</g, "&lt;")
104
+ .replace(/>/g, "&gt;")
105
+ .replace(/"/g, "&quot;")
106
+ .replace(/'/g, "&#39;");
107
+ },
108
+ // #2 opt-in escape hatch: mark a string as trusted raw HTML so `esc()` leaves it untouched.
109
+ // Usage in a template: {olum.raw(props.richText)}. Use it ONLY on HTML you control or have
110
+ // already sanitized — this is the deliberate, greppable way to bypass auto-escaping.
111
+ raw(html) {
112
+ return { __olumRaw: true, html: html == null ? "" : String(html) };
113
+ },
114
+ eventsHandler(el, nodes, compName, methodsRefObj) {
115
+ function event(item, str, modifiers) {
116
+ const eventName = str.split("|")[0];
117
+ str = str.split("|").slice(1).join();
118
+
119
+ let methods = str.split("&");
120
+ const data = methods.map((chunk) => {
121
+ const chunks = chunk.split("=");
122
+ const name = chunks[0];
123
+ const args = JSON.parse(chunks.slice(1).join(""));
124
+ return { args, methodName: name };
125
+ });
126
+
127
+ const opts = {
128
+ once: false,
129
+ passive: false,
130
+ capture: false,
131
+ };
132
+
133
+ if (modifiers && modifiers.length) {
134
+ if (modifiers.includes("once")) opts.once = true;
135
+ if (modifiers.includes("passive")) opts.passive = true;
136
+ if (modifiers.includes("capture")) opts.capture = true;
137
+ }
138
+
139
+ item.addEventListener(
140
+ eventName.slice(2),
141
+ (e) => {
142
+ if (modifiers && modifiers.length) {
143
+ if (modifiers.includes("prevent")) e.preventDefault();
144
+ if (modifiers.includes("stop")) e.stopPropagation();
145
+ }
146
+
147
+ function init() {
148
+ data.forEach((obj) => {
149
+ if (methodsRefObj[obj.methodName]) {
150
+ if (obj.args.length) {
151
+ const eventIndex = obj.args.indexOf("$event");
152
+ if (eventIndex !== -1) obj.args.splice(eventIndex, 1, e);
153
+ methodsRefObj[obj.methodName](...obj.args);
154
+ } else {
155
+ methodsRefObj[obj.methodName](e);
156
+ }
157
+ } else {
158
+ console.warn("olum: can't access the method");
159
+ }
160
+ });
161
+ }
162
+
163
+ if (modifiers && modifiers.length && modifiers.includes("self")) {
164
+ if (e.target === item) init();
165
+ return;
166
+ }
167
+ init();
168
+ },
169
+ opts
170
+ );
171
+
172
+ item.removeAttribute("data-o-event");
173
+ item.removeAttribute("data-o-event-mode");
174
+ // item.removeAttribute("data-child-of");
175
+ }
176
+
177
+ nodes.forEach((node) => {
178
+ const hasEvent = olum.clean(node.getAttribute("data-o-event"));
179
+ const hasMode = olum.clean(node.getAttribute("data-o-event-mode"));
180
+ const mode = hasMode && hasMode.trim() !== "" ? hasMode.split(".") : [];
181
+ if (hasEvent) event(node, hasEvent, mode);
182
+ });
183
+ },
184
+ stylesHandler(el, nodes, compName) {
185
+ function setStyles(item, obj, keys) {
186
+ let str = "";
187
+ keys.forEach((key) => {
188
+ const val = obj[key];
189
+ if (val) {
190
+ // console.log({ key, val });
191
+ str += key + ": " + val + "; ";
192
+ }
193
+ });
194
+ str = str.trim();
195
+ // console.log(str);
196
+ if (str !== "") item.setAttribute("style", str);
197
+ item.removeAttribute("data-o-style");
198
+ // item.removeAttribute("data-child-of");
199
+ }
200
+
201
+ function style(item, str) {
202
+ const obj = JSON.parse(str);
203
+ const keys = Object.keys(obj);
204
+ if (keys.length) {
205
+ // console.log("directObj: ", obj);
206
+ setStyles(item, obj, keys);
207
+ }
208
+ }
209
+
210
+ nodes.forEach((node) => {
211
+ const hasStyle = olum.clean(node.getAttribute("data-o-style"));
212
+ if (hasStyle) style(node, hasStyle);
213
+ });
214
+ },
215
+ handleMarkup(compName, id, el, methods) {
216
+ el.setAttribute("data-child-of", compName);
217
+ el.setAttribute("data-o-" + id, "");
218
+ var nodes = el.querySelectorAll("*");
219
+ nodes.forEach((node) => {
220
+ node.setAttribute("data-child-of", compName);
221
+ node.setAttribute("data-o-" + id, "");
222
+ });
223
+ this.eventsHandler(el, nodes, compName, methods);
224
+ this.stylesHandler(el, nodes, compName);
225
+ return el;
226
+ },
227
+ isObj(obj) {
228
+ return obj !== null && typeof obj === "object";
229
+ },
230
+ isFullArr(arr) {
231
+ return !!(this.isObj(arr) && Array.isArray(arr) && arr.length);
232
+ },
233
+ isFullObj(obj) {
234
+ return !!(this.isObj(obj) && Array.isArray(Object.keys(obj)) && Object.keys(obj).length);
235
+ },
236
+ createStore(entry) {
237
+ const store = {};
238
+ const map = [];
239
+ store[entry.__OLUM__.compName] = entry; // bind root component
240
+
241
+ // bind children components
242
+ const recursive = (comp) => {
243
+ if (this.isFullArr(comp.__OLUM__.components)) {
244
+ comp.__OLUM__.components.forEach((item) => {
245
+ var key = Object.keys(item)[0];
246
+ var newComp = item[key]();
247
+ if (!store[key]) store[key] = newComp;
248
+
249
+ const obj = { name: key, children: newComp.__OLUM__.components.map((obj) => Object.keys(obj)[0]) };
250
+ map.push(obj);
251
+
252
+ if (this.isFullArr(obj.children)) recursive(newComp);
253
+ });
254
+ }
255
+ };
256
+ recursive(entry);
257
+
258
+ return { store, map };
259
+ },
260
+
261
+ // direct <olum> placeholders of a container: those with no <olum> ancestor inside the container.
262
+ // (deeper placeholders belong to a nested component and are handled when that component renders)
263
+ directOlums(container) {
264
+ return Array.prototype.slice.call(container.querySelectorAll("olum")).filter((p) => {
265
+ const anc = p.parentElement && p.parentElement.closest && p.parentElement.closest("olum");
266
+ return !anc || !container.contains(anc);
267
+ });
268
+ },
269
+ buildTree(comp, store, compKey) {
270
+ const rootElm = comp.__OLUM__.getElm;
271
+ if (!rootElm) return null;
272
+ const self = this;
273
+ // lazy global factory registry: name -> factory. Accumulated from every instance's components map
274
+ // as we descend, so slot-passed and cross-file components resolve regardless of where they were declared.
275
+ const registry = window.olum.app.registry || (window.olum.app.registry = {});
276
+
277
+ function renderChildren(containerComp, containerKey, containerElm) {
278
+ if (containerComp.__OLUM__.components) Object.assign(registry, containerComp.__OLUM__.components);
279
+ const placeholders = self.directOlums(containerElm);
280
+ const occ = {}; // per-name occurrence counter -> positional instance keys, stable across re-renders
281
+ placeholders.forEach((placeholder) => {
282
+ const name = placeholder.getAttribute("name");
283
+ const factory = registry[name] || (containerComp.__OLUM__.components && containerComp.__OLUM__.components[name]);
284
+ if (!factory) {
285
+ console.warn("olum: couldn't find " + name + " Component while building the tree!");
286
+ return;
287
+ }
288
+ // #3 (keyed reconciliation): a placeholder rendered inside a keyed <for> carries data-o-key
289
+ // (the evaluated `key={...}` value for that item). Keyed instances are stored under
290
+ // "...Name@<key>", so the SAME instance — and therefore its state and DOM — is reused for a
291
+ // given item no matter where it moves, is inserted, or is removed in the list. Without a key
292
+ // we fall back to positional "...Name#<i>" keys, where state follows position, not identity
293
+ // (delete the middle item of a list and the survivors inherit the wrong neighbour's state).
294
+ const keyVal = placeholder.getAttribute("data-o-key");
295
+ let instanceKey;
296
+ if (keyVal !== null && keyVal !== "") {
297
+ instanceKey = containerKey + ">" + name + "@" + keyVal;
298
+ } else {
299
+ occ[name] = occ[name] === undefined ? 0 : occ[name] + 1;
300
+ instanceKey = containerKey + ">" + name + "#" + occ[name];
301
+ }
302
+
303
+ // reuse the instance from a previous render (preserves its state) or mint a fresh one
304
+ let child = store[instanceKey];
305
+ if (!child) {
306
+ child = factory(instanceKey);
307
+ store[instanceKey] = child;
308
+ }
309
+
310
+ // handle props
311
+ child.parentCompName = containerKey;
312
+ const propsJson = placeholder.getAttribute("data-o-props");
313
+ child.incomingProps = propsJson ? JSON.parse(decodeURIComponent(propsJson)) : {};
314
+ const srcStr = placeholder.getAttribute("data-o-props-src") || "";
315
+ child.incomingPropSources = {};
316
+ if (srcStr) srcStr.split("|").forEach((pair) => {
317
+ const parts = pair.split(":"); // propKey:kind:srcKey
318
+ const propKey = parts[0], kind = parts[1], srcKey = parts[2];
319
+ if (propKey && kind && srcKey) child.incomingPropSources[propKey] = { kind, key: srcKey };
320
+ });
321
+
322
+ // handle slot/children — must be set before getElm so ${children} resolves in the child's template
323
+ child.children = placeholder.innerHTML.trim();
324
+
325
+ const elm = child.__OLUM__.getElm;
326
+ if (elm) {
327
+ elm.setAttribute("data-o-if", placeholder.getAttribute("if") ? placeholder.getAttribute("if") : "olum-no-condition"); // display if condition value (truthy, falsy)
328
+ placeholder.replaceWith(elm);
329
+ renderChildren(child, instanceKey, elm); // recurse into this child's own subtree
330
+ }
331
+ });
332
+ }
333
+
334
+ renderChildren(comp, compKey, rootElm);
335
+ return rootElm;
336
+ },
337
+ getInnerNames(entry) {
338
+ const comps = [];
339
+ const map = window.olum.app.map;
340
+ if (map) {
341
+ map.find((obj) => {
342
+ if (obj.name == entry) obj.children.forEach((child) => comps.push(child));
343
+ });
344
+ }
345
+
346
+ function recursive(num) {
347
+ const child = comps[num];
348
+ map.forEach((obj) => {
349
+ if (obj.name == child) obj.children.forEach((item) => comps.push(item));
350
+ });
351
+ if (num + 1 <= comps.length) recursive(num + 1);
352
+ }
353
+
354
+ if (comps.length) recursive(0);
355
+
356
+ // console.warn("innerComps: ", comps);
357
+ return comps;
358
+ },
359
+ };
360
+
361
+ class Olum {
362
+ root = null;
363
+ $(s) {
364
+ this.root = document.querySelector(s);
365
+ return this;
366
+ }
367
+
368
+ use(comp) {
369
+ const entry = comp();
370
+ const { store, rootKey } = this.share(entry);
371
+ const tree = window.olum.buildTree(entry, store, rootKey);
372
+ if (!tree) return console.warn("olum: couldn't build tree!");
373
+
374
+ this.setupListeners(store); // unmounted hook & state system
375
+
376
+ this.root.append(tree); // bind full comps tree
377
+ // handle mounted hook
378
+ if (entry.hooks.mounted) entry.hooks.mounted(); // force mounting parent component (entry point comp) regardless of the data-o-if value
379
+ entry.hooks.isMounted = true;
380
+ // mount every instance created during buildTree (keyed by runtime instance key)
381
+ Object.keys(store).forEach((key) => {
382
+ if (key === rootKey) return;
383
+ const c = store[key];
384
+ if (!c || !c.el) return;
385
+ const ifConValue = c.el.getAttribute("data-o-if");
386
+ if (!ifConValue) {
387
+ // don't render comp because it has falsy value
388
+ } else {
389
+ if (["olum-no-condition", "true"].includes(ifConValue)) {
390
+ if (c.hooks.mounted) c.hooks.mounted();
391
+ c.hooks.isMounted = true;
392
+ }
393
+ }
394
+ });
395
+ }
396
+
397
+ getPath(el, root) {
398
+ const path = [];
399
+ let cur = el;
400
+ while (cur && cur !== root) {
401
+ const parent = cur.parentElement;
402
+ if (!parent) break;
403
+ const siblings = Array.from(parent.children).filter(c => c.tagName === cur.tagName);
404
+ path.unshift({ tag: cur.tagName, index: siblings.indexOf(cur) });
405
+ cur = parent;
406
+ }
407
+ return path;
408
+ }
409
+
410
+ findByPath(root, path) {
411
+ let cur = root;
412
+ for (const step of path) {
413
+ const siblings = Array.from(cur.children).filter(c => c.tagName === step.tag);
414
+ cur = siblings[step.index];
415
+ if (!cur) return null;
416
+ }
417
+ return cur;
418
+ }
419
+
420
+ setupListeners(store) {
421
+ function mkHash(str) {
422
+ var hash = 0;
423
+ var i;
424
+ var char;
425
+ if (str.length === 0) return hash;
426
+ for (i = 0; i < str.length; i++) {
427
+ char = str.charCodeAt(i);
428
+ hash = (hash << 5) - hash + char;
429
+ hash |= 0;
430
+ }
431
+ return hash;
432
+ }
433
+
434
+ // const snapshots = {}; // persists across re-renders: { [compName]: Map<pathKey, entry> }
435
+
436
+ window.addEventListener("updateOlumComp", (e) => {
437
+ if (e && e.detail && e.detail.compName && e.detail.compId && e.detail.hash) {
438
+ if (e.detail.hash !== mkHash(e.detail.compName + e.detail.compId)) return;
439
+ const comp = store[e.detail.compName];
440
+ if (!comp || !comp.el) return;
441
+ if (!document.body.contains(comp.el)) return; // comp was unmounted; ignore stale state updates
442
+
443
+ const compName = e.detail.compName;
444
+
445
+ // snapshot input values of child components currently in DOM
446
+ // only child components (data-child-of !== compName) so we don't overwrite state inputs with stale values
447
+ // entries not updated here (hidden components) keep their previous snapshot value intact
448
+ // if (!snapshots[compName]) snapshots[compName] = new Map();
449
+ // comp.el.querySelectorAll("input, textarea, select").forEach(field => {
450
+ // const childOf = field.getAttribute("data-child-of");
451
+ // if (childOf && childOf !== compName) {
452
+ // const path = this.getPath(field, comp.el);
453
+ // const entry = { path };
454
+ // if (field.type === "checkbox" || field.type === "radio") entry.checked = field.checked;
455
+ // else entry.value = field.value;
456
+ // snapshots[compName].set(JSON.stringify(path), entry);
457
+ // }
458
+ // });
459
+
460
+ const innerNames = Object.keys(store).filter(name => name !== compName);
461
+ const prevMounted = {};
462
+ innerNames.forEach((name) => {
463
+ const c = store[name];
464
+ if (c) prevMounted[name] = c.hooks.isMounted;
465
+ });
466
+
467
+ // get current active element location/path before re-render (buildTree)
468
+ const activeEl = document.activeElement;
469
+ const selStart = activeEl && activeEl.selectionStart != null ? activeEl.selectionStart : null;
470
+ const selEnd = activeEl && activeEl.selectionEnd != null ? activeEl.selectionEnd : null;
471
+ const activePath = (activeEl && comp.el.contains(activeEl)) ? this.getPath(activeEl, comp.el) : null;
472
+
473
+ const treeElm = window.olum.buildTree(comp, store, compName);
474
+ if (!treeElm) return console.warn("olum: couldn't build tree!");
475
+ comp.el.replaceWith(treeElm);
476
+
477
+ // restore child component input values after rebuild
478
+ // snapshots[compName].forEach(({ path, value, checked }) => {
479
+ // const field = this.findByPath(treeElm, path);
480
+ // if (!field) return; // component is hidden in this render, skip
481
+ // if (checked !== undefined) field.checked = checked;
482
+ // else if (value !== undefined) field.value = value;
483
+ // });
484
+
485
+ // load prev active element after re-render (buildTree) to focus (e.g. input) or restore its data (e.g. forms)
486
+ if (activePath && activePath.length) {
487
+ const toRestore = this.findByPath(treeElm, activePath);
488
+ if (toRestore) {
489
+ if (toRestore.focus && typeof toRestore.focus == "function") toRestore.focus();
490
+ if (selStart !== null && toRestore.setSelectionRange && typeof toRestore.setSelectionRange == "function") toRestore.setSelectionRange(selStart, selEnd);
491
+ }
492
+ }
493
+
494
+ // after rebuild, call lifecycle hooks for any inner component that changed visibility.
495
+ // recompute keys here so instances created during this rebuild (e.g. a grown loop) are included.
496
+ const afterNames = Object.keys(store).filter(name => name !== compName);
497
+ afterNames.forEach((name) => {
498
+ const c = store[name];
499
+ if (!c) return;
500
+ const isInDOM = document.body.contains(c.el);
501
+ if (prevMounted[name] && !isInDOM) {
502
+ // was mounted, now removed from DOM → unMounted
503
+ if (c.hooks.unMounted && !c.hooks.isUnMounted) {
504
+ c.hooks.unMounted();
505
+ c.hooks.isUnMounted = true;
506
+ c.hooks.isMounted = false;
507
+ }
508
+ } else if (!prevMounted[name] && isInDOM) {
509
+ // was not mounted, now in DOM → mounted
510
+ if (c.hooks.mounted && !c.hooks.isMounted) {
511
+ c.hooks.mounted();
512
+ c.hooks.isMounted = true;
513
+ c.hooks.isUnMounted = false;
514
+ }
515
+ }
516
+ });
517
+ }
518
+ });
519
+ }
520
+
521
+ share(entry) {
522
+ // store now holds live instances keyed by runtime instance key; buildTree populates it lazily.
523
+ // registry holds name->factory and is accumulated as the tree is walked.
524
+ const store = {};
525
+ const rootKey = entry.__OLUM__.compName;
526
+ store[rootKey] = entry;
527
+ Object.assign(window.olum.app, { store, registry: {} }); // this is a must to access live changes
528
+ return { store, rootKey };
529
+ }
530
+ }
package/lib/attr.js ADDED
@@ -0,0 +1,39 @@
1
+ const { getLineInfo } = require("./lineCounter");
2
+ const path = require("path");
3
+
4
+ function handleAttr(body, counter, filePath) {
5
+ let name = path.basename(filePath).replace(/\.html/i, "");
6
+ name = name.slice(0, 1).toLowerCase() + name.slice(1); // make compName camel case
7
+
8
+ const elms = new Array().slice.call(body.querySelectorAll("*"));
9
+ elms.forEach((item) => {
10
+ const attrs = item.getAttributeNames();
11
+ if (attrs.length) {
12
+ // {identifier} shorthand: <img {src} /> → <img src="{src}" /> → src="${src}" in template -- inspired by svelete
13
+ attrs.filter(attr => /^\{\w+\}$/.test(attr)).forEach(attr => {
14
+ const attrName = attr.slice(1, -1);
15
+ item.removeAttribute(attr);
16
+ item.setAttribute(attrName, "{" + attrName + "}");
17
+ });
18
+
19
+ const dynamicAttrs = attrs.filter((attr) => attr.startsWith(":"));
20
+ if (dynamicAttrs.length) {
21
+ dynamicAttrs.forEach((attrName) => {
22
+ let attrValue = item.getAttribute(attrName);
23
+ if (attrValue) {
24
+ attrName = attrName.trim().slice(1);
25
+ attrValue = attrValue.trim();
26
+ let chain = "-[[-${JSON.parse(JSON.stringify(" + attrValue + "))}-]]-";
27
+ chain = chain.replace(/{/g, "&lt;-olum-&gt;"); // using <-olum-> (&lt;-olum-&gt;) as placeholder for "{"
28
+ // // console.log({chain});
29
+ // item.setAttribute("data-o-attr-" + attrName, chain);
30
+ item.setAttribute(attrName, chain);
31
+ item.removeAttribute(":"+attrName);
32
+ }
33
+ });
34
+ }
35
+ }
36
+ });
37
+ }
38
+
39
+ module.exports = handleAttr;
package/lib/clean.js ADDED
@@ -0,0 +1,4 @@
1
+ const { htmlComment } = require("./regex");
2
+ module.exports = function clean(str) {
3
+ return str.replace(htmlComment, "").trim();
4
+ };
package/lib/colors.js ADDED
@@ -0,0 +1,72 @@
1
+ // todo check how to convert colors.bgRed.white into colors("bgRed.white")
2
+ let map = {
3
+ reset: { start: "\x1B[0m", end: "\x1B[0m" },
4
+ bold: { start: "\x1B[1m", end: "\x1B[22m" },
5
+ dim: { start: "\x1B[2m", end: "\x1B[22m" },
6
+ italic: { start: "\x1B[3m", end: "\x1B[23m" },
7
+ underline: { start: "\x1B[4m", end: "\x1B[24m" },
8
+ inverse: { start: "\x1B[7m", end: "\x1B[27m" },
9
+ hidden: { start: "\x1B[8m", end: "\x1B[28m" },
10
+ strikethrough: { start: "\x1B[9m", end: "\x1B[29m" },
11
+ black: { start: "\x1B[30m", end: "\x1B[39m" },
12
+ red: { start: "\x1B[31m", end: "\x1B[39m" },
13
+ green: { start: "\x1B[32m", end: "\x1B[39m" },
14
+ yellow: { start: "\x1B[33m", end: "\x1B[39m" },
15
+ blue: { start: "\x1B[34m", end: "\x1B[39m" },
16
+ magenta: { start: "\x1B[35m", end: "\x1B[39m" },
17
+ cyan: { start: "\x1B[36m", end: "\x1B[39m" },
18
+ white: { start: "\x1B[37m", end: "\x1B[39m" },
19
+ gray: { start: "\x1B[90m", end: "\x1B[39m" },
20
+ grey: { start: "\x1B[90m", end: "\x1B[39m" },
21
+ brightRed: { start: "\x1B[91m", end: "\x1B[39m" },
22
+ brightGreen: { start: "\x1B[92m", end: "\x1B[39m" },
23
+ brightYellow: { start: "\x1B[93m", end: "\x1B[39m" },
24
+ brightBlue: { start: "\x1B[94m", end: "\x1B[39m" },
25
+ brightMagenta: { start: "\x1B[95m", end: "\x1B[39m" },
26
+ brightCyan: { start: "\x1B[96m", end: "\x1B[39m" },
27
+ brightWhite: { start: "\x1B[97m", end: "\x1B[39m" },
28
+ bgBlack: { start: "\x1B[40m", end: "\x1B[49m" },
29
+ bgRed: { start: "\x1B[41m", end: "\x1B[49m" },
30
+ bgGreen: { start: "\x1B[42m", end: "\x1B[49m" },
31
+ bgYellow: { start: "\x1B[43m", end: "\x1B[49m" },
32
+ bgBlue: { start: "\x1B[44m", end: "\x1B[49m" },
33
+ bgMagenta: { start: "\x1B[45m", end: "\x1B[49m" },
34
+ bgCyan: { start: "\x1B[46m", end: "\x1B[49m" },
35
+ bgWhite: { start: "\x1B[47m", end: "\x1B[49m" },
36
+ bgGray: { start: "\x1B[100m", end: "\x1B[49m" },
37
+ bgGrey: { start: "\x1B[100m", end: "\x1B[49m" },
38
+ bgBrightRed: { start: "\x1B[101m", end: "\x1B[49m" },
39
+ bgBrightGreen: { start: "\x1B[102m", end: "\x1B[49m" },
40
+ bgBrightYellow: { start: "\x1B[103m", end: "\x1B[49m" },
41
+ bgBrightBlue: { start: "\x1B[104m", end: "\x1B[49m" },
42
+ bgBrightMagenta: { start: "\x1B[105m", end: "\x1B[49m" },
43
+ bgBrightCyan: { start: "\x1B[106m", end: "\x1B[49m" },
44
+ bgBrightWhite: { start: "\x1B[107m", end: "\x1B[49m" },
45
+ blackBG: { start: "\x1B[40m", end: "\x1B[49m" },
46
+ redBG: { start: "\x1B[41m", end: "\x1B[49m" },
47
+ greenBG: { start: "\x1B[42m", end: "\x1B[49m" },
48
+ yellowBG: { start: "\x1B[43m", end: "\x1B[49m" },
49
+ blueBG: { start: "\x1B[44m", end: "\x1B[49m" },
50
+ magentaBG: { start: "\x1B[45m", end: "\x1B[49m" },
51
+ cyanBG: { start: "\x1B[46m", end: "\x1B[49m" },
52
+ whiteBG: { start: "\x1B[47m", end: "\x1B[49m" },
53
+ };
54
+
55
+ function colors(props, text) {
56
+ let arr = props.split(".");
57
+ if (arr.length) {
58
+ if (arr.length === 1) return map[arr[0]].start + text + map["reset"].start;
59
+ else if (arr.length > 1) {
60
+ let str = "";
61
+ arr.forEach((item) => (str += map[item].start));
62
+ return str + text + map["reset"].start;
63
+ }
64
+ }
65
+ }
66
+
67
+ // test cases:
68
+ // console.log(colors("cyan", "Serving"), colors("green", "localhost"));
69
+ // console.log(colors("red", "err"));
70
+ // console.log(colors("yellow.bold", "<<Olum Warning>>"));
71
+ // console.log(colors("yellow", "File"));
72
+ module.exports = colors;
@@ -0,0 +1,10 @@
1
+ const { getLineInfo } = require("./lineCounter");
2
+
3
+ function handleCompPlaceholder(body, counter, filePath) {
4
+ const comps = new Array().slice.call(body.querySelectorAll("olum"));
5
+ return comps.map(comp => {
6
+ if (comp.hasAttribute("name")) return comp.getAttribute("name");
7
+ }).filter(item => item);
8
+ }
9
+
10
+ module.exports = handleCompPlaceholder;
@@ -0,0 +1,21 @@
1
+ const fs = require("fs");
2
+ const getTempPath = require("./getTempPath");
3
+ const parser = require("./parser");
4
+ const { lineCounter } = require("./lineCounter");
5
+ const { compExt } = require("./regex");
6
+
7
+ // todo handle the component when it has these cases [null, only html, html + css, html+js, html+css+js, only css, only js]
8
+ const pathsArr = getTempPath();
9
+ pathsArr.forEach((filePath) => {
10
+ // console.warn("final: ", filePath);
11
+ if (fs.existsSync(filePath)) {
12
+ const fileContent = fs.readFileSync(filePath).toString();
13
+ const counter = lineCounter(fileContent);
14
+ let parsedData = parser(fileContent, counter, filePath);
15
+ const newFilePath = filePath.replace(compExt, ".js");
16
+ // create .js module
17
+ fs.writeFileSync(newFilePath, parsedData);
18
+ // remove .html template
19
+ fs.unlinkSync(filePath);
20
+ }
21
+ });