slicejs-cli 4.1.1 → 4.3.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.
- package/client.js +60 -3
- package/commands/build/build.js +18 -1
- package/commands/buildProduction/buildProduction.js +82 -81
- package/commands/docs/docs.js +365 -0
- package/commands/types/types.js +116 -7
- package/commands/utils/JsMinifier.js +230 -0
- package/commands/utils/SourceDiagnostics.js +201 -0
- package/commands/utils/bundling/BundleGenerator.js +622 -59
- package/package.json +1 -1
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
import Print from '../Print.js';
|
|
2
|
+
|
|
3
|
+
const DEFAULT_FRAMEWORK_BASE_URL = 'https://raw.githubusercontent.com/VKneider/slicejs_docs/master/';
|
|
4
|
+
const DEFAULT_COMPONENTS_MANIFEST_URL = 'https://raw.githubusercontent.com/VKneider/slice.js_visual_library/master/agent/docs-manifest.json';
|
|
5
|
+
const DEFAULT_COMPONENTS_RAW_BASE_URL = 'https://raw.githubusercontent.com/VKneider/slice.js_visual_library';
|
|
6
|
+
|
|
7
|
+
const SOURCE_ALIASES = new Map([
|
|
8
|
+
['framework', 'framework'],
|
|
9
|
+
['slice-docs', 'framework'],
|
|
10
|
+
['slicejs-docs', 'framework'],
|
|
11
|
+
['components', 'components'],
|
|
12
|
+
['visual', 'components'],
|
|
13
|
+
['visual-library', 'components'],
|
|
14
|
+
['slice-components', 'components'],
|
|
15
|
+
['all', 'all']
|
|
16
|
+
]);
|
|
17
|
+
|
|
18
|
+
function normalizeSource(source = 'all') {
|
|
19
|
+
const normalized = SOURCE_ALIASES.get(String(source).trim().toLowerCase());
|
|
20
|
+
if (!normalized) {
|
|
21
|
+
throw new Error(`Unknown documentation source "${source}". Use framework, components, or all.`);
|
|
22
|
+
}
|
|
23
|
+
return normalized;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function joinUrl(base, suffix) {
|
|
27
|
+
return `${String(base).replace(/\/+$/, '')}/${String(suffix).replace(/^\/+/, '')}`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function fetchResponse(url, fetchImpl) {
|
|
31
|
+
const response = await fetchImpl(url);
|
|
32
|
+
if (!response.ok) {
|
|
33
|
+
throw new Error(`HTTP ${response.status} while fetching ${url}`);
|
|
34
|
+
}
|
|
35
|
+
return response;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function fetchText(url, fetchImpl) {
|
|
39
|
+
const response = await fetchResponse(url, fetchImpl);
|
|
40
|
+
return response.text();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function fetchJson(url, fetchImpl) {
|
|
44
|
+
const response = await fetchResponse(url, fetchImpl);
|
|
45
|
+
return response.json();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function getTitle(content, fallback) {
|
|
49
|
+
const titleLine = content.split(/\r?\n/).find((line) => line.startsWith('# '));
|
|
50
|
+
return titleLine ? titleLine.slice(2).trim() : fallback;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function parseFrameworkLlm(content, baseUrl, revision = 'master') {
|
|
54
|
+
const documents = [];
|
|
55
|
+
const sections = (`\n${content}`).split(/\n=== /).slice(1);
|
|
56
|
+
|
|
57
|
+
for (const section of sections) {
|
|
58
|
+
const lines = section.split('\n');
|
|
59
|
+
const path = lines[0].replace(' ===', '').trim();
|
|
60
|
+
const searchText = lines.slice(1).join('\n').trim();
|
|
61
|
+
if (!path || !searchText) continue;
|
|
62
|
+
|
|
63
|
+
const id = path.replace(/^markdown\//, '').replace(/\.md$/, '');
|
|
64
|
+
documents.push({
|
|
65
|
+
source: 'framework',
|
|
66
|
+
catalog: 'slicejs-docs',
|
|
67
|
+
id,
|
|
68
|
+
title: getTitle(searchText, id.split('/').pop()),
|
|
69
|
+
path,
|
|
70
|
+
sourcePath: path,
|
|
71
|
+
rawUrl: joinUrl(baseUrl, path),
|
|
72
|
+
description: '',
|
|
73
|
+
tags: [],
|
|
74
|
+
revision,
|
|
75
|
+
searchText
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return documents;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function normalizeComponentsManifest(manifest, rawBaseUrl, preferRawBaseUrl = false) {
|
|
83
|
+
if (!manifest || !Array.isArray(manifest.documents)) {
|
|
84
|
+
throw new Error('Components documentation manifest is missing a documents array');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return manifest.documents.map((document) => ({
|
|
88
|
+
...document,
|
|
89
|
+
source: 'components',
|
|
90
|
+
catalog: manifest.catalog || 'slicejs-components',
|
|
91
|
+
path: document.sourcePath,
|
|
92
|
+
rawUrl: preferRawBaseUrl || !document.rawUrl
|
|
93
|
+
? joinUrl(rawBaseUrl, `${manifest.revision || 'master'}/${document.sourcePath}`)
|
|
94
|
+
: document.rawUrl,
|
|
95
|
+
searchText: null,
|
|
96
|
+
revision: manifest.revision,
|
|
97
|
+
version: manifest.version
|
|
98
|
+
}));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function qualifiedId(document) {
|
|
102
|
+
return `${document.source}:${document.id}`;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function matchesDocument(document, requestedId) {
|
|
106
|
+
const value = String(requestedId).trim().toLowerCase();
|
|
107
|
+
return [document.id, document.component, document.title, qualifiedId(document)]
|
|
108
|
+
.filter(Boolean)
|
|
109
|
+
.some((candidate) => String(candidate).toLowerCase() === value);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function scoreDocument(document, content, query, maxSnippets) {
|
|
113
|
+
const phrase = query.toLowerCase().trim();
|
|
114
|
+
const terms = [...new Set(phrase.split(/\s+/).filter(Boolean))];
|
|
115
|
+
if (terms.length === 0) return null;
|
|
116
|
+
|
|
117
|
+
const lines = content.split(/\r?\n/);
|
|
118
|
+
const title = String(document.title || '').toLowerCase();
|
|
119
|
+
const tags = Array.isArray(document.tags) ? document.tags.join(' ').toLowerCase() : '';
|
|
120
|
+
const matched = new Set();
|
|
121
|
+
let score = 0;
|
|
122
|
+
|
|
123
|
+
for (const term of terms) {
|
|
124
|
+
if (title.includes(term)) {
|
|
125
|
+
score += 6;
|
|
126
|
+
matched.add(term);
|
|
127
|
+
}
|
|
128
|
+
if (tags.includes(term)) {
|
|
129
|
+
score += 4;
|
|
130
|
+
matched.add(term);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (terms.length > 1 && title.includes(phrase)) score += 10;
|
|
135
|
+
|
|
136
|
+
const hits = [];
|
|
137
|
+
let section = '';
|
|
138
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
139
|
+
const line = lines[index];
|
|
140
|
+
if (/^#{1,6}\s/.test(line)) section = line.replace(/^#{1,6}\s+/, '').trim();
|
|
141
|
+
|
|
142
|
+
const lower = line.toLowerCase();
|
|
143
|
+
let lineScore = 0;
|
|
144
|
+
for (const term of terms) {
|
|
145
|
+
if (lower.includes(term)) {
|
|
146
|
+
lineScore += /^#{1,6}\s/.test(line) ? 3 : 1;
|
|
147
|
+
matched.add(term);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (terms.length > 1 && lower.includes(phrase)) lineScore += 5;
|
|
151
|
+
if (lineScore > 0) {
|
|
152
|
+
score += lineScore;
|
|
153
|
+
hits.push({ line: index + 1, section: section || document.title, text: line.trim(), lineScore });
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (matched.size === 0) return null;
|
|
158
|
+
const coverage = matched.size / terms.length;
|
|
159
|
+
score *= 0.5 + 0.5 * coverage;
|
|
160
|
+
|
|
161
|
+
return {
|
|
162
|
+
doc_id: qualifiedId(document),
|
|
163
|
+
source: document.source,
|
|
164
|
+
title: document.title,
|
|
165
|
+
path: document.path,
|
|
166
|
+
relevance_score: Math.round(score * 100) / 100,
|
|
167
|
+
matched_terms: [...matched],
|
|
168
|
+
snippets: hits
|
|
169
|
+
.sort((a, b) => b.lineScore - a.lineScore || a.line - b.line)
|
|
170
|
+
.slice(0, maxSnippets)
|
|
171
|
+
.map(({ line, section: hitSection, text }) => ({ line, section: hitSection, text }))
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function createDocsClient(options = {}) {
|
|
176
|
+
const fetchImpl = options.fetchImpl || globalThis.fetch;
|
|
177
|
+
if (typeof fetchImpl !== 'function') {
|
|
178
|
+
throw new Error('A fetch implementation is required to access documentation');
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const frameworkBaseUrl = options.frameworkBaseUrl || process.env.SLICE_FRAMEWORK_DOCS_BASE_URL || DEFAULT_FRAMEWORK_BASE_URL;
|
|
182
|
+
const frameworkRevision = options.frameworkRevision || process.env.SLICE_FRAMEWORK_DOCS_REVISION || 'master';
|
|
183
|
+
const componentsManifestUrl = options.componentsManifestUrl || process.env.SLICE_COMPONENTS_MANIFEST_URL || DEFAULT_COMPONENTS_MANIFEST_URL;
|
|
184
|
+
const componentsRawBaseOverride = options.componentsRawBaseUrl || process.env.SLICE_COMPONENTS_RAW_BASE_URL || null;
|
|
185
|
+
const componentsRawBaseUrl = componentsRawBaseOverride || DEFAULT_COMPONENTS_RAW_BASE_URL;
|
|
186
|
+
const catalogCache = new Map();
|
|
187
|
+
const contentCache = new Map();
|
|
188
|
+
|
|
189
|
+
async function loadCatalog(source) {
|
|
190
|
+
const normalized = normalizeSource(source);
|
|
191
|
+
if (normalized === 'all') {
|
|
192
|
+
const [framework, components] = await Promise.all([
|
|
193
|
+
loadCatalog('framework'),
|
|
194
|
+
loadCatalog('components')
|
|
195
|
+
]);
|
|
196
|
+
return [...framework, ...components];
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (catalogCache.has(normalized)) return catalogCache.get(normalized);
|
|
200
|
+
|
|
201
|
+
const promise = normalized === 'framework'
|
|
202
|
+
? fetchText(joinUrl(frameworkBaseUrl, 'llm.txt'), fetchImpl)
|
|
203
|
+
.then((content) => parseFrameworkLlm(content, frameworkBaseUrl, frameworkRevision))
|
|
204
|
+
: fetchJson(componentsManifestUrl, fetchImpl)
|
|
205
|
+
.then((manifest) => normalizeComponentsManifest(manifest, componentsRawBaseUrl, Boolean(componentsRawBaseOverride)));
|
|
206
|
+
|
|
207
|
+
catalogCache.set(normalized, promise);
|
|
208
|
+
return promise;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
async function getContent(document) {
|
|
212
|
+
const key = qualifiedId(document);
|
|
213
|
+
if (contentCache.has(key)) return contentCache.get(key);
|
|
214
|
+
|
|
215
|
+
const promise = fetchText(document.rawUrl, fetchImpl);
|
|
216
|
+
contentCache.set(key, promise);
|
|
217
|
+
return promise;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
async function list(source = 'all') {
|
|
221
|
+
return loadCatalog(source);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
async function search(query, { source = 'all', maxResults = 5, snippetsPerDoc = 3 } = {}) {
|
|
225
|
+
const documents = await loadCatalog(source);
|
|
226
|
+
const results = [];
|
|
227
|
+
for (const document of documents) {
|
|
228
|
+
const content = document.searchText || await getContent(document);
|
|
229
|
+
const result = scoreDocument(document, content, query, snippetsPerDoc);
|
|
230
|
+
if (result) results.push(result);
|
|
231
|
+
}
|
|
232
|
+
return results
|
|
233
|
+
.sort((a, b) => b.relevance_score - a.relevance_score)
|
|
234
|
+
.slice(0, maxResults);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
async function get(ids, { source = 'all' } = {}) {
|
|
238
|
+
const requestedIds = Array.isArray(ids) ? ids : [ids];
|
|
239
|
+
const documents = await loadCatalog(source);
|
|
240
|
+
const results = [];
|
|
241
|
+
|
|
242
|
+
for (const requestedId of requestedIds) {
|
|
243
|
+
const document = documents.find((candidate) => matchesDocument(candidate, requestedId));
|
|
244
|
+
if (!document) {
|
|
245
|
+
throw new Error(`Documentation document not found: "${requestedId}" in source "${normalizeSource(source)}"`);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const content = await getContent(document);
|
|
249
|
+
results.push({
|
|
250
|
+
doc_id: qualifiedId(document),
|
|
251
|
+
source: document.source,
|
|
252
|
+
catalog: document.catalog,
|
|
253
|
+
id: document.id,
|
|
254
|
+
title: document.title,
|
|
255
|
+
content,
|
|
256
|
+
metadata: {
|
|
257
|
+
path: document.path,
|
|
258
|
+
sourcePath: document.sourcePath,
|
|
259
|
+
rawUrl: document.rawUrl,
|
|
260
|
+
route: document.route || null,
|
|
261
|
+
component: document.component || null,
|
|
262
|
+
kind: document.kind || null,
|
|
263
|
+
tags: document.tags || [],
|
|
264
|
+
version: document.version || null,
|
|
265
|
+
revision: document.revision || null
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
return results;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
return { list, search, get };
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function parseMaxResults(value, fallback = 5) {
|
|
277
|
+
const parsed = Number(value);
|
|
278
|
+
if (!Number.isInteger(parsed) || parsed < 1) return fallback;
|
|
279
|
+
return parsed;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
export async function runDocsList(options = {}) {
|
|
283
|
+
const client = createDocsClient();
|
|
284
|
+
const documents = await client.list(options.source || 'all');
|
|
285
|
+
const entries = documents.map((document) => ({
|
|
286
|
+
doc_id: qualifiedId(document),
|
|
287
|
+
source: document.source,
|
|
288
|
+
catalog: document.catalog,
|
|
289
|
+
id: document.id,
|
|
290
|
+
title: document.title,
|
|
291
|
+
path: document.path,
|
|
292
|
+
sourcePath: document.sourcePath,
|
|
293
|
+
component: document.component,
|
|
294
|
+
kind: document.kind,
|
|
295
|
+
route: document.route || null,
|
|
296
|
+
tags: document.tags || [],
|
|
297
|
+
version: document.version || null,
|
|
298
|
+
revision: document.revision || null
|
|
299
|
+
}));
|
|
300
|
+
if (options.json) {
|
|
301
|
+
console.log(JSON.stringify(entries));
|
|
302
|
+
return entries;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
Print.title(`Documentation (${normalizeSource(options.source || 'all')})`);
|
|
306
|
+
for (const document of documents) {
|
|
307
|
+
const suffix = document.component ? ` [${document.kind}]` : '';
|
|
308
|
+
console.log(`- ${document.source}:${document.id} — ${document.title}${suffix}`);
|
|
309
|
+
}
|
|
310
|
+
return entries;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
export async function runDocsSearch(query, options = {}) {
|
|
314
|
+
if (!query || !query.trim()) throw new Error('A search query is required');
|
|
315
|
+
const client = createDocsClient();
|
|
316
|
+
const results = await client.search(query, {
|
|
317
|
+
source: options.source || 'all',
|
|
318
|
+
maxResults: parseMaxResults(options.maxResults),
|
|
319
|
+
snippetsPerDoc: parseMaxResults(options.snippetsPerDoc, 3)
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
if (options.json) {
|
|
323
|
+
console.log(JSON.stringify(results));
|
|
324
|
+
return results;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
Print.title(`Documentation search: ${query}`);
|
|
328
|
+
for (const result of results) {
|
|
329
|
+
console.log(`- ${result.doc_id} — ${result.title} (${result.relevance_score})`);
|
|
330
|
+
for (const snippet of result.snippets) {
|
|
331
|
+
console.log(` ${snippet.section}: ${snippet.text}`);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
if (results.length === 0) Print.info('No matching documentation found.');
|
|
335
|
+
return results;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
export async function runDocsGet(ids, options = {}) {
|
|
339
|
+
if (!ids || ids.length === 0) throw new Error('At least one documentation ID is required');
|
|
340
|
+
const client = createDocsClient();
|
|
341
|
+
const results = await client.get(ids, { source: options.source || 'all' });
|
|
342
|
+
|
|
343
|
+
if (options.json) {
|
|
344
|
+
console.log(JSON.stringify(results.length === 1 ? results[0] : results));
|
|
345
|
+
return results;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
if (options.stdout) {
|
|
349
|
+
for (let index = 0; index < results.length; index += 1) {
|
|
350
|
+
if (index > 0) process.stdout.write('\n\n---\n\n');
|
|
351
|
+
process.stdout.write(results[index].content);
|
|
352
|
+
}
|
|
353
|
+
if (results.length > 0 && !results[results.length - 1].content.endsWith('\n')) process.stdout.write('\n');
|
|
354
|
+
return results;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
for (const result of results) {
|
|
358
|
+
Print.title(`${result.title} (${result.doc_id})`);
|
|
359
|
+
console.log(result.content);
|
|
360
|
+
Print.info(`Source: ${result.metadata.rawUrl}`);
|
|
361
|
+
}
|
|
362
|
+
return results;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
export { normalizeSource, parseFrameworkLlm, scoreDocument };
|
package/commands/types/types.js
CHANGED
|
@@ -5,6 +5,12 @@ import traverse from '@babel/traverse';
|
|
|
5
5
|
|
|
6
6
|
import Print from '../Print.js';
|
|
7
7
|
import { getConfigPath, getComponentsJsPath, joinRoot } from '../utils/PathHelper.js';
|
|
8
|
+
import {
|
|
9
|
+
describeSyntaxError,
|
|
10
|
+
findUnresolvedRelativeImports,
|
|
11
|
+
hasDefaultExport,
|
|
12
|
+
findUnknownComponentBuilds
|
|
13
|
+
} from '../utils/SourceDiagnostics.js';
|
|
8
14
|
|
|
9
15
|
const TYPE_MAP = {
|
|
10
16
|
string: 'string',
|
|
@@ -137,7 +143,7 @@ const payloadToTs = (payload) => {
|
|
|
137
143
|
} else if (value && typeof value === 'object' && typeof value.type === 'string') {
|
|
138
144
|
tsType = PAYLOAD_TYPE_MAP[value.type.toLowerCase()] || 'unknown';
|
|
139
145
|
}
|
|
140
|
-
return `${key}: ${tsType}`;
|
|
146
|
+
return `${tsPropertyKey(key)}: ${tsType}`;
|
|
141
147
|
})
|
|
142
148
|
.join('; ');
|
|
143
149
|
return inner.length > 0 ? `{ ${inner} }` : 'Record<string, unknown>';
|
|
@@ -465,7 +471,43 @@ const typeFromProp = (propMeta) => {
|
|
|
465
471
|
return TYPE_MAP[propMeta.type] || 'unknown';
|
|
466
472
|
};
|
|
467
473
|
|
|
468
|
-
|
|
474
|
+
/**
|
|
475
|
+
* True when `name` can be written as a bare TypeScript property key or
|
|
476
|
+
* identifier. Anything else has to be quoted (or escaped, for a type name).
|
|
477
|
+
* @param {string} name
|
|
478
|
+
* @returns {boolean}
|
|
479
|
+
*/
|
|
480
|
+
const isSafeTsIdentifier = (name) => /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(String(name));
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* Property key for a declaration line: bare when it can be, quoted otherwise.
|
|
484
|
+
* `data-id` and `my-btn` are legal keys but only in quoted form; emitting them
|
|
485
|
+
* bare produced a .d.ts that does not parse, which silently took out editor
|
|
486
|
+
* tooling for the whole project (jsconfig includes the generated file).
|
|
487
|
+
* @param {string} name
|
|
488
|
+
* @returns {string}
|
|
489
|
+
*/
|
|
490
|
+
const tsPropertyKey = (name) => (isSafeTsIdentifier(name) ? String(name) : JSON.stringify(String(name)));
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* Interface name for a component.
|
|
494
|
+
*
|
|
495
|
+
* A component's registered name need not be a valid identifier — `my-btn` used
|
|
496
|
+
* to yield `export interface my-btnProps`, which is a syntax error. Characters
|
|
497
|
+
* outside [A-Za-z0-9_$] are hex-escaped rather than dropped, so two distinct
|
|
498
|
+
* component names can never collapse onto the same interface (the same reason
|
|
499
|
+
* BundleGenerator.toSafeIdentifier escapes instead of replacing).
|
|
500
|
+
*
|
|
501
|
+
* @param {string} componentName
|
|
502
|
+
* @returns {string}
|
|
503
|
+
*/
|
|
504
|
+
const interfaceNameFor = (componentName) => {
|
|
505
|
+
const raw = String(componentName);
|
|
506
|
+
if (isSafeTsIdentifier(raw)) return `${raw}Props`;
|
|
507
|
+
const encoded = raw.replace(/[^A-Za-z0-9_$]/g, (char) => `_${char.charCodeAt(0).toString(16)}_`);
|
|
508
|
+
// Prefixed so a name starting with a digit still yields a valid identifier.
|
|
509
|
+
return `SliceComponent_${encoded}Props`;
|
|
510
|
+
};
|
|
469
511
|
const DYNAMIC_FALLBACK_PROP = '__dynamicPropsFallback';
|
|
470
512
|
|
|
471
513
|
const DEFAULT_EDITOR_COMPILER_OPTIONS = {
|
|
@@ -553,7 +595,9 @@ const generateEventRegistryLines = (eventRegistry) => {
|
|
|
553
595
|
const lines = [];
|
|
554
596
|
lines.push('export interface SliceEventRegistry {');
|
|
555
597
|
for (const name of names) {
|
|
556
|
-
|
|
598
|
+
// JSON.stringify, not a hand-rolled quote: an event name containing a quote
|
|
599
|
+
// would otherwise terminate the string early.
|
|
600
|
+
lines.push(` ${JSON.stringify(name)}: ${payloadToTs(events[name].payload)};`);
|
|
557
601
|
}
|
|
558
602
|
lines.push('}');
|
|
559
603
|
lines.push('');
|
|
@@ -612,7 +656,7 @@ const generateDeclarationContent = (componentPropsMap, eventRegistry = {}) => {
|
|
|
612
656
|
if (!isDynamicFallback) {
|
|
613
657
|
for (const [propName, propMeta] of Object.entries(sortedProps)) {
|
|
614
658
|
const optionalMark = propMeta.required ? '' : '?';
|
|
615
|
-
lines.push(` ${propName}${optionalMark}: ${typeFromProp(propMeta)};`);
|
|
659
|
+
lines.push(` ${tsPropertyKey(propName)}${optionalMark}: ${typeFromProp(propMeta)};`);
|
|
616
660
|
}
|
|
617
661
|
}
|
|
618
662
|
lines.push('}');
|
|
@@ -621,7 +665,7 @@ const generateDeclarationContent = (componentPropsMap, eventRegistry = {}) => {
|
|
|
621
665
|
|
|
622
666
|
lines.push('export interface SliceComponentPropsMap {');
|
|
623
667
|
for (const componentName of Object.keys(componentsSorted)) {
|
|
624
|
-
lines.push(` ${componentName}: ${interfaceNameFor(componentName)};`);
|
|
668
|
+
lines.push(` ${tsPropertyKey(componentName)}: ${interfaceNameFor(componentName)};`);
|
|
625
669
|
}
|
|
626
670
|
lines.push('}');
|
|
627
671
|
lines.push('');
|
|
@@ -771,9 +815,25 @@ const PROP_VALID_TYPES = new Set(['string', 'number', 'boolean', 'object', 'arra
|
|
|
771
815
|
* @param {{ projectRoot: string }} options
|
|
772
816
|
* @returns {Promise<{ errors: Array, warnings: Array, checkedCount: number }>}
|
|
773
817
|
*/
|
|
818
|
+
/**
|
|
819
|
+
* Walks every component registered in components.js.
|
|
820
|
+
*
|
|
821
|
+
* Note this covers MORE than the bundler does: the bundler only ever sees
|
|
822
|
+
* components reachable from a route, so a registered-but-unused component with a
|
|
823
|
+
* syntax error, a missing default export or a broken import was never checked
|
|
824
|
+
* anywhere — it failed the first time somebody navigated to it.
|
|
825
|
+
*
|
|
826
|
+
* @returns {Promise<{errors: object[], warnings: object[], sourceErrors: object[], checkedCount: number}>}
|
|
827
|
+
*/
|
|
774
828
|
const validateComponentProps = async ({ projectRoot }) => {
|
|
775
829
|
const errors = [];
|
|
776
830
|
const warnings = [];
|
|
831
|
+
// Problems with the file itself rather than with a prop definition. Kept
|
|
832
|
+
// separate because they are reported differently (they carry a file and a
|
|
833
|
+
// position, not a prop name).
|
|
834
|
+
const sourceErrors = [];
|
|
835
|
+
// Reported but not blocking — see findUnknownComponentBuilds below.
|
|
836
|
+
const sourceWarnings = [];
|
|
777
837
|
let checkedCount = 0;
|
|
778
838
|
|
|
779
839
|
const registryPath = getComponentsJsPath(import.meta.url, projectRoot);
|
|
@@ -781,10 +841,11 @@ const validateComponentProps = async ({ projectRoot }) => {
|
|
|
781
841
|
try {
|
|
782
842
|
registryContent = await fs.readFile(registryPath, 'utf8');
|
|
783
843
|
} catch {
|
|
784
|
-
return { errors, warnings, checkedCount }; // no registry → nothing to validate
|
|
844
|
+
return { errors, warnings, sourceErrors, sourceWarnings, checkedCount }; // no registry → nothing to validate
|
|
785
845
|
}
|
|
786
846
|
|
|
787
847
|
const registryMap = parseComponentsRegistry(registryContent, registryPath);
|
|
848
|
+
const registeredNames = new Set(Object.keys(registryMap));
|
|
788
849
|
const configPath = getConfigPath(import.meta.url, projectRoot);
|
|
789
850
|
const categoryPathCache = new Map();
|
|
790
851
|
|
|
@@ -803,6 +864,54 @@ const validateComponentProps = async ({ projectRoot }) => {
|
|
|
803
864
|
let source;
|
|
804
865
|
try { source = await fs.readFile(componentFile, 'utf8'); } catch { continue; }
|
|
805
866
|
|
|
867
|
+
const syntax = describeSyntaxError(source, componentFile);
|
|
868
|
+
if (!syntax.ok) {
|
|
869
|
+
sourceErrors.push({ component: componentName, message: `Syntax error at ${syntax.message}` });
|
|
870
|
+
continue; // nothing further can be said about a file that does not parse
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
// Being in components.js means "this is a component", and the runtime loads
|
|
874
|
+
// one with `const { default: myClass } = await import(...)` (Slice.getClass).
|
|
875
|
+
// Without a default export that entry can never resolve, so it is a broken
|
|
876
|
+
// registration, not a style issue.
|
|
877
|
+
//
|
|
878
|
+
// components.js is auto-generated by scanning directories, so a plain module
|
|
879
|
+
// parked at <Category>/<Name>/<Name>.js gets registered by accident. The fix
|
|
880
|
+
// is to move it out of the components tree — it is a module, not a component.
|
|
881
|
+
if (hasDefaultExport(source) === false) {
|
|
882
|
+
sourceErrors.push({
|
|
883
|
+
component: componentName,
|
|
884
|
+
message: `${componentFile}: no default export, but it is registered as a component. `
|
|
885
|
+
+ `Slice loads components with \`const { default: myClass } = await import(...)\`, so `
|
|
886
|
+
+ `slice.build('${componentName}') can never resolve. Add a default export, or — if this `
|
|
887
|
+
+ 'is a plain module of constants/helpers — move it out of the components tree so it '
|
|
888
|
+
+ 'stops being auto-registered.'
|
|
889
|
+
});
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
const unresolved = findUnresolvedRelativeImports(source, path.dirname(componentFile));
|
|
893
|
+
if (unresolved.length > 0) {
|
|
894
|
+
sourceErrors.push({
|
|
895
|
+
component: componentName,
|
|
896
|
+
message: `${componentFile}: cannot resolve ${unresolved.map((s2) => `"${s2}"`).join(', ')}. `
|
|
897
|
+
+ 'An unresolved relative import silently becomes `undefined` at runtime.'
|
|
898
|
+
});
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
// A warning, not an error: components legitimately build OPTIONAL ones. The
|
|
902
|
+
// framework's own Button does `if (this.icon) slice.build('Icon', ...)`, and
|
|
903
|
+
// a project that never passes an icon has no reason to install Icon. So an
|
|
904
|
+
// unregistered name is a likely typo, not a certain one.
|
|
905
|
+
const unknownBuilds = findUnknownComponentBuilds(source, registeredNames);
|
|
906
|
+
if (unknownBuilds.length > 0) {
|
|
907
|
+
sourceWarnings.push({
|
|
908
|
+
component: componentName,
|
|
909
|
+
message: `builds ${unknownBuilds.map((n) => `"${n}"`).join(', ')}, not registered in `
|
|
910
|
+
+ 'components.js. If that is a typo it will silently render nothing; if the component '
|
|
911
|
+
+ 'is optional, ignore this.'
|
|
912
|
+
});
|
|
913
|
+
}
|
|
914
|
+
|
|
806
915
|
const props = extractStaticPropsFromSource(source, componentFile);
|
|
807
916
|
if (!props) continue;
|
|
808
917
|
checkedCount++;
|
|
@@ -825,7 +934,7 @@ const validateComponentProps = async ({ projectRoot }) => {
|
|
|
825
934
|
}
|
|
826
935
|
}
|
|
827
936
|
|
|
828
|
-
return { errors, warnings, checkedCount };
|
|
937
|
+
return { errors, warnings, sourceErrors, sourceWarnings, checkedCount };
|
|
829
938
|
};
|
|
830
939
|
|
|
831
940
|
const generateTypesFile = async ({ projectRoot, outputPath }) => {
|