apprun 3.36.0 → 3.37.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 (93) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/README.md +143 -25
  3. package/WHATSNEW.md +29 -12
  4. package/apprun-book.jpg +0 -0
  5. package/apprun.d.ts +136 -46
  6. package/cli/app.js +29 -0
  7. package/cli/index.html +13 -0
  8. package/{apprun-cli.js → cli/index.js} +8 -14
  9. package/dist/apprun-code.js +2 -0
  10. package/dist/apprun-code.js.map +1 -0
  11. package/dist/apprun-dev-tools.js +1 -2
  12. package/dist/apprun-dev-tools.js.map +1 -1
  13. package/dist/apprun-html.esm.js +7 -7
  14. package/dist/apprun-html.esm.js.map +1 -1
  15. package/dist/apprun-html.js +1 -1
  16. package/dist/apprun-html.js.map +1 -1
  17. package/dist/apprun-play-html.esm.js +7 -7
  18. package/dist/apprun-play-html.esm.js.map +1 -1
  19. package/dist/apprun-play.js +1 -1
  20. package/dist/apprun-play.js.map +1 -1
  21. package/dist/apprun.esm.js +1 -1
  22. package/dist/apprun.esm.js.map +1 -1
  23. package/dist/apprun.js +1 -1
  24. package/dist/apprun.js.map +1 -1
  25. package/dist/createState.js +2 -0
  26. package/dist/createState.js.map +1 -0
  27. package/esm/add-components.js +90 -0
  28. package/esm/add-components.js.map +1 -0
  29. package/esm/app.js +15 -8
  30. package/esm/app.js.map +1 -1
  31. package/esm/apprun-code.js +51 -16
  32. package/esm/apprun-code.js.map +1 -1
  33. package/esm/apprun-dev-tools.js +32 -32
  34. package/esm/apprun-dev-tools.js.map +1 -1
  35. package/esm/apprun-play.js +50 -17
  36. package/esm/apprun-play.js.map +1 -1
  37. package/esm/apprun.js +42 -11
  38. package/esm/apprun.js.map +1 -1
  39. package/esm/component.js +17 -20
  40. package/esm/component.js.map +1 -1
  41. package/esm/createState.js +9 -0
  42. package/esm/createState.js.map +1 -0
  43. package/esm/decorator.js.map +1 -1
  44. package/esm/directive.js +1 -1
  45. package/esm/directive.js.map +1 -1
  46. package/esm/router.js +241 -18
  47. package/esm/router.js.map +1 -1
  48. package/esm/shadow.js +1 -1
  49. package/esm/shadow.js.map +1 -1
  50. package/esm/type-utils.js +2 -3
  51. package/esm/type-utils.js.map +1 -1
  52. package/esm/vdom-my-new.js +327 -0
  53. package/esm/vdom-my-new.js.map +1 -0
  54. package/esm/vdom-my-prop-attr.js +227 -0
  55. package/esm/vdom-my-prop-attr.js.map +1 -0
  56. package/esm/vdom-my.js +114 -150
  57. package/esm/vdom-my.js.map +1 -1
  58. package/esm/vdom-patch.js +1 -1
  59. package/esm/vdom-patch.js.map +1 -1
  60. package/esm/version.js +1 -1
  61. package/esm/web-component.js +3 -4
  62. package/esm/web-component.js.map +1 -1
  63. package/index.html +5 -2
  64. package/jest.config.js +2 -2
  65. package/jest.setup.js +29 -3
  66. package/jsx-runtime.js +1 -1
  67. package/jsx-runtime.js.map +1 -1
  68. package/package.json +14 -14
  69. package/src/add-components.ts +103 -0
  70. package/src/app.ts +10 -19
  71. package/src/apprun-code.tsx +48 -10
  72. package/src/apprun-dev-tools.tsx +23 -27
  73. package/src/apprun-play.tsx +46 -9
  74. package/src/apprun.ts +49 -40
  75. package/src/component.ts +15 -8
  76. package/src/createState.ts +11 -0
  77. package/src/decorator.ts +2 -1
  78. package/src/router.ts +261 -17
  79. package/src/shadow.tsx +1 -1
  80. package/src/tsconfig.json +2 -2
  81. package/src/types.ts +62 -2
  82. package/src/vdom-my-new.ts +311 -0
  83. package/src/vdom-my-prop-attr.ts +241 -0
  84. package/src/vdom-my.ts +109 -134
  85. package/src/version.ts +1 -1
  86. package/src/web-component.ts +4 -10
  87. package/tsconfig.jest.json +15 -6
  88. package/tsconfig.json +3 -3
  89. package/webpack.config.cjs +3 -1
  90. package/cli/export.js +0 -92
  91. package/cli/import.js +0 -68
  92. package/plan/plan-apprun-bugfixes.md +0 -207
  93. package/src/types/apprun.d.ts +0 -56
