@zenithbuild/cli 0.6.17 → 0.7.1

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 (64) hide show
  1. package/dist/build/compiler-runtime.d.ts +59 -0
  2. package/dist/build/compiler-runtime.js +277 -0
  3. package/dist/build/expression-rewrites.d.ts +88 -0
  4. package/dist/build/expression-rewrites.js +372 -0
  5. package/dist/build/hoisted-code-transforms.d.ts +44 -0
  6. package/dist/build/hoisted-code-transforms.js +316 -0
  7. package/dist/build/merge-component-ir.d.ts +16 -0
  8. package/dist/build/merge-component-ir.js +257 -0
  9. package/dist/build/page-component-loop.d.ts +92 -0
  10. package/dist/build/page-component-loop.js +257 -0
  11. package/dist/build/page-ir-normalization.d.ts +23 -0
  12. package/dist/build/page-ir-normalization.js +370 -0
  13. package/dist/build/page-loop-metrics.d.ts +100 -0
  14. package/dist/build/page-loop-metrics.js +131 -0
  15. package/dist/build/page-loop-state.d.ts +261 -0
  16. package/dist/build/page-loop-state.js +92 -0
  17. package/dist/build/page-loop.d.ts +33 -0
  18. package/dist/build/page-loop.js +217 -0
  19. package/dist/build/scoped-identifier-rewrite.d.ts +112 -0
  20. package/dist/build/scoped-identifier-rewrite.js +245 -0
  21. package/dist/build/server-script.d.ts +41 -0
  22. package/dist/build/server-script.js +210 -0
  23. package/dist/build/type-declarations.d.ts +16 -0
  24. package/dist/build/type-declarations.js +158 -0
  25. package/dist/build/typescript-expression-utils.d.ts +23 -0
  26. package/dist/build/typescript-expression-utils.js +272 -0
  27. package/dist/build.d.ts +10 -18
  28. package/dist/build.js +74 -2261
  29. package/dist/component-instance-ir.d.ts +2 -2
  30. package/dist/component-instance-ir.js +146 -39
  31. package/dist/component-occurrences.js +63 -15
  32. package/dist/config.d.ts +66 -0
  33. package/dist/config.js +86 -0
  34. package/dist/debug-script.d.ts +1 -0
  35. package/dist/debug-script.js +8 -0
  36. package/dist/dev-build-session.d.ts +23 -0
  37. package/dist/dev-build-session.js +421 -0
  38. package/dist/dev-server.js +256 -54
  39. package/dist/framework-components/Image.zen +316 -0
  40. package/dist/images/materialize.d.ts +17 -0
  41. package/dist/images/materialize.js +200 -0
  42. package/dist/images/payload.d.ts +18 -0
  43. package/dist/images/payload.js +65 -0
  44. package/dist/images/runtime.d.ts +4 -0
  45. package/dist/images/runtime.js +254 -0
  46. package/dist/images/service.d.ts +4 -0
  47. package/dist/images/service.js +302 -0
  48. package/dist/images/shared.d.ts +58 -0
  49. package/dist/images/shared.js +306 -0
  50. package/dist/index.js +2 -17
  51. package/dist/manifest.js +45 -0
  52. package/dist/preview.d.ts +4 -1
  53. package/dist/preview.js +59 -6
  54. package/dist/resolve-components.js +20 -3
  55. package/dist/server-contract.js +3 -2
  56. package/dist/server-script-composition.d.ts +39 -0
  57. package/dist/server-script-composition.js +133 -0
  58. package/dist/startup-profile.d.ts +10 -0
  59. package/dist/startup-profile.js +62 -0
  60. package/dist/toolchain-paths.d.ts +1 -0
  61. package/dist/toolchain-paths.js +31 -0
  62. package/dist/version-check.d.ts +2 -1
  63. package/dist/version-check.js +12 -5
  64. package/package.json +5 -4
