chocola 1.3.2 → 1.3.4

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.
Binary file
Binary file
@@ -31,22 +31,24 @@ export function processComponentElement(
31
31
  if (instance && instance.body) {
32
32
  let body = instance.body;
33
33
  body = body.replace(
34
- /(?<!\b(?:if|del-if)=)\{ctx\.(\w+)\}/g,
34
+ /(?<!\b(?:if|del-if)=)\{(\w+)\}/g,
35
35
  (_, key) => ctx[key] || ""
36
36
  );
37
37
  const fragment = JSDOM.fragment(body);
38
38
  const children = Array.from(fragment.querySelectorAll("*"));
39
39
  children.forEach(child => {
40
- if (child.hasAttribute("if")) {
41
- const expr = child.getAttribute("if").slice(1, -1);
42
- const fn = new Function("ctx", `if (${expr} === true) {return true} else {return ${expr} === '{true}'}`);
43
- if (!fn(ctx)) child.style.display = "none";
44
- }
45
- if (child.hasAttribute("del-if")) {
46
- const expr = child.getAttribute("del-if").slice(1, -1);
47
- const fn = new Function("ctx", `if (${expr} === true) {return true} else {return ${expr} === '{true}'}`);
48
- if (!fn(ctx)) child.remove();
49
- }
40
+ ["if", "del-if"].forEach(statement => {
41
+ const statAtt = child.getAttribute(statement);
42
+ if (!statAtt) return;
43
+ const expr = statAtt.slice(1, -1);
44
+ const fn = new Function("ctx", `return ctx.${expr} === true || ctx.${expr} === '{true}'`);
45
+ if (!fn(ctx)) {
46
+ if (statement === "if") child.style.display = "none";
47
+ if (statement === "del-if") child.remove();
48
+ }
49
+
50
+ child.removeAttribute(statement);
51
+ });
50
52
  });
51
53
  const firstChild = fragment.firstChild;
52
54
 
@@ -74,7 +76,6 @@ export function processComponentElement(
74
76
  script = script.replace(ctxRegex, "ctx");
75
77
  script = script.replace(/RUNTIME\([^)]*\)\s*{/, match => match + "\n" + ctxDef);
76
78
 
77
- // Determine or create a single runtime function per component
78
79
  let letterEntry = runtimeMap && runtimeMap.get(compName);
79
80
  let letter;
80
81
  if (!letterEntry) {
@@ -43,10 +43,8 @@ export function getAppElements(appContainer) {
43
43
  export function extractContextFromElement(element) {
44
44
  const ctx = {};
45
45
  for (const attr of element.attributes) {
46
- if (attr.name.startsWith("ctx.")) {
47
- const key = attr.name.slice(4);
48
- ctx[key] = attr.value;
49
- }
46
+ const key = attr.name;
47
+ ctx[key] = attr.value;
50
48
  }
51
49
  return ctx;
52
50
  }
@@ -4,8 +4,6 @@ import { readMyFile, checkFile } from "./fs.js";
4
4
  import { JSDOM } from "jsdom";
5
5
  import path from "path";
6
6
 
7
- // ===== Component Loading =====
8
-
9
7
  /**
10
8
  * Discovers and loads all components from a library directory.
11
9
  * Components are JavaScript files that start with an uppercase letter.
@@ -31,7 +29,6 @@ export async function getComponents(libDir) {
31
29
  }
32
30
 
33
31
  for (const comp of components) {
34
- // Only load .js files that start with uppercase (component convention)
35
32
  if (!comp.endsWith(".js") || comp[0] !== comp[0].toUpperCase()) continue;
36
33
 
37
34
  componentsLib.push(comp);
@@ -45,7 +42,6 @@ export async function getComponents(libDir) {
45
42
 
46
43
  const instance = module.default();
47
44
 
48
- // Load external body template if specified
49
45
  if (instance.bodyPath) {
50
46
  instance.body = await fs.readFile(
51
47
  path.resolve(libDir, instance.bodyPath),
@@ -62,8 +58,6 @@ export async function getComponents(libDir) {
62
58
  }
63
59
  }
64
60
 
65
- // ===== Index File Loading =====
66
-
67
61
  /**
68
62
  * Loads the project index file (HTML or .choco)
69
63
  * If both HTML and .choco files exist, throws an error
@@ -104,8 +98,6 @@ export async function getSrcIndex(srcPath) {
104
98
  }
105
99
  }
106
100
 
107
- // ===== Asset Processing =====
108
-
109
101
  /**
110
102
  * Processes stylesheet links: copies CSS files to output and updates link href
111
103
  * @param {HTMLLinkElement} link - The link element to process
@@ -162,12 +154,10 @@ export async function copyResources(rootDir, srcDir, outDirPath) {
162
154
  const newElements = Array.from(newDoc.window.document.querySelectorAll("*"));
163
155
 
164
156
  for (const el of newElements) {
165
- // Skip LINK and SCRIPT tags as they're already processed
166
157
  if (el.tagName === "LINK" || el.tagName === "SCRIPT") continue;
167
158
 
168
159
  const src = el.getAttribute("src") || el.getAttribute("href");
169
160
 
170
- // Only copy local resources, not external web links
171
161
  if (src && !isWebLink(src)) {
172
162
  const srcPath = path.join(rootDir, srcDir, src);
173
163
  const destPath = path.join(outDirPath, src);
package/compiler/utils.js CHANGED
@@ -26,7 +26,6 @@ export function throwError(err) {
26
26
  export async function loadWithAssets(filePath) {
27
27
  let code = await fs.readFile(filePath, "utf8");
28
28
 
29
- // Find all `import varName from "*.html"` or `import varName from "*.css"`
30
29
  const importRegex = /import\s+(\w+)\s+from\s+["'](.+?\.(html|css))["'];?/g;
31
30
 
32
31
  let match;
@@ -34,16 +33,13 @@ export async function loadWithAssets(filePath) {
34
33
  const varName = match[1];
35
34
  const relPath = match[2];
36
35
 
37
- // Resolve relative path and read the asset file
38
36
  const absPath = path.resolve(path.dirname(filePath), relPath);
39
37
  let content = await fs.readFile(absPath, "utf8");
40
38
 
41
- // Replace the import with a const assignment of the file contents
42
39
  const replacement = `const ${varName} = ${JSON.stringify(content)};`;
43
40
  code = code.replace(match[0], replacement);
44
41
  }
45
42
 
46
- // Convert to data URL and import dynamically to avoid filesystem restrictions
47
43
  const dataUrl =
48
44
  "data:text/javascript;base64," +
49
45
  Buffer.from(code).toString("base64");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chocola",
3
- "version": "1.3.2",
3
+ "version": "1.3.4",
4
4
  "description": "The sweetest way to build reactive web apps",
5
5
  "keywords": [
6
6
  "web",
package/chocola-1.3.1.tgz DELETED
Binary file
package/types/index.js DELETED
@@ -1,135 +0,0 @@
1
- const iconsType = "home" | "search" | "menu" | "settings" | "favorite" | "account_circle" | "shopping_cart" | "notifications" | "info" | "help" | "exit_to_app" | "check_circle" | "close" | "edit" | "delete" | "add" | "arrow_back" | "arrow_forward" | "play_arrow" | "pause" | "stop" | "camera_alt" | "photo" | "alarm" | "event" | "attach_file" | "print" | "share" | "cloud" | "cloud_upload" | "cloud_download" | "lock" | "lock_open" | "visibility" | "visibility_off" | "phone" | "email" | "map" | "place" | "directions" | "train" | "directions_car" | "directions_bike" | "school" | "work" | "lightbulb" | "battery_full" | "battery_std" | "wifi" | "bluetooth";
2
- const iconsArray = [
3
- "home", "search", "menu", "settings", "favorite", "account_circle",
4
- "shopping_cart", "notifications", "info", "help", "exit_to_app",
5
- "check_circle", "close", "edit", "delete", "add", "arrow_back",
6
- "arrow_forward", "play_arrow", "pause", "stop", "camera_alt",
7
- "photo", "alarm", "event", "attach_file", "print", "share",
8
- "cloud", "cloud_upload", "cloud_download", "lock", "lock_open",
9
- "visibility", "visibility_off", "phone", "email", "map", "place",
10
- "directions", "train", "directions_car", "directions_bike", "school",
11
- "work", "lightbulb", "battery_full", "battery_std", "wifi", "bluetooth"
12
- ];
13
- /**
14
- * An instrinsic object that contains chocola types
15
- */
16
- const ct = {};
17
- /**
18
- * Creates an interface for the component static context
19
- * @param {object} ctxInterface
20
- */
21
- ct.defCtx = (ctxInterface) => {
22
- if (!ctxInterface) return undefined;
23
- if (typeof ctxInterface !== "object") return;
24
- return ctxInterface;
25
- }
26
-
27
- ct.Number = class Number {
28
- /**
29
- * @param {number} value
30
- * @returns
31
- */
32
- constructor(value) {
33
- if (!value) return undefined;
34
- if (typeof value !== "number") return;
35
- return value;
36
- }
37
- /**
38
- * @param {number} min
39
- * @param {number} max
40
- * @returns {number}
41
- */
42
- clamp(min, max) {
43
- return Math.min(Math.max(this.value, min), max);
44
- }
45
- /**
46
- * @param {number} min
47
- * @returns {number}
48
- */
49
- min(min) {
50
- return Math.min(this.value, min);
51
- }
52
- /**
53
- * @param {number} max
54
- * @returns {number}
55
- */
56
- max(max) {
57
- return Math.max(this.value, max);
58
- }
59
- }
60
- /**
61
- * @param {number} value
62
- */
63
- ct.Float = (value) => {
64
- if (!value) return undefined;
65
- if (Number.isInteger(value)) return;
66
- return value;
67
- }
68
- /**
69
- * @param {number} value
70
- */
71
- ct.Int = (value) => {
72
- if (!value) return undefined;
73
- if (!Number.isInteger(value)) return;
74
- return value;
75
- }
76
- /**
77
- * @param {string} value
78
- */
79
- ct.String = (value) => {
80
- if (!value) return undefined;
81
- if (typeof value !== "string") return;
82
- return value;
83
- }
84
- /**
85
- * @param {boolean} value
86
- */
87
- ct.Boolean = (value) => {
88
- if (value === null) return undefined;
89
- if (typeof value !== "boolean") return;
90
- return value;
91
- }
92
- /**
93
- * @param {object} value
94
- */
95
- ct.Object = (value) => {
96
- if (!value) return undefined;
97
- if (typeof value !== "object") return;
98
- return value;
99
- }
100
- /**
101
- * @param {function} value
102
- */
103
- ct.Function = (value) => {
104
- if (!value) return undefined;
105
- if (typeof value !== "function") return;
106
- return value;
107
- }
108
- /**
109
- * @param {symbol} value
110
- */
111
- ct.Symbol = (value) => {
112
- if (!value) return undefined;
113
- if (typeof value !== "symbol") return;
114
- return value;
115
- }
116
- /**
117
- * @param {URLPattern | "none"} value
118
- */
119
- ct.Url = (value) => {
120
- if (!value) return undefined;
121
- if (!value.startsWith("http://") || !value.startsWith("https://")) return;
122
- return value;
123
- }
124
- /**
125
- *
126
- * @param {"home" | "search" | "menu" | "settings" | "favorite" | "account_circle" | "shopping_cart" | "notifications" | "info" | "help" | "exit_to_app" | "check_circle" | "close" | "edit" | "delete" | "add" | "arrow_back" | "arrow_forward" | "play_arrow" | "pause" | "stop" | "camera_alt" | "photo" | "alarm" | "event" | "attach_file" | "print" | "share" | "cloud" | "cloud_upload" | "cloud_download" | "lock" | "lock_open" | "visibility" | "visibility_off" | "phone" | "email" | "map" | "place" | "directions" | "train" | "directions_car" | "directions_bike" | "school" | "work" | "lightbulb" | "battery_full" | "battery_std" | "wifi" | "bluetooth"} value
127
- * @returns
128
- */
129
- ct.Icon = (value) => {
130
- if (!value) return "help";
131
- if (!iconsArray.includes(value)) return "help";
132
- return value;
133
- }
134
-
135
- export default ct;