package/src/vdom-my.ts CHANGED
@@ -1,13 +1,56 @@
1
+ /** * VDOM Implementation for AppRun
2
+ *
3
+ * Notes for AppRun’s key prop
4
+ * Use the key prop only if you need to preserve browser-side DOM state (such as cursor
5
+ * position in an <input>, focus, or maintaining state in inline components).
6
+ *
7
+ * For most use cases, especially with stateless or purely data-driven UIs, key is not
8
+ * needed and may decrease performance by forcing DOM moves.
9
+ *
10
+ * AppRun aggressively updates DOM to match your vdom, so DOM node preservation is
11
+ * only valuable when you intentionally depend on browser-managed state.
12
+ *
13
+ * | Use Case | Should I use `key`? | Why |
14
+ * | ----------------------------- | ------------------- | --------------------- |
15
+ * | Preserve input cursor/focus | ✅ Yes | Keeps browser state |
16
+ * | Inline stateful component | ✅ Yes | Preserves component |
17
+ * | Regular data list (stateless) | 🚫 No | Unnecessary DOM moves |
18
+ * | Purely visual updates | 🚫 No | No state to preserve |
19
+ *
20
+ * Features:
21
+ * - Virtual DOM rendering and diffing for efficient DOM updates
22
+ * - JSX Fragment support for both Babel and TypeScript
23
+ * - Element creation with props, children, and event handling
24
+ * - SVG element support with proper namespace handling
25
+ * - Component lifecycle management and caching
26
+ * - Keyed element optimization with automatic cleanup for memory management
27
+ * - Safe HTML insertion and text node creation
28
+ * - Directive processing integration
29
+ *
30
+ * Implementation:
31
+ * - Uses plain JavaScript object for keyCache instead of Map for better performance
32
+ * - Implements automatic cleanup of disconnected elements from keyCache
33
+ * - Supports both string and function-based tags
34
+ * - Handles component mounting and state management
35
+ * - Optimized children updating with minimal DOM operations
36
+ * - Memory-efficient caching with configurable thresholds (500 ops, 1000 max size)
37
+ *
38
+ * Recent Changes:
39
+ * - 2025-07-15: Converted keyCache from Map to plain object ({}) for improved performance
40
+ * - Updated cleanup functions to use object property deletion instead of Map methods
41
+ * - Enhanced memory management with automatic cleanup of disconnected elements
42
+ * - Added comprehensive key prop usage documentation and guidelines
43
+ */
44
+
1
45
  import { VDOM, VNode } from './types';
2
46
  import directive from './directive';
47
+ import { updateProps } from './vdom-my-prop-attr';
3
48
  export type Element = any; //HTMLElement | SVGSVGElement | SVGElement;
4
49
 
5
50
  export function Fragment(props, ...children): any[] {
6
51
  return collect(children);
7
52
  }
8
53
 
