zero-query 1.0.9 → 1.2.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 (154) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +2 -0
  3. package/cli/args.js +33 -33
  4. package/cli/commands/build-api.js +443 -0
  5. package/cli/commands/build.js +254 -216
  6. package/cli/commands/bundle.js +1228 -1183
  7. package/cli/commands/create.js +137 -121
  8. package/cli/commands/dev/devtools/index.js +56 -56
  9. package/cli/commands/dev/devtools/js/components.js +49 -49
  10. package/cli/commands/dev/devtools/js/core.js +423 -423
  11. package/cli/commands/dev/devtools/js/elements.js +421 -421
  12. package/cli/commands/dev/devtools/js/network.js +166 -166
  13. package/cli/commands/dev/devtools/js/performance.js +73 -73
  14. package/cli/commands/dev/devtools/js/router.js +105 -105
  15. package/cli/commands/dev/devtools/js/source.js +132 -132
  16. package/cli/commands/dev/devtools/js/stats.js +35 -35
  17. package/cli/commands/dev/devtools/js/tabs.js +79 -79
  18. package/cli/commands/dev/devtools/panel.html +95 -95
  19. package/cli/commands/dev/devtools/styles.css +244 -244
  20. package/cli/commands/dev/index.js +107 -107
  21. package/cli/commands/dev/logger.js +75 -75
  22. package/cli/commands/dev/overlay.js +858 -858
  23. package/cli/commands/dev/server.js +220 -167
  24. package/cli/commands/dev/validator.js +94 -94
  25. package/cli/commands/dev/watcher.js +172 -172
  26. package/cli/help.js +114 -112
  27. package/cli/index.js +52 -52
  28. package/cli/scaffold/default/LICENSE +21 -21
  29. package/cli/scaffold/default/app/app.js +207 -207
  30. package/cli/scaffold/default/app/components/about.js +201 -201
  31. package/cli/scaffold/default/app/components/api-demo.js +143 -143
  32. package/cli/scaffold/default/app/components/contact-card.js +231 -231
  33. package/cli/scaffold/default/app/components/contacts/contacts.css +706 -706
  34. package/cli/scaffold/default/app/components/contacts/contacts.html +200 -200
  35. package/cli/scaffold/default/app/components/contacts/contacts.js +196 -196
  36. package/cli/scaffold/default/app/components/counter.js +127 -127
  37. package/cli/scaffold/default/app/components/home.js +249 -249
  38. package/cli/scaffold/default/app/components/not-found.js +16 -16
  39. package/cli/scaffold/default/app/components/playground/playground.css +115 -115
  40. package/cli/scaffold/default/app/components/playground/playground.html +161 -161
  41. package/cli/scaffold/default/app/components/playground/playground.js +116 -116
  42. package/cli/scaffold/default/app/components/todos.js +225 -225
  43. package/cli/scaffold/default/app/components/toolkit/toolkit.css +97 -97
  44. package/cli/scaffold/default/app/components/toolkit/toolkit.html +146 -146
  45. package/cli/scaffold/default/app/components/toolkit/toolkit.js +280 -280
  46. package/cli/scaffold/default/app/routes.js +15 -15
  47. package/cli/scaffold/default/app/store.js +101 -101
  48. package/cli/scaffold/default/global.css +552 -552
  49. package/cli/scaffold/default/index.html +99 -99
  50. package/cli/scaffold/minimal/app/app.js +85 -85
  51. package/cli/scaffold/minimal/app/components/about.js +68 -68
  52. package/cli/scaffold/minimal/app/components/counter.js +122 -122
  53. package/cli/scaffold/minimal/app/components/home.js +68 -68
  54. package/cli/scaffold/minimal/app/components/not-found.js +16 -16
  55. package/cli/scaffold/minimal/app/routes.js +9 -9
  56. package/cli/scaffold/minimal/app/store.js +36 -36
  57. package/cli/scaffold/minimal/global.css +300 -300
  58. package/cli/scaffold/minimal/index.html +44 -44
  59. package/cli/scaffold/ssr/app/app.js +41 -41
  60. package/cli/scaffold/ssr/app/components/about.js +55 -55
  61. package/cli/scaffold/ssr/app/components/blog/index.js +65 -65
  62. package/cli/scaffold/ssr/app/components/blog/post.js +86 -86
  63. package/cli/scaffold/ssr/app/components/home.js +37 -37
  64. package/cli/scaffold/ssr/app/components/not-found.js +15 -15
  65. package/cli/scaffold/ssr/app/routes.js +8 -8
  66. package/cli/scaffold/ssr/global.css +228 -228
  67. package/cli/scaffold/ssr/index.html +37 -37
  68. package/cli/scaffold/ssr/package.json +8 -8
  69. package/cli/scaffold/ssr/server/data/posts.js +144 -144
  70. package/cli/scaffold/ssr/server/index.js +213 -213
  71. package/cli/scaffold/webrtc/app/app.js +11 -0
  72. package/cli/scaffold/webrtc/app/components/video-room.js +295 -0
  73. package/cli/scaffold/webrtc/app/lib/room.js +252 -0
  74. package/cli/scaffold/webrtc/assets/.gitkeep +0 -0
  75. package/cli/scaffold/webrtc/global.css +250 -0
  76. package/cli/scaffold/webrtc/index.html +21 -0
  77. package/cli/utils.js +305 -287
  78. package/dist/API.md +7264 -0
  79. package/dist/zquery.dist.zip +0 -0
  80. package/dist/zquery.js +10313 -6252
  81. package/dist/zquery.min.js +8 -601
  82. package/index.d.ts +570 -365
  83. package/index.js +311 -232
  84. package/package.json +76 -69
  85. package/src/component.js +1709 -1454
  86. package/src/core.js +921 -921
  87. package/src/diff.js +497 -497
  88. package/src/errors.js +209 -209
  89. package/src/expression.js +922 -922
  90. package/src/http.js +242 -242
  91. package/src/package.json +1 -1
  92. package/src/reactive.js +255 -254
  93. package/src/router.js +843 -773
  94. package/src/ssr.js +418 -418
  95. package/src/store.js +318 -272
  96. package/src/utils.js +515 -515
  97. package/src/webrtc/e2ee.js +351 -0
  98. package/src/webrtc/errors.js +116 -0
  99. package/src/webrtc/ice.js +301 -0
  100. package/src/webrtc/index.js +131 -0
  101. package/src/webrtc/joinToken.js +119 -0
  102. package/src/webrtc/observe.js +172 -0
  103. package/src/webrtc/peer.js +351 -0
  104. package/src/webrtc/reactive.js +268 -0
  105. package/src/webrtc/room.js +625 -0
  106. package/src/webrtc/sdp.js +302 -0
  107. package/src/webrtc/sfu/index.js +43 -0
  108. package/src/webrtc/sfu/livekit.js +131 -0
  109. package/src/webrtc/sfu/mediasoup.js +150 -0
  110. package/src/webrtc/signaling.js +373 -0
  111. package/src/webrtc/turn.js +237 -0
  112. package/tests/_helpers/webrtcFakes.js +289 -0
  113. package/tests/audit.test.js +4158 -4158
  114. package/tests/cli.test.js +1136 -1023
  115. package/tests/compare.test.js +497 -0
  116. package/tests/component.test.js +3969 -3938
  117. package/tests/core.test.js +1910 -1910
  118. package/tests/dev-server.test.js +489 -0
  119. package/tests/diff.test.js +1416 -1416
  120. package/tests/docs.test.js +1664 -0
  121. package/tests/electron-features.test.js +864 -0
  122. package/tests/errors.test.js +619 -619
  123. package/tests/expression.test.js +1056 -1056
  124. package/tests/http.test.js +648 -648
  125. package/tests/reactive.test.js +819 -819
  126. package/tests/router.test.js +2327 -2327
  127. package/tests/ssr.test.js +870 -870
  128. package/tests/store.test.js +830 -830
  129. package/tests/test-minifier.js +153 -153
  130. package/tests/test-ssr.js +27 -27
  131. package/tests/utils.test.js +1377 -1377
  132. package/tests/webrtc/e2ee.test.js +283 -0
  133. package/tests/webrtc/ice.test.js +202 -0
  134. package/tests/webrtc/joinToken.test.js +89 -0
  135. package/tests/webrtc/observe.test.js +111 -0
  136. package/tests/webrtc/peer.test.js +373 -0
  137. package/tests/webrtc/reactive.test.js +235 -0
  138. package/tests/webrtc/room.test.js +406 -0
  139. package/tests/webrtc/sdp.test.js +151 -0
  140. package/tests/webrtc/sfu-livekit.test.js +119 -0
  141. package/tests/webrtc/sfu.test.js +160 -0
  142. package/tests/webrtc/signaling.test.js +251 -0
  143. package/tests/webrtc/turn.test.js +256 -0
  144. package/types/collection.d.ts +383 -383
  145. package/types/component.d.ts +186 -186
  146. package/types/errors.d.ts +135 -135
  147. package/types/http.d.ts +92 -92
  148. package/types/misc.d.ts +201 -201
  149. package/types/reactive.d.ts +98 -98
  150. package/types/router.d.ts +190 -190
  151. package/types/ssr.d.ts +102 -102
  152. package/types/store.d.ts +146 -145
  153. package/types/utils.d.ts +245 -245
  154. package/types/webrtc.d.ts +653 -0