@@ -0,0 +1,421 @@
1
+ import { existsSync } from 'node:fs';
2
+ import { createHash } from 'node:crypto';
3
+ import { resolve } from 'node:path';
4
+ import { buildComponentRegistry } from './resolve-components.js';
5
+ import { collectAssets, createCompilerWarningEmitter, runBundler } from './build/compiler-runtime.js';
6
+ import { buildPageEnvelopes } from './build/page-loop.js';
7
+ import { createPageLoopCaches } from './build/page-loop-state.js';
8
+ import { deriveProjectRootFromPagesDir, ensureZenithTypeDeclarations } from './build/type-declarations.js';
9
+ import { buildImageArtifacts } from './images/service.js';
10
+ import { createImageRuntimePayload } from './images/payload.js';
11
+ import { createStartupProfiler } from './startup-profile.js';
12
+ import { resolveBundlerBin } from './toolchain-paths.js';
13
+ import { createBundlerToolchain, createCompilerToolchain, ensureToolchainCompatibility, getActiveToolchainCandidate } from './toolchain-runner.js';
14
+ import { maybeWarnAboutZenithVersionMismatch } from './version-check.js';
15
+ import { generateManifest } from './manifest.js';
16
+ function createCompilerTotals() {
17
+ return {
18
+ pageMs: 0,
19
+ ownerMs: 0,
20
+ componentMs: 0,
21
+ pageCalls: 0,
22
+ ownerCalls: 0,
23
+ componentCalls: 0,
24
+ componentCacheHits: 0,
25
+ componentCacheMisses: 0
26
+ };
27
+ }
28
+ function createExpressionRewriteMetrics() {
29
+ return {
30
+ calls: 0,
31
+ cacheHits: 0,
32
+ cacheMisses: 0,
33
+ templateCompileMs: 0
34
+ };
35
+ }
36
+ function toManifestEntryMap(manifest, pagesDir) {
37
+ const map = new Map();
38
+ for (const entry of manifest) {
39
+ map.set(resolve(pagesDir, entry.file), entry);
40
+ }
41
+ return map;
42
+ }
43
+ function orderEnvelopes(manifest, pagesDir, envelopeByFile) {
44
+ const ordered = [];
45
+ for (const entry of manifest) {
46
+ const envelope = envelopeByFile.get(resolve(pagesDir, entry.file));
47
+ if (!envelope) {
48
+ return null;
49
+ }
50
+ ordered.push(envelope);
51
+ }
52
+ return ordered;
53
+ }
54
+ function isCssOnlyChange(changedFiles) {
55
+ return changedFiles.length > 0 && changedFiles.every((filePath) => filePath.endsWith('.css'));
56
+ }
57
+ function stableJson(value) {
58
+ if (value === null || value === undefined) {
59
+ return 'null';
60
+ }
61
+ if (Array.isArray(value)) {
62
+ return `[${value.map((entry) => stableJson(entry)).join(',')}]`;
63
+ }
64
+ if (typeof value === 'object') {
65
+ return `{${Object.keys(value).sort().map((key) => `${JSON.stringify(key)}:${stableJson(value[key])}`).join(',')}}`;
66
+ }
67
+ return JSON.stringify(value);
68
+ }
69
+ function collectJsImportSpecifiers(source) {
70
+ const values = [];
71
+ const patterns = [
72
+ /\bimport\s+(?:[^'"\n;]*?\s+from\s+)?['"]([^'"]+)['"]/g,
73
+ /\bimport\s*\(\s*['"]([^'"]+)['"]\s*\)/g,
74
+ /\bexport\s+[^'"\n;]*?\s+from\s+['"]([^'"]+)['"]/g
75
+ ];
76
+ for (const pattern of patterns) {
77
+ pattern.lastIndex = 0;
78
+ for (const match of source.matchAll(pattern)) {
79
+ const value = String(match[1] || '').trim();
80
+ if (value.length > 0 && !values.includes(value)) {
81
+ values.push(value);
82
+ }
83
+ }
84
+ }
85
+ return values.sort();
86
+ }
87
+ function isExternalRuntimeSpecifier(specifier) {
88
+ return !specifier.startsWith('.')
89
+ && !specifier.startsWith('/')
90
+ && !specifier.startsWith('@/')
91
+ && !specifier.startsWith('\0zenith:')
92
+ && !specifier.includes('zenith:');
93
+ }
94
+ function collectEnvelopeAssetContract(envelope) {
95
+ const cssImportSpecifiers = new Set();
96
+ const externalImportSpecifiers = new Set();
97
+ for (const entry of envelope.ir.hoisted?.imports || []) {
98
+ for (const specifier of collectJsImportSpecifiers(String(entry || ''))) {
99
+ if (specifier.endsWith('.css')) {
100
+ cssImportSpecifiers.add(specifier);
101
+ }
102
+ if (isExternalRuntimeSpecifier(specifier)) {
103
+ externalImportSpecifiers.add(specifier);
104
+ }
105
+ }
106
+ }
107
+ for (const moduleEntry of envelope.ir.modules || []) {
108
+ for (const specifier of collectJsImportSpecifiers(String(moduleEntry?.source || ''))) {
109
+ if (specifier.endsWith('.css')) {
110
+ cssImportSpecifiers.add(specifier);
111
+ }
112
+ if (isExternalRuntimeSpecifier(specifier)) {
113
+ externalImportSpecifiers.add(specifier);
114
+ }
115
+ }
116
+ }
117
+ for (const importEntry of envelope.ir.imports || []) {
118
+ const specifier = String(importEntry?.spec || '').trim();
119
+ if (!specifier) {
120
+ continue;
121
+ }
122
+ if (specifier.endsWith('.css')) {
123
+ cssImportSpecifiers.add(specifier);
124
+ }
125
+ if (isExternalRuntimeSpecifier(specifier)) {
126
+ externalImportSpecifiers.add(specifier);
127
+ }
128
+ }
129
+ return {
130
+ componentHoistIds: Object.keys(envelope.ir.components_scripts || {}).sort(),
131
+ cssImportSpecifiers: [...cssImportSpecifiers].sort(),
132
+ externalImportSpecifiers: [...externalImportSpecifiers].sort()
133
+ };
134
+ }
135
+ function collectTemplateClassSignature(envelope) {
136
+ const html = typeof envelope?.ir?.html === 'string' ? envelope.ir.html : '';
137
+ if (!html) {
138
+ return [];
139
+ }
140
+ const classes = new Set();
141
+ const classAttrRe = /\bclass\s*=\s*(?:"([^"]*)"|'([^']*)')/gi;
142
+ let match;
143
+ while ((match = classAttrRe.exec(html)) !== null) {
144
+ const rawValue = String(match[1] || match[2] || '');
145
+ for (const token of rawValue.split(/\s+/)) {
146
+ const value = token.trim();
147
+ if (value.length > 0) {
148
+ classes.add(value);
149
+ }
150
+ }
151
+ }
152
+ return [...classes].sort();
153
+ }
154
+ function buildPageOnlyFastPathSignature(envelope) {
155
+ return stableJson({
156
+ route: envelope.route,
157
+ router: envelope.router === true,
158
+ assetContract: collectEnvelopeAssetContract(envelope),
159
+ templateClassSignature: collectTemplateClassSignature(envelope),
160
+ styleBlocks: envelope.ir.style_blocks || [],
161
+ serverScript: envelope.ir.server_script || null,
162
+ prerender: envelope.ir.prerender === true,
163
+ hasGuard: envelope.ir.has_guard === true,
164
+ hasLoad: envelope.ir.has_load === true,
165
+ guardModuleRef: envelope.ir.guard_module_ref || null,
166
+ loadModuleRef: envelope.ir.load_module_ref || null
167
+ });
168
+ }
169
+ function buildGlobalGraphHash(envelopes) {
170
+ const nodesByHoistId = new Map();
171
+ const edgeSet = new Set();
172
+ for (const envelope of envelopes) {
173
+ for (const node of envelope.ir.graph_nodes || []) {
174
+ if (node && typeof node.hoist_id === 'string' && node.hoist_id.length > 0) {
175
+ nodesByHoistId.set(node.hoist_id, true);
176
+ }
177
+ }
178
+ for (const edge of envelope.ir.graph_edges || []) {
179
+ if (typeof edge === 'string' && edge.length > 0) {
180
+ edgeSet.add(edge);
181
+ }
182
+ }
183
+ }
184
+ let seed = '';
185
+ for (const hoistId of [...nodesByHoistId.keys()].sort()) {
186
+ seed += `node:${hoistId}\n`;
187
+ }
188
+ for (const edge of [...edgeSet].sort()) {
189
+ seed += `edge:${edge}\n`;
190
+ }
191
+ return createHash('sha256').update(seed).digest('hex');
192
+ }
193
+ function selectPageOnlyEntries(changedFiles, pagesDir, manifestEntryByPath) {
194
+ if (changedFiles.length === 0) {
195
+ return [];
196
+ }
197
+ const selected = new Map();
198
+ for (const filePath of changedFiles) {
199
+ const resolvedPath = resolve(filePath);
200
+ if (!resolvedPath.startsWith(pagesDir) || !resolvedPath.endsWith('.zen') || !existsSync(resolvedPath)) {
201
+ return [];
202
+ }
203
+ const entry = manifestEntryByPath.get(resolvedPath);
204
+ if (!entry) {
205
+ return [];
206
+ }
207
+ selected.set(entry.file, entry);
208
+ }
209
+ return [...selected.values()];
210
+ }
211
+ async function maybeRunVersionCheck({ state, startupProfile, projectRoot, logger, bundlerBin }) {
212
+ if (state.versionChecked) {
213
+ return;
214
+ }
215
+ const resolvedBundlerCandidate = getActiveToolchainCandidate(bundlerBin);
216
+ await startupProfile.measureAsync('version_mismatch_check', () => maybeWarnAboutZenithVersionMismatch({
217
+ projectRoot,
218
+ logger,
219
+ command: 'dev',
220
+ bundlerBinPath: resolvedBundlerCandidate?.path || resolveBundlerBin(projectRoot)
221
+ }));
222
+ state.versionChecked = true;
223
+ }
224
+ function buildCompilerWarningEmitter(logger) {
225
+ return createCompilerWarningEmitter((line) => {
226
+ if (logger && typeof logger.warn === 'function') {
227
+ logger.warn(line, { onceKey: `compiler-warning:${line}` });
228
+ return;
229
+ }
230
+ console.warn(line);
231
+ });
232
+ }
233
+ export function createDevBuildSession(options) {
234
+ const { pagesDir, outDir, config = {}, logger = null } = options;
235
+ const resolvedPagesDir = resolve(pagesDir);
236
+ const projectRoot = deriveProjectRootFromPagesDir(resolvedPagesDir);
237
+ const srcDir = resolve(resolvedPagesDir, '..');
238
+ const compilerBin = createCompilerToolchain({ projectRoot, logger });
239
+ const bundlerBin = createBundlerToolchain({ projectRoot, logger });
240
+ const softNavigationEnabled = config.softNavigation === true || config.router === true;
241
+ const compilerOpts = {
242
+ typescriptDefault: config.typescriptDefault === true,
243
+ experimentalEmbeddedMarkup: config.embeddedMarkupExpressions === true
244
+ || config.experimental?.embeddedMarkupExpressions === true,
245
+ strictDomLints: config.strictDomLints === true
246
+ };
247
+ ensureToolchainCompatibility(bundlerBin);
248
+ const state = {
249
+ versionChecked: false,
250
+ registry: new Map(),
251
+ manifest: [],
252
+ manifestEntryByPath: new Map(),
253
+ envelopeByFile: new Map(),
254
+ pageOnlyFastPathSignatureByFile: new Map(),
255
+ globalGraphHash: '',
256
+ pageLoopCaches: createPageLoopCaches(),
257
+ hasSuccessfulBuild: false,
258
+ imageManifest: {},
259
+ imageRuntimePayload: createImageRuntimePayload(config.images, {}, 'endpoint')
260
+ };
261
+ async function syncImageState(startupProfile) {
262
+ const { manifest } = await startupProfile.measureAsync('build_image_artifacts', () => buildImageArtifacts({
263
+ projectRoot,
264
+ outDir,
265
+ config: config.images
266
+ }));
267
+ state.imageManifest = manifest;
268
+ state.imageRuntimePayload = createImageRuntimePayload(config.images, manifest, 'endpoint');
269
+ }
270
+ async function runBundlerWithCachedEnvelopes(startupProfile, activeLogger, showBundlerInfo, bundlerOptions = {}) {
271
+ const orderedEnvelopes = bundlerOptions.envelopesOverride
272
+ || orderEnvelopes(state.manifest, resolvedPagesDir, state.envelopeByFile);
273
+ if (!orderedEnvelopes || orderedEnvelopes.length === 0) {
274
+ throw new Error('Dev rebuild cache is incomplete; full rebuild required.');
275
+ }
276
+ await startupProfile.measureAsync('run_bundler', () => runBundler(orderedEnvelopes, outDir, projectRoot, activeLogger, showBundlerInfo, bundlerBin, {
277
+ devStableAssets: true,
278
+ rebuildStrategy: bundlerOptions.rebuildStrategy || 'full',
279
+ changedRoutes: bundlerOptions.changedRoutes || [],
280
+ fastPath: bundlerOptions.fastPath === true,
281
+ globalGraphHash: bundlerOptions.globalGraphHash || ''
282
+ }), { envelopes: orderedEnvelopes.length });
283
+ const assets = await startupProfile.measureAsync('collect_assets', () => collectAssets(outDir));
284
+ return { assets, envelopeCount: orderedEnvelopes.length };
285
+ }
286
+ async function runFullBuild(activeLogger, showBundlerInfo) {
287
+ const startupProfile = createStartupProfiler('cli-build');
288
+ const compilerTotals = createCompilerTotals();
289
+ await maybeRunVersionCheck({
290
+ state,
291
+ startupProfile,
292
+ projectRoot,
293
+ logger: activeLogger,
294
+ bundlerBin
295
+ });
296
+ state.registry = startupProfile.measureSync('build_component_registry', () => buildComponentRegistry(srcDir));
297
+ state.manifest = await startupProfile.measureAsync('generate_manifest', () => generateManifest(resolvedPagesDir));
298
+ await startupProfile.measureAsync('ensure_zenith_type_declarations', () => ensureZenithTypeDeclarations({
299
+ manifest: state.manifest,
300
+ pagesDir: resolvedPagesDir
301
+ }));
302
+ state.pageLoopCaches = createPageLoopCaches();
303
+ const emitCompilerWarning = buildCompilerWarningEmitter(activeLogger);
304
+ const { envelopes, expressionRewriteMetrics } = await buildPageEnvelopes({
305
+ manifest: state.manifest,
306
+ pagesDir: resolvedPagesDir,
307
+ srcDir,
308
+ registry: state.registry,
309
+ compilerOpts,
310
+ compilerBin,
311
+ softNavigationEnabled,
312
+ startupProfile,
313
+ compilerTotals,
314
+ emitCompilerWarning,
315
+ pageLoopCaches: state.pageLoopCaches
316
+ });
317
+ state.envelopeByFile = new Map(envelopes.map((entry) => [entry.file, entry]));
318
+ state.manifestEntryByPath = toManifestEntryMap(state.manifest, resolvedPagesDir);
319
+ state.pageOnlyFastPathSignatureByFile = new Map(envelopes.map((entry) => [entry.file, buildPageOnlyFastPathSignature(entry)]));
320
+ state.globalGraphHash = buildGlobalGraphHash(envelopes);
321
+ const { assets } = await runBundlerWithCachedEnvelopes(startupProfile, activeLogger, showBundlerInfo);
322
+ await syncImageState(startupProfile);
323
+ startupProfile.emit('build_complete', {
324
+ pages: state.manifest.length,
325
+ assets: assets.length,
326
+ compilerTotals,
327
+ expressionRewriteMetrics,
328
+ strategy: 'full'
329
+ });
330
+ state.hasSuccessfulBuild = true;
331
+ return { pages: state.manifest.length, assets, strategy: 'full' };
332
+ }
333
+ async function runBundleOnlyBuild(activeLogger, showBundlerInfo) {
334
+ const startupProfile = createStartupProfiler('cli-build');
335
+ const compilerTotals = createCompilerTotals();
336
+ const expressionRewriteMetrics = createExpressionRewriteMetrics();
337
+ const { assets } = await runBundlerWithCachedEnvelopes(startupProfile, activeLogger, showBundlerInfo, { rebuildStrategy: 'bundle-only' });
338
+ await syncImageState(startupProfile);
339
+ startupProfile.emit('build_complete', {
340
+ pages: state.manifest.length,
341
+ assets: assets.length,
342
+ compilerTotals,
343
+ expressionRewriteMetrics,
344
+ strategy: 'bundle-only'
345
+ });
346
+ return { pages: state.manifest.length, assets, strategy: 'bundle-only' };
347
+ }
348
+ async function runPageOnlyBuild(entries, activeLogger, showBundlerInfo) {
349
+ const startupProfile = createStartupProfiler('cli-build');
350
+ const compilerTotals = createCompilerTotals();
351
+ const emitCompilerWarning = buildCompilerWarningEmitter(activeLogger);
352
+ const { envelopes, expressionRewriteMetrics } = await buildPageEnvelopes({
353
+ manifest: entries,
354
+ pagesDir: resolvedPagesDir,
355
+ srcDir,
356
+ registry: state.registry,
357
+ compilerOpts,
358
+ compilerBin,
359
+ softNavigationEnabled,
360
+ startupProfile,
361
+ compilerTotals,
362
+ emitCompilerWarning,
363
+ pageLoopCaches: state.pageLoopCaches
364
+ });
365
+ const previousFastPathSignatures = new Map(state.pageOnlyFastPathSignatureByFile);
366
+ const canUseFastPath = entries.every((entry, index) => {
367
+ const previous = previousFastPathSignatures.get(resolve(resolvedPagesDir, entry.file));
368
+ const next = buildPageOnlyFastPathSignature(envelopes[index]);
369
+ return typeof previous === 'string' && previous === next;
370
+ });
371
+ for (const envelope of envelopes) {
372
+ state.envelopeByFile.set(envelope.file, envelope);
373
+ state.pageOnlyFastPathSignatureByFile.set(envelope.file, buildPageOnlyFastPathSignature(envelope));
374
+ }
375
+ const orderedEnvelopes = orderEnvelopes(state.manifest, resolvedPagesDir, state.envelopeByFile);
376
+ if (!orderedEnvelopes || orderedEnvelopes.length === 0) {
377
+ throw new Error('Dev rebuild cache is incomplete; full rebuild required.');
378
+ }
379
+ state.globalGraphHash = buildGlobalGraphHash(orderedEnvelopes);
380
+ const { assets } = await runBundlerWithCachedEnvelopes(startupProfile, activeLogger, showBundlerInfo, {
381
+ rebuildStrategy: 'page-only',
382
+ changedRoutes: entries.map((entry) => entry.path),
383
+ fastPath: canUseFastPath,
384
+ envelopesOverride: canUseFastPath ? envelopes : orderedEnvelopes,
385
+ globalGraphHash: canUseFastPath ? state.globalGraphHash : ''
386
+ });
387
+ await syncImageState(startupProfile);
388
+ startupProfile.emit('build_complete', {
389
+ pages: state.manifest.length,
390
+ assets: assets.length,
391
+ compilerTotals,
392
+ expressionRewriteMetrics,
393
+ strategy: 'page-only',
394
+ rebuiltPages: entries.length
395
+ });
396
+ return { pages: state.manifest.length, assets, strategy: 'page-only', rebuiltPages: entries.length };
397
+ }
398
+ return {
399
+ async build(buildOptions = {}) {
400
+ const activeLogger = buildOptions.logger || logger;
401
+ const showBundlerInfo = buildOptions.showBundlerInfo !== false;
402
+ const changedFiles = Array.isArray(buildOptions.changedFiles)
403
+ ? [...new Set(buildOptions.changedFiles.map((entry) => resolve(String(entry))))]
404
+ : [];
405
+ if (!state.hasSuccessfulBuild || changedFiles.length === 0) {
406
+ return runFullBuild(activeLogger, showBundlerInfo);
407
+ }
408
+ if (isCssOnlyChange(changedFiles)) {
409
+ return runBundleOnlyBuild(activeLogger, showBundlerInfo);
410
+ }
411
+ const pageOnlyEntries = selectPageOnlyEntries(changedFiles, resolvedPagesDir, state.manifestEntryByPath);
412
+ if (pageOnlyEntries.length > 0) {
413
+ return runPageOnlyBuild(pageOnlyEntries, activeLogger, showBundlerInfo);
414
+ }
415
+ return runFullBuild(activeLogger, showBundlerInfo);
416
+ },
417
+ getImageRuntimePayload() {
418
+ return state.imageRuntimePayload;
419
+ }
420
+ };
421
+ }