9
- const ATTR_PROPS = '_props';
10
-
11
54
  function collect(children) {
12
55
  const ch = [];
13
56
  const push = (c) => {
@@ -25,6 +68,30 @@ function collect(children) {
25
68
  return ch;
26
69
  }
27
70
 
71
+ const keyCache: { [key: string]: Element } = {};
72
+ let cleanupCounter = 0;
73
+ const CLEANUP_THRESHOLD = 500; // Cleanup every 500 operations
74
+ const MAX_CACHE_SIZE = 1000;
75
+
76
+ // Lightweight cleanup function - only runs when needed
77
+ function cleanupKeyCache() {
78
+ if (Object.keys(keyCache).length <= MAX_CACHE_SIZE) return; // Skip if under limit
79
+
80
+ for (const [key, element] of Object.entries(keyCache)) {
81
+ if (!element.isConnected) {
82
+ delete keyCache[key];
83
+ }
84
+ }
85
+ }
86
+
87
+ // Export cleanup function for manual cleanup if needed
88
+ export function clearKeyCache() {
89
+ for (const key in keyCache) {
90
+ delete keyCache[key];
91
+ }
92
+ cleanupCounter = 0;
93
+ }
94
+
28
95
  export function createElement(tag: string | Function | [], props?: {}, ...children) {
29
96
  const ch = collect(children);
30
97
  if (typeof tag === 'string') return { tag, props, children: ch };
@@ -35,8 +102,6 @@ export function createElement(tag: string | Function | [], props?: {}, ...childr
35
102
  else throw new Error(`Unknown tag in vdom ${tag}`);
36
103
  };
37
104
 
38
- const keyCache = {};
39
-
40
105
  export const updateElement = (element: Element | string, nodes: VDOM, component = {}) => {
41
106
  // tslint:disable-next-line
42
107
  if (nodes == null || nodes === false) return;
@@ -77,86 +142,56 @@ function update(element: Element, node: VNode, isSvg: boolean) {
77
142
  updateProps(element, node.props, isSvg);
78
143
  }
79
144
 
80
- function updateChildren(element: Element, children: any[], isSvg: boolean) {
145
+ function updateChildren(element, children, isSvg: boolean) {
81
146
  const old_len = element.childNodes?.length || 0;
82
147
  const new_len = children?.length || 0;
83
-
84
- // Handle key-based reordering first if any children have keys
85
- const hasKeysInNewChildren = children?.some(child =>
86
- child && typeof child === 'object' && child.props && child.props.key !== undefined
87
- );
88
-
89
- if (hasKeysInNewChildren) {
90
- // Create a map of existing keyed elements
91
- const existingKeyedElements = new Map();
92
- for (let i = 0; i < old_len; i++) {
93
- const el = element.childNodes[i];
94
- if (el && el.key) {
95
- existingKeyedElements.set(el.key, el);
96
- }
97
- }
98
-
99
- // Build new DOM structure
100
- const fragment = document.createDocumentFragment();
101
- for (let i = 0; i < new_len; i++) {
102
- const child = children[i];
103
- if (child == null) continue;
104
-
105
- const key = child.props && child.props['key'];
106
- if (key && existingKeyedElements.has(key)) {
107
- // Reuse existing element
108
- const existingEl = existingKeyedElements.get(key);
109
- update(existingEl, child as VNode, isSvg);
110
- fragment.appendChild(existingEl);
111
- existingKeyedElements.delete(key); // Mark as used
112
- } else {
113
- // Create new element
114
- fragment.appendChild(create(child, isSvg));
115
- }
116
- }
117
-
118
- // Clear current children and append new structure
119
- while (element.firstChild) {
120
- element.removeChild(element.firstChild);
121
- }
122
- element.appendChild(fragment);
123
- return;
124
- }
125
-
126
- // Original non-keyed logic
127
148
  const len = Math.min(old_len, new_len);
128
149
  for (let i = 0; i < len; i++) {
129
150
  const child = children[i];
130
- if (child == null) continue;
131
151
  const el = element.childNodes[i];
132
- if (!el) continue; // Safety check for undefined childNodes
133
152
  if (typeof child === 'string') {
134
- if (el.nodeType === 3) {
135
- if (el.nodeValue !== child) {
136
- el.nodeValue = child;
153
+ if (el.textContent !== child) {
154
+ if (el.nodeType === 3) {
155
+ el.nodeValue = child
156
+ } else {
157
+ element.replaceChild(createText(child), el);
137
158
  }
138
- } else {
139
- element.replaceChild(createText(child), el);
140
159
  }
141
160
  } else if (child instanceof HTMLElement || child instanceof SVGElement) {
142
- element.replaceChild(child, el);
143
- } else if (child && typeof child === 'object') {
144
- update(element.childNodes[i], child as VNode, isSvg);
161
+ element.insertBefore(child, el);
162
+ } else {
163
+ const key = child.props && child.props['key'];
164
+ if (key) {
165
+ if (el.key === key) {
166
+ update(element.childNodes[i], child, isSvg);
167
+ } else {
168
+ // console.log(el.key, key);
169
+ const old = keyCache[key];
170
+ if (old) {
171
+ // const temp = old.nextSibling;
172
+ element.insertBefore(old, el);
173
+ // temp ? element.insertBefore(el, temp) : element.appendChild(el);
174
+ update(element.childNodes[i], child, isSvg);
175
+ } else {
176
+ element.replaceChild(create(child, isSvg), el);
177
+ }
178
+ }
179
+ } else {
180
+ update(element.childNodes[i], child, isSvg);
181
+ }
145
182
  }
146
183
  }
147
184
 
148
- // Remove extra old nodes
149
- while (element.childNodes.length > len) {
185
+ let n = element.childNodes?.length || 0;
186
+ while (n > len) {
150
187
  element.removeChild(element.lastChild);
188
+ n--;
151
189
  }
152
190
 
153
191
  if (new_len > len) {
154
192
  const d = document.createDocumentFragment();
155
193
  for (let i = len; i < children.length; i++) {
156
- const child = children[i];
157
- if (child != null) {
158
- d.appendChild(create(child, isSvg));
159
- }
194
+ d.appendChild(create(children[i], isSvg));
160
195
  }
161
196
  element.appendChild(d);
162
197
  }
@@ -182,12 +217,7 @@ function create(node: VNode | string | HTMLElement | SVGElement, isSvg: boolean)
182
217
  // console.assert(node !== null && node !== undefined);
183
218
  if ((node instanceof HTMLElement) || (node instanceof SVGElement)) return node;
184
219
  if (typeof node === "string") return createText(node);
185
-
186
- // Type guard for VNode objects - handle invalid node types gracefully
187
- if (!node || typeof node !== 'object' || !node.tag || (typeof node.tag === 'function')) {
188
- return createText(typeof node === 'object' ? JSON.stringify(node) : String(node ?? ''));
189
- }
190
-
220
+ if (!node.tag || (typeof node.tag === 'function')) return createText(JSON.stringify(node));
191
221
  isSvg = isSvg || node.tag === "svg";
192
222
  const element = isSvg
193
223
  ? document.createElementNS("http://www.w3.org/2000/svg", node.tag)
@@ -195,73 +225,18 @@ function create(node: VNode | string | HTMLElement | SVGElement, isSvg: boolean)
195
225
 
196
226
  updateProps(element, node.props, isSvg);
197
227
  if (node.children) node.children.forEach(child => element.appendChild(create(child, isSvg)));
198
- return element
199
- }
200
228
 
201
- function mergeProps(oldProps: {}, newProps: {}): {} {
202
- newProps['class'] = newProps['class'] || newProps['className'];
203
- delete newProps['className'];
204
- const props = {};
205
- if (oldProps) Object.keys(oldProps).forEach(p => props[p] = null);
206
- if (newProps) Object.keys(newProps).forEach(p => props[p] = newProps[p]);
207
- return props;
208
- }
209
-
210
- export function updateProps(element: Element, props: {}, isSvg) {
211
- // console.assert(!!element);
212
- const cached = element[ATTR_PROPS] || {};
213
- props = mergeProps(cached, props || {});
214
- element[ATTR_PROPS] = props;
229
+ if (node.props && (node.props as any).key !== undefined) {
230
+ (element as any).key = (node.props as any).key;
231
+ keyCache[(node.props as any).key] = element;
215
232
 
216
- for (const name in props) {
217
- const value = props[name];
218
- // if (cached[name] === value) continue;
219
- // console.log('updateProps', name, value);
220
- if (name.startsWith('data-')) {
221
- const dname = name.substring(5);
222
- const cname = dname.replace(/-(\w)/g, (match) => match[1].toUpperCase());
223
- if (element.dataset[cname] !== value) {
224
- if (value || value === "") element.dataset[cname] = value;
225
- else delete element.dataset[cname];
226
- }
227
- } else if (name === 'style') {
228
- if (element.style.cssText) element.style.cssText = '';
229
- if (typeof value === 'string') element.style.cssText = value;
230
- else {
231
- for (const s in value) {
232
- if (element.style[s] !== value[s]) element.style[s] = value[s];
233
- }
234
- }
235
- } else if (name.startsWith('xlink')) {
236
- const xname = name.replace('xlink', '').toLowerCase();
237
- if (value == null || value === false) {
238
- element.removeAttributeNS('http://www.w3.org/1999/xlink', xname);
239
- } else {
240
- element.setAttributeNS('http://www.w3.org/1999/xlink', xname, value);
241
- }
242
- } else if (name.startsWith('on')) {
243
- if (!value || typeof value === 'function') {
244
- element[name] = value;
245
- } else if (typeof value === 'string') {
246
- if (value) element.setAttribute(name, value);
247
- else element.removeAttribute(name);
248
- }
249
- } else if (/^id$|^class$|^list$|^readonly$|^contenteditable$|^role|-|^for$/g.test(name) || isSvg) {
250
- if (element.getAttribute(name) !== value) {
251
- if (value) element.setAttribute(name, value);
252
- else element.removeAttribute(name);
253
- }
254
- } else if (element[name] !== value) {
255
- element[name] = value;
233
+ // Lightweight cleanup - only when counter reaches threshold
234
+ if (++cleanupCounter >= CLEANUP_THRESHOLD) {
235
+ cleanupKeyCache();
236
+ cleanupCounter = 0;
256
237
  }
257
- if (name === 'key' && value !== undefined) {
258
- keyCache[value] = element;
259
- element.key = value; // Set key property on the DOM element
260
- }
261
- }
262
- if (props && typeof props['ref'] === 'function') {
263
- window.requestAnimationFrame(() => props['ref'](element));
264
238
  }
239
+ return element
265
240
  }
266
241
 
267
242
  function render_component(node, parent, idx) {
package/src/version.ts CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
  // Import version from package.json to maintain single source of truth
12
12
  // This version string is used across the framework
13
- export const APPRUN_VERSION = '3.36.0';
13
+ export const APPRUN_VERSION = '3.37.0';
14
14
 
15
15
  // Version string with prefix for global tracking
16
16
  export const APPRUN_VERSION_GLOBAL = `AppRun-${APPRUN_VERSION}`;
@@ -53,18 +53,12 @@
53
53
 
54
54
  declare var customElements;
55
55
 
56
- export type CustomElementOptions = {
57
- render?: boolean;
58
- shadow?: boolean;
59
- history?: boolean;
60
- global_event?: boolean;
61
- observedAttributes?: string[];
62
- };
56
+ import { CustomElementOptions } from './types';
63
57
 
64
58
  export const customElement = (componentClass, options: CustomElementOptions = {}) => class CustomElement extends HTMLElement {
65
- private _shadowRoot;
66
- private _component;
67
- private _attrMap: (arg0: string) => string;
59
+ public _shadowRoot;
60
+ public _component;
61
+ public _attrMap: (arg0: string) => string;
68
62
  public on;
69
63
  public run;
70
64
  constructor() {
@@ -1,17 +1,21 @@
1
1
  {
2
2
  "compilerOptions": {
3
- "target": "es2015",
4
- "module": "es2015",
3
+ "target": "es2020",
4
+ "module": "esnext",
5
5
  "moduleResolution": "node",
6
6
  "jsx": "react",
7
7
  "reactNamespace": "app",
8
- "lib": ["dom", "esnext"],
8
+ "lib": [
9
+ "dom",
10
+ "esnext"
11
+ ],
9
12
  "experimentalDecorators": true,
10
13
  "sourceMap": true,
11
14
  "esModuleInterop": true,
12
15
  "downlevelIteration": true,
13
16
  "allowJs": true,
14
17
  "outDir": "coverage/src",
18
+ "isolatedModules": true,
15
19
  "strict": true,
16
20
  "noImplicitAny": true,
17
21
  "strictNullChecks": true,
@@ -24,17 +28,22 @@
24
28
  "forceConsistentCasingInFileNames": true,
25
29
  "baseUrl": ".",
26
30
  "paths": {
27
- "*": ["src/types/*"]
31
+ "*": [
32
+ "src/types/*"
33
+ ]
28
34
  },
29
35
  "typeRoots": [
30
36
  "./node_modules/@types",
31
37
  "./src/types"
32
38
  ],
33
- "types": ["jest", "node"]
39
+ "types": [
40
+ "jest",
41
+ "node"
42
+ ]
34
43
  },
35
44
  "include": [
36
45
  "src/**/*",
37
46
  "tests/**/*",
38
47
  "demo/**/*"
39
48
  ]
40
- }
49
+ }
package/tsconfig.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "compilerOptions": {
3
- "target": "es2018",
4
- "module": "es2015",
3
+ "target": "es2020",
4
+ "module": "esnext",
5
5
  "moduleResolution": "node",
6
6
  "jsx": "react",
7
7
  "jsxFactory": "app.h",
8
8
  "jsxFragmentFactory": "app.Fragment",
9
- "lib": ["dom", "es2018", "esnext.asynciterable"],
9
+ "lib": ["dom", "es2020", "esnext.asynciterable"],
10
10
  "experimentalDecorators": true,
11
11
  "sourceMap": true,
12
12
  "esModuleInterop": true,
@@ -5,9 +5,11 @@ module.exports = {
5
5
  entry: {
6
6
  'dist/apprun': './src/apprun.ts',
7
7
  'dist/apprun-play': './src/apprun-play.tsx',
8
+ 'dist/apprun-code': './src/apprun-code.tsx',
8
9
  'dist/apprun-html': './src/apprun-html.ts',
9
- './jsx-runtime': './src/vdom.ts',
10
10
  'dist/apprun-dev-tools': './src/apprun-dev-tools.tsx',
11
+ 'dist/createState': './src/createState.ts',
12
+ 'jsx-runtime': './src/vdom.ts',
11
13
  'demo/app': './demo/main.ts'
12
14
  },
13
15
  output: {
package/cli/export.js DELETED
@@ -1,92 +0,0 @@
1
- import { exec } from 'child_process';
2
- import yaml from 'js-yaml';
3
- import { unlinkSync } from 'fs';
4
-
5
- function replacer(key, value) {
6
- if (typeof value === 'function') return value.toString();
7
- return ['', null].includes(value) || (typeof value === 'object' && (value.length === 0 || Object.keys(value).length === 0)) ? undefined : value;
8
- }
9
-
10
- function createProxy(obj) {
11
- const handler = {
12
- get(target, property, receiver) {
13
-
14
- // Get the property value
15
- const value = Reflect.get(target, property, receiver);
16
-
17
- // If the value is an object (including arrays), proxy it
18
- if (typeof value === 'object' && value !== null) {
19
- if (Array.isArray(value)) {
20
- // Proxy each element of the array if it's an object
21
- return value.map(item => createProxy(item));
22
- } else {
23
- // Recursively proxy the object
24
- return createProxy(value);
25
- }
26
- }
27
-
28
- return `{${property}}`
29
- },
30
-
31
- };
32
-
33
- return Array.isArray(obj) ?
34
- obj.map(item => createProxy(item)) : new Proxy(obj, handler);
35
- }
36
-
37
- function getVDOM(component) {
38
- let view;
39
- if (typeof component.state === 'object') {
40
- const proxy = createProxy(component.state);
41
- view = component.view(proxy);
42
- } else {
43
- view = component.view(component.state);
44
- }
45
- return view;
46
- }
47
-
48
- export default function export_yaml(fn) {
49
-
50
- const tmp = process.cwd() + '/_tmp.js';
51
- exec(`npx esbuild ${fn} --outfile=${tmp} --format=esm --bundle`, (error, stdout, stderr) => {
52
- if (error) {
53
- console.error(`exec error: ${error}`);
54
- return;
55
- }
56
-
57
- import(tmp).then((m) => {
58
-
59
- const defaul_export = m.default
60
- let component;
61
- if (typeof defaul_export === 'function' &&
62
- /^class\s/.test(Function.prototype.toString.call(defaul_export))) {
63
- component = new m.default();
64
- component = component.mount();
65
- } else if (typeof defaul_export === 'function') {
66
- component = defaul_export();
67
- } else {
68
- component = defaul_export;
69
- }
70
-
71
- const vdom = getVDOM(component);
72
- const events = component['_actions'].map(a => a.name);
73
-
74
- const component_def = {
75
- name: component.constructor.name,
76
- file: fn,
77
- state: component.state,
78
- view: vdom,
79
- actions: events,
80
- update: component.update,
81
- };
82
-
83
- const yaml_def = yaml.dump(component_def, { replacer });
84
- console.log(yaml_def);
85
-
86
- unlinkSync(tmp);
87
- });
88
- });
89
- }
90
-
91
-
92
-
package/cli/import.js DELETED
@@ -1,68 +0,0 @@
1
- import ymal from 'js-yaml';
2
- import { readFileSync } from 'fs';
3
-
4
- function getProp(prop) {
5
- if (typeof prop === 'object') {
6
- return Object.keys(prop).map(name => `${name}:${prop[name]}`).join(';');
7
- }
8
- else return prop.toString();
9
- }
10
-
11
- function toProps(props) {
12
- return Object.keys(props)
13
- .map(name => ` ${name === 'className' ? 'class' : name}="${getProp(props[name])}"`)
14
- .join('');
15
- }
16
-
17
- function toHTMLArray(nodes) {
18
- return nodes.map(node => toHTML(node)).join('');
19
- }
20
-
21
- function toHTML(vdom) {
22
- if (!vdom) return '';
23
- if (Array.isArray(vdom)) return toHTMLArray(vdom)
24
- if (vdom.tag) {
25
- const props = vdom.props ? toProps(vdom.props) : '';
26
- const children = vdom.children ? toHTMLArray(vdom.children) : '';
27
- return `<${vdom.tag}${props}>${children}</${vdom.tag}>`;
28
- }
29
- if (typeof vdom === 'object') return JSON.stringify(vdom);
30
- else {
31
- const html = vdom.toString();
32
- return html.startsWith('_html:') ? html.substring(6) : html;
33
- }
34
- }
35
-
36
- export default function import_yaml(fn) {
37
-
38
- const def = ymal.load(readFileSync(fn, 'utf8'))
39
-
40
- const { name, state, view, update } = def;
41
-
42
- const jsx = toHTML(view);
43
-
44
- // const update_text = JSON.stringify(update, null, 2)
45
- // .replace(/\\n/g, '\n');
46
-
47
- const update_text =
48
- Object.keys(update).map(key => `
49
- "${key}" : ${update[key].replace(/\\n/g, '\n')}`).join(',\n'
50
- );
51
-
52
- const component = `
53
- import { app, Component } from 'apprun';
54
-
55
- class ${name} extends Component {
56
-
57
- state = ${JSON.stringify(state)};
58
-
59
- view = ${jsx};
60
-
61
- update = {
62
- ${update_text}
63
- };
64
- };
65
- `;
66
-
67
- console.log(component);
68
- }