@@ -1,216 +1,254 @@
1
- /**
2
- * cli/commands/build.js - library build command
3
- *
4
- * Concatenates the zQuery source modules into dist/zquery.js and
5
- * dist/zquery.min.js.
6
- */
7
-
8
- 'use strict';
9
-
10
- const fs = require('fs');
11
- const path = require('path');
12
- const zlib = require('zlib');
13
- const { execSync } = require('child_process');
14
- const { minify, sizeKB } = require('../utils');
15
-
16
- function buildLibrary() {
17
- const pkg = JSON.parse(fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf-8'));
18
- const VERSION = pkg.version;
19
-
20
- const modules = [
21
- 'src/errors.js',
22
- 'src/reactive.js', 'src/diff.js', 'src/core.js', 'src/expression.js',
23
- 'src/component.js', 'src/router.js', 'src/store.js', 'src/http.js',
24
- 'src/utils.js',
25
- ];
26
-
27
- const DIST = path.join(process.cwd(), 'dist');
28
- const OUT_FILE = path.join(DIST, 'zquery.js');
29
- const MIN_FILE = path.join(DIST, 'zquery.min.js');
30
-
31
- const start = Date.now();
32
- if (!fs.existsSync(DIST)) fs.mkdirSync(DIST, { recursive: true });
33
-
34
- // -----------------------------------------------------------------------
35
- // Run unit tests and capture results for $.unitTests
36
- // -----------------------------------------------------------------------
37
- let testResults = { passed: 0, failed: 0, total: 0, suites: 0, duration: 0, ok: false };
38
- try {
39
- const json = execSync('npx vitest run --reporter=json', {
40
- cwd: process.cwd(),
41
- encoding: 'utf-8',
42
- stdio: ['pipe', 'pipe', 'pipe'],
43
- timeout: 120000,
44
- });
45
- // vitest --reporter=json outputs JSON to stdout; may include non-JSON lines
46
- const jsonStart = json.indexOf('{');
47
- if (jsonStart !== -1) {
48
- const parsed = JSON.parse(json.slice(jsonStart));
49
- const passed = parsed.numPassedTests || 0;
50
- const failed = parsed.numFailedTests || 0;
51
- const total = parsed.numTotalTests || 0;
52
- const suites = parsed.numTotalTestSuites || 0;
53
- const dur = Math.round((parsed.testResults || []).reduce((s, r) => s + (r.endTime - r.startTime), 0));
54
- testResults = { passed, failed, total, suites, duration: dur, ok: parsed.success !== false };
55
- }
56
- console.log(` ✓ Tests: ${testResults.passed}/${testResults.total} passed (${testResults.suites} suites)\n`);
57
- } catch (err) {
58
- // Tests may fail but we still want to capture the numbers
59
- const out = (err.stdout || '') + (err.stderr || '');
60
- const jsonStart = out.indexOf('{');
61
- if (jsonStart !== -1) {
62
- try {
63
- const parsed = JSON.parse(out.slice(jsonStart));
64
- testResults = {
65
- passed: parsed.numPassedTests || 0,
66
- failed: parsed.numFailedTests || 0,
67
- total: parsed.numTotalTests || 0,
68
- suites: parsed.numTotalTestSuites || 0,
69
- duration: Math.round((parsed.testResults || []).reduce((s, r) => s + (r.endTime - r.startTime), 0)),
70
- ok: false,
71
- };
72
- } catch (_) { /* keep defaults */ }
73
- }
74
- console.log(` ⚠ Tests: ${testResults.passed}/${testResults.total} passed, ${testResults.failed} failed\n`);
75
- }
76
-
77
- const parts = modules.map(file => {
78
- let code = fs.readFileSync(path.join(process.cwd(), file), 'utf-8');
79
- code = code.replace(/^import\s+[\s\S]*?from\s+['"].*?['"];?\s*$/gm, '');
80
- code = code.replace(/^export\s+(default\s+)?/gm, '');
81
- code = code.replace(/^export\s*\{[\s\S]*?\};\s*$/gm, '');
82
- return `// --- ${file} ${'-'.repeat(60 - file.length)}\n${code.trim()}`;
83
- });
84
-
85
- let indexCode = fs.readFileSync(path.join(process.cwd(), 'index.js'), 'utf-8');
86
- indexCode = indexCode.replace(/^import\s+[\s\S]*?from\s+['"].*?['"];?\s*$/gm, '');
87
- indexCode = indexCode.replace(/^export\s*\{[\s\S]*?\};\s*$/gm, '');
88
- indexCode = indexCode.replace(/^export\s+(default\s+)?/gm, '');
89
-
90
- const banner = `/**\n * zQuery (zeroQuery) v${VERSION}\n * Lightweight Frontend Library\n * https://github.com/tonywied17/zero-query\n * (c) ${new Date().getFullYear()} Anthony Wiedman - MIT License\n */`;
91
-
92
- const bundle = `${banner}\n(function(global) {\n 'use strict';\n\n${parts.join('\n\n')}\n\n// --- index.js (assembly) ${'-'.repeat(42)}\n${indexCode.trim().replace("'__VERSION__'", `'${VERSION}'`)}\n\n})(typeof window !== 'undefined' ? window : globalThis);\n`;
93
-
94
- fs.writeFileSync(OUT_FILE, bundle, 'utf-8');
95
- const minified = minify(bundle, banner);
96
- fs.writeFileSync(MIN_FILE, minified, 'utf-8');
97
-
98
- // Inject actual minified library size into both outputs
99
- const libSizeKB = Math.round(Buffer.from(minified).length / 1024);
100
- const libSizeStr = `~${libSizeKB} KB`;
101
- const testObj = JSON.stringify(testResults);
102
- let outContent = fs.readFileSync(OUT_FILE, 'utf-8').replace("'__LIB_SIZE__'", `'${libSizeStr}'`);
103
- let minContent = minified.replace("'__LIB_SIZE__'", `'${libSizeStr}'`);
104
- outContent = outContent.replace("'__UNIT_TESTS__'", testObj);
105
- minContent = minContent.replace("'__UNIT_TESTS__'", testObj);
106
- fs.writeFileSync(OUT_FILE, outContent, 'utf-8');
107
- fs.writeFileSync(MIN_FILE, minContent, 'utf-8');
108
-
109
- const elapsed = Date.now() - start;
110
- console.log(` ✓ dist/zquery.js (${sizeKB(fs.readFileSync(OUT_FILE))} KB)`);
111
- console.log(` ✓ dist/zquery.min.js (${sizeKB(fs.readFileSync(MIN_FILE))} KB)`);
112
- console.log(` Done in ${elapsed}ms\n`);
113
-
114
- // --- Create dist/zquery.dist.zip -----------------------------------------
115
- const root = process.cwd();
116
- const zipFiles = [
117
- { src: OUT_FILE, name: 'zquery.js' },
118
- { src: MIN_FILE, name: 'zquery.min.js' },
119
- { src: path.join(root, 'LICENSE'), name: 'LICENSE' },
120
- { src: path.join(root, 'API.md'), name: 'API.md' },
121
- { src: path.join(root, 'README.md'), name: 'README.md' },
122
- ];
123
-
124
- // Minimal ZIP builder (deflate via zlib, no external deps)
125
- function buildZip(entries) {
126
- const localHeaders = [];
127
- const centralHeaders = [];
128
- let offset = 0;
129
-
130
- for (const { name, data } of entries) {
131
- const nameBytes = Buffer.from(name, 'utf-8');
132
- const compressed = zlib.deflateRawSync(data, { level: 9 });
133
- const crc = crc32(data);
134
- const compLen = compressed.length;
135
- const uncompLen = data.length;
136
-
137
- // Local file header
138
- const local = Buffer.alloc(30 + nameBytes.length);
139
- local.writeUInt32LE(0x04034b50, 0); // signature
140
- local.writeUInt16LE(20, 4); // version needed
141
- local.writeUInt16LE(0, 6); // flags
142
- local.writeUInt16LE(8, 8); // compression: deflate
143
- local.writeUInt16LE(0, 10); // mod time
144
- local.writeUInt16LE(0, 12); // mod date
145
- local.writeUInt32LE(crc, 14);
146
- local.writeUInt32LE(compLen, 18);
147
- local.writeUInt32LE(uncompLen, 22);
148
- local.writeUInt16LE(nameBytes.length, 26);
149
- local.writeUInt16LE(0, 28); // extra field length
150
- nameBytes.copy(local, 30);
151
-
152
- localHeaders.push(Buffer.concat([local, compressed]));
153
-
154
- // Central directory header
155
- const central = Buffer.alloc(46 + nameBytes.length);
156
- central.writeUInt32LE(0x02014b50, 0);
157
- central.writeUInt16LE(20, 4); // version made by
158
- central.writeUInt16LE(20, 6); // version needed
159
- central.writeUInt16LE(0, 8); // flags
160
- central.writeUInt16LE(8, 10); // compression: deflate
161
- central.writeUInt16LE(0, 12); // mod time
162
- central.writeUInt16LE(0, 14); // mod date
163
- central.writeUInt32LE(crc, 16);
164
- central.writeUInt32LE(compLen, 20);
165
- central.writeUInt32LE(uncompLen, 24);
166
- central.writeUInt16LE(nameBytes.length, 28);
167
- central.writeUInt16LE(0, 30); // extra field length
168
- central.writeUInt16LE(0, 32); // comment length
169
- central.writeUInt16LE(0, 34); // disk number
170
- central.writeUInt16LE(0, 36); // internal attrs
171
- central.writeUInt32LE(0, 38); // external attrs
172
- central.writeUInt32LE(offset, 42); // local header offset
173
- nameBytes.copy(central, 46);
174
- centralHeaders.push(central);
175
-
176
- offset += local.length + compressed.length;
177
- }
178
-
179
- const centralDir = Buffer.concat(centralHeaders);
180
- const eocd = Buffer.alloc(22);
181
- eocd.writeUInt32LE(0x06054b50, 0);
182
- eocd.writeUInt16LE(0, 4); // disk number
183
- eocd.writeUInt16LE(0, 6); // central dir disk
184
- eocd.writeUInt16LE(entries.length, 8); // entries on disk
185
- eocd.writeUInt16LE(entries.length, 10); // total entries
186
- eocd.writeUInt32LE(centralDir.length, 12);
187
- eocd.writeUInt32LE(offset, 16); // central dir offset
188
- eocd.writeUInt16LE(0, 20); // comment length
189
-
190
- return Buffer.concat([...localHeaders, centralDir, eocd]);
191
- }
192
-
193
- function crc32(buf) {
194
- let crc = 0xFFFFFFFF;
195
- for (let i = 0; i < buf.length; i++) {
196
- crc ^= buf[i];
197
- for (let j = 0; j < 8; j++) {
198
- crc = (crc >>> 1) ^ ((crc & 1) ? 0xEDB88320 : 0);
199
- }
200
- }
201
- return (crc ^ 0xFFFFFFFF) >>> 0;
202
- }
203
-
204
- const entries = zipFiles
205
- .filter(f => fs.existsSync(f.src))
206
- .map(f => ({ name: f.name, data: fs.readFileSync(f.src) }));
207
-
208
- const zipBuf = buildZip(entries);
209
- const zipPath = path.join(DIST, 'zquery.dist.zip');
210
- fs.writeFileSync(zipPath, zipBuf);
211
- console.log(` ✓ dist/zquery.dist.zip (${sizeKB(zipBuf)} KB) - ${entries.length} files`);
212
-
213
- return { DIST, OUT_FILE, MIN_FILE };
214
- }
215
-
216
- module.exports = buildLibrary;
1
+ /**
2
+ * cli/commands/build.js - library build command
3
+ *
4
+ * Concatenates the zQuery source modules into dist/zquery.js and
5
+ * dist/zquery.min.js.
6
+ */
7
+
8
+ 'use strict';
9
+
10
+ const fs = require('fs');
11
+ const path = require('path');
12
+ const zlib = require('zlib');
13
+ const { execSync } = require('child_process');
14
+ const { minify, sizeKB } = require('../utils');
15
+
16
+ function buildLibrary() {
17
+ const pkg = JSON.parse(fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf-8'));
18
+ const VERSION = pkg.version;
19
+
20
+ const modules = [
21
+ 'src/errors.js',
22
+ 'src/webrtc/errors.js', 'src/webrtc/sdp.js', 'src/webrtc/ice.js', 'src/webrtc/signaling.js', 'src/webrtc/peer.js', 'src/webrtc/room.js', 'src/webrtc/reactive.js', 'src/webrtc/turn.js', 'src/webrtc/e2ee.js', 'src/webrtc/joinToken.js', 'src/webrtc/observe.js', 'src/webrtc/sfu/mediasoup.js', 'src/webrtc/sfu/livekit.js', 'src/webrtc/sfu/index.js', 'src/webrtc/index.js',
23
+ 'src/reactive.js', 'src/diff.js', 'src/core.js', 'src/expression.js',
24
+ 'src/component.js', 'src/router.js', 'src/store.js', 'src/http.js',
25
+ 'src/utils.js',
26
+ ];
27
+
28
+ // Guard: ensure we're inside the zero-query source repo
29
+ const missing = modules.filter(m => !fs.existsSync(path.join(process.cwd(), m)));
30
+ if (missing.length > 0) {
31
+ console.error(` "zquery build" must be run from the zero-query source repository.`);
32
+ console.error(` Missing source files: ${missing.join(', ')}`);
33
+ console.error(` To bundle your app for production, use "zquery bundle" instead.\n`);
34
+ process.exit(1);
35
+ }
36
+
37
+ const DIST = path.join(process.cwd(), 'dist');
38
+ const OUT_FILE = path.join(DIST, 'zquery.js');
39
+ const MIN_FILE = path.join(DIST, 'zquery.min.js');
40
+
41
+ const start = Date.now();
42
+ if (!fs.existsSync(DIST)) fs.mkdirSync(DIST, { recursive: true });
43
+
44
+ // -----------------------------------------------------------------------
45
+ // Run unit tests and capture results for $.unitTests
46
+ // -----------------------------------------------------------------------
47
+ let testResults = { passed: 0, failed: 0, total: 0, suites: 0, duration: 0, ok: false };
48
+ try {
49
+ const json = execSync('npx vitest run --reporter=json', {
50
+ cwd: process.cwd(),
51
+ encoding: 'utf-8',
52
+ stdio: ['pipe', 'pipe', 'pipe'],
53
+ timeout: 120000,
54
+ });
55
+ // vitest --reporter=json outputs JSON to stdout; may include non-JSON lines
56
+ const jsonStart = json.indexOf('{');
57
+ if (jsonStart !== -1) {
58
+ const parsed = JSON.parse(json.slice(jsonStart));
59
+ const passed = parsed.numPassedTests || 0;
60
+ const failed = parsed.numFailedTests || 0;
61
+ const total = parsed.numTotalTests || 0;
62
+ const suites = parsed.numTotalTestSuites || 0;
63
+ const dur = Math.round((parsed.testResults || []).reduce((s, r) => s + (r.endTime - r.startTime), 0));
64
+ testResults = { passed, failed, total, suites, duration: dur, ok: parsed.success !== false };
65
+ }
66
+ console.log(` Tests: ${testResults.passed}/${testResults.total} passed (${testResults.suites} suites)\n`);
67
+ } catch (err) {
68
+ // Tests may fail but we still want to capture the numbers
69
+ const out = (err.stdout || '') + (err.stderr || '');
70
+ const jsonStart = out.indexOf('{');
71
+ if (jsonStart !== -1) {
72
+ try {
73
+ const parsed = JSON.parse(out.slice(jsonStart));
74
+ testResults = {
75
+ passed: parsed.numPassedTests || 0,
76
+ failed: parsed.numFailedTests || 0,
77
+ total: parsed.numTotalTests || 0,
78
+ suites: parsed.numTotalTestSuites || 0,
79
+ duration: Math.round((parsed.testResults || []).reduce((s, r) => s + (r.endTime - r.startTime), 0)),
80
+ ok: false,
81
+ };
82
+ } catch (_) { /* keep defaults */ }
83
+ }
84
+ console.log(` ⚠ Tests: ${testResults.passed}/${testResults.total} passed, ${testResults.failed} failed\n`);
85
+ }
86
+
87
+ const parts = modules.map(file => {
88
+ let code = fs.readFileSync(path.join(process.cwd(), file), 'utf-8');
89
+ code = code.replace(/^import\s+[\s\S]*?from\s+['"].*?['"];?\s*$/gm, '');
90
+ code = code.replace(/^export\s+(?:\*|\{[\s\S]*?\})\s+from\s+['"].*?['"];?\s*$/gm, '');
91
+ code = code.replace(/^export\s+(default\s+)?/gm, '');
92
+ code = code.replace(/^export\s*\{[\s\S]*?\};\s*$/gm, '');
93
+ return `// --- ${file} ${'-'.repeat(60 - file.length)}\n${code.trim()}`;
94
+ });
95
+
96
+ let indexCode = fs.readFileSync(path.join(process.cwd(), 'index.js'), 'utf-8');
97
+ indexCode = indexCode.replace(/^import\s+[\s\S]*?from\s+['"].*?['"];?\s*$/gm, '');
98
+ indexCode = indexCode.replace(/^export\s+(?:\*|\{[\s\S]*?\})\s+from\s+['"].*?['"];?\s*$/gm, '');
99
+ indexCode = indexCode.replace(/^export\s*\{[\s\S]*?\};\s*$/gm, '');
100
+ indexCode = indexCode.replace(/^export\s+(default\s+)?/gm, '');
101
+
102
+ const banner = `/**\n * zQuery (zeroQuery) v${VERSION}\n * Lightweight Frontend Library\n * https://github.com/tonywied17/zero-query\n * (c) ${new Date().getFullYear()} Anthony Wiedman - MIT License\n */`;
103
+
104
+ const bundle = `${banner}\n(function(global) {\n 'use strict';\n\n${parts.join('\n\n')}\n\n// --- index.js (assembly) ${'-'.repeat(42)}\n${indexCode.trim().replace("'__VERSION__'", `'${VERSION}'`)}\n\n})(typeof window !== 'undefined' ? window : globalThis);\n`;
105
+
106
+ fs.writeFileSync(OUT_FILE, bundle, 'utf-8');
107
+ const minified = minify(bundle, banner);
108
+ fs.writeFileSync(MIN_FILE, minified, 'utf-8');
109
+
110
+ // Inject actual minified library size into both outputs.
111
+ // The unminified bundle keeps single quotes; esbuild rewrites the minified
112
+ // output with double quotes, so we tolerate both.
113
+ const libSizeKB = Math.round(Buffer.from(minified).length / 1024);
114
+ const libSizeStr = `~${libSizeKB} KB`;
115
+ const testObj = JSON.stringify(testResults);
116
+ const libRe = /(['"])__LIB_SIZE__\1/g;
117
+ const testRe = /(['"])__UNIT_TESTS__\1/g;
118
+ let outContent = fs.readFileSync(OUT_FILE, 'utf-8').replace(libRe, `'${libSizeStr}'`);
119
+ let minContent = minified.replace(libRe, JSON.stringify(libSizeStr));
120
+ outContent = outContent.replace(testRe, testObj);
121
+ minContent = minContent.replace(testRe, testObj);
122
+ fs.writeFileSync(OUT_FILE, outContent, 'utf-8');
123
+ fs.writeFileSync(MIN_FILE, minContent, 'utf-8');
124
+
125
+ const elapsed = Date.now() - start;
126
+ console.log(` ✓ dist/zquery.js (${sizeKB(fs.readFileSync(OUT_FILE))} KB)`);
127
+ console.log(` ✓ dist/zquery.min.js (${sizeKB(fs.readFileSync(MIN_FILE))} KB)`);
128
+ console.log(` Done in ${elapsed}ms\n`);
129
+
130
+ // --- Generate API.md before zipping --------------------------------------
131
+ const root = process.cwd();
132
+ try {
133
+ const buildApi = require('./build-api');
134
+ // buildApi() is async (dynamic imports), so we run it synchronously via
135
+ // a child process to keep the build function synchronous.
136
+ execSync('node cli/commands/build-api.js', {
137
+ cwd: root,
138
+ stdio: 'inherit',
139
+ timeout: 60000,
140
+ });
141
+ } catch (err) {
142
+ console.log(' ⚠ API.md generation skipped:', err.message || 'unknown error');
143
+ }
144
+
145
+ // --- Copy API.md into dist -----------------------------------------------
146
+ const apiSrc = path.join(root, 'API.md');
147
+ const apiDist = path.join(DIST, 'API.md');
148
+ if (fs.existsSync(apiSrc)) {
149
+ fs.copyFileSync(apiSrc, apiDist);
150
+ console.log(` ✓ dist/API.md (${sizeKB(fs.readFileSync(apiDist))} KB)`);
151
+ }
152
+
153
+ // --- Create dist/zquery.dist.zip -----------------------------------------
154
+ const zipFiles = [
155
+ { src: OUT_FILE, name: 'zquery.js' },
156
+ { src: MIN_FILE, name: 'zquery.min.js' },
157
+ { src: path.join(root, 'LICENSE'), name: 'LICENSE' },
158
+ { src: apiDist, name: 'API.md' },
159
+ { src: path.join(root, 'README.md'), name: 'README.md' },
160
+ ];
161
+
162
+ // Minimal ZIP builder (deflate via zlib, no external deps)
163
+ function buildZip(entries) {
164
+ const localHeaders = [];
165
+ const centralHeaders = [];
166
+ let offset = 0;
167
+
168
+ for (const { name, data } of entries) {
169
+ const nameBytes = Buffer.from(name, 'utf-8');
170
+ const compressed = zlib.deflateRawSync(data, { level: 9 });
171
+ const crc = crc32(data);
172
+ const compLen = compressed.length;
173
+ const uncompLen = data.length;
174
+
175
+ // Local file header
176
+ const local = Buffer.alloc(30 + nameBytes.length);
177
+ local.writeUInt32LE(0x04034b50, 0); // signature
178
+ local.writeUInt16LE(20, 4); // version needed
179
+ local.writeUInt16LE(0, 6); // flags
180
+ local.writeUInt16LE(8, 8); // compression: deflate
181
+ local.writeUInt16LE(0, 10); // mod time
182
+ local.writeUInt16LE(0, 12); // mod date
183
+ local.writeUInt32LE(crc, 14);
184
+ local.writeUInt32LE(compLen, 18);
185
+ local.writeUInt32LE(uncompLen, 22);
186
+ local.writeUInt16LE(nameBytes.length, 26);
187
+ local.writeUInt16LE(0, 28); // extra field length
188
+ nameBytes.copy(local, 30);
189
+
190
+ localHeaders.push(Buffer.concat([local, compressed]));
191
+
192
+ // Central directory header
193
+ const central = Buffer.alloc(46 + nameBytes.length);
194
+ central.writeUInt32LE(0x02014b50, 0);
195
+ central.writeUInt16LE(20, 4); // version made by
196
+ central.writeUInt16LE(20, 6); // version needed
197
+ central.writeUInt16LE(0, 8); // flags
198
+ central.writeUInt16LE(8, 10); // compression: deflate
199
+ central.writeUInt16LE(0, 12); // mod time
200
+ central.writeUInt16LE(0, 14); // mod date
201
+ central.writeUInt32LE(crc, 16);
202
+ central.writeUInt32LE(compLen, 20);
203
+ central.writeUInt32LE(uncompLen, 24);
204
+ central.writeUInt16LE(nameBytes.length, 28);
205
+ central.writeUInt16LE(0, 30); // extra field length
206
+ central.writeUInt16LE(0, 32); // comment length
207
+ central.writeUInt16LE(0, 34); // disk number
208
+ central.writeUInt16LE(0, 36); // internal attrs
209
+ central.writeUInt32LE(0, 38); // external attrs
210
+ central.writeUInt32LE(offset, 42); // local header offset
211
+ nameBytes.copy(central, 46);
212
+ centralHeaders.push(central);
213
+
214
+ offset += local.length + compressed.length;
215
+ }
216
+
217
+ const centralDir = Buffer.concat(centralHeaders);
218
+ const eocd = Buffer.alloc(22);
219
+ eocd.writeUInt32LE(0x06054b50, 0);
220
+ eocd.writeUInt16LE(0, 4); // disk number
221
+ eocd.writeUInt16LE(0, 6); // central dir disk
222
+ eocd.writeUInt16LE(entries.length, 8); // entries on disk
223
+ eocd.writeUInt16LE(entries.length, 10); // total entries
224
+ eocd.writeUInt32LE(centralDir.length, 12);
225
+ eocd.writeUInt32LE(offset, 16); // central dir offset
226
+ eocd.writeUInt16LE(0, 20); // comment length
227
+
228
+ return Buffer.concat([...localHeaders, centralDir, eocd]);
229
+ }
230
+
231
+ function crc32(buf) {
232
+ let crc = 0xFFFFFFFF;
233
+ for (let i = 0; i < buf.length; i++) {
234
+ crc ^= buf[i];
235
+ for (let j = 0; j < 8; j++) {
236
+ crc = (crc >>> 1) ^ ((crc & 1) ? 0xEDB88320 : 0);
237
+ }
238
+ }
239
+ return (crc ^ 0xFFFFFFFF) >>> 0;
240
+ }
241
+
242
+ const entries = zipFiles
243
+ .filter(f => fs.existsSync(f.src))
244
+ .map(f => ({ name: f.name, data: fs.readFileSync(f.src) }));
245
+
246
+ const zipBuf = buildZip(entries);
247
+ const zipPath = path.join(DIST, 'zquery.dist.zip');
248
+ fs.writeFileSync(zipPath, zipBuf);
249
+ console.log(` ✓ dist/zquery.dist.zip (${sizeKB(zipBuf)} KB) - ${entries.length} files`);
250
+
251
+ return { DIST, OUT_FILE, MIN_FILE };
252
+ }
253
+
254
+ module.exports = buildLibrary;