@storybook/svelte 11.0.0-alpha.8 → 11.0.0-beta.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/dist/_browser-chunks/{chunk-QEV3A2KO.js → chunk-FPEMDWKX.js} +1 -1
- package/dist/_browser-chunks/{chunk-JERPECF7.js → chunk-SLNVXRXZ.js} +1 -1
- package/dist/_node-chunks/build-docgen-MSJ3SPME.js +481 -0
- package/dist/_node-chunks/chunk-CWKCHCWY.js +213 -0
- package/dist/_node-chunks/chunk-EHBEHVES.js +401 -0
- package/dist/_node-chunks/chunk-MHATCRYC.js +1062 -0
- package/dist/_node-chunks/{walk-UNPFNDYR.js → chunk-OZSDLXII.js} +7 -6
- package/dist/_node-chunks/chunk-S5NTL526.js +21 -0
- package/dist/_node-chunks/chunk-X4C3MPED.js +135 -0
- package/dist/_node-chunks/chunk-XTPACDWA.js +130 -0
- package/dist/_node-chunks/{parsers-B3TUDVKM.js → parsers-2O2L5JOB.js} +6 -6
- package/dist/_node-chunks/svelte-project-manager-U5Z7RBKO.js +118 -0
- package/dist/_node-chunks/walk-OTIE5SYQ.js +17 -0
- package/dist/{chunk-C7Fmzjt_.d.ts → chunk-Cepksr9x.d.ts} +43 -38
- package/dist/docgen/component-docgen/svelte2tsx/generate-docgen.d.ts +100 -0
- package/dist/docgen/component-docgen/svelte2tsx/generate-docgen.js +33 -0
- package/dist/docgen/docgen-worker.js +27 -13
- package/dist/entry-preview-docs.js +1 -1
- package/dist/entry-preview.js +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -2
- package/dist/preset.js +798 -28
- package/dist/svelte-csf/addon-check.js +6 -6
- package/dist/svelte-csf/compiler/transform.d.ts +0 -27
- package/dist/svelte-csf/compiler/transform.js +174 -306
- package/dist/svelte-csf/indexer/index.js +13 -180
- package/dist/svelte-csf/runtime/component-helpers.d.ts +1 -12
- package/dist/svelte-csf/runtime/component-helpers.js +1 -0
- package/dist/svelte-csf/runtime/create-runtime-stories.d.ts +1 -13
- package/dist/svelte-csf/runtime/create-runtime-stories.js +28 -6
- package/package.json +9 -5
- package/static/svelte-csf/Story.svelte +23 -14
- package/static/svelte-csf/StoryRenderer.svelte +18 -4
- package/static/svelte-csf/contexts/renderer.svelte.js +23 -5
- package/dist/_node-chunks/chunk-5AP4EINI.js +0 -18
- package/dist/_node-chunks/chunk-H7NN6AMU.js +0 -1024
|
@@ -171,7 +171,7 @@ function unWrap(obj) {
|
|
|
171
171
|
}
|
|
172
172
|
function prepareStory(context, rawStory, rawInnerStory) {
|
|
173
173
|
let story = unWrap(rawStory), innerStory = rawInnerStory && unWrap(rawInnerStory), preparedStory;
|
|
174
|
-
return !story || Object.keys(story).length === 0 ? preparedStory = {
|
|
174
|
+
return !story || typeof story == "object" && Object.keys(story).length === 0 ? preparedStory = {
|
|
175
175
|
Component: context.component
|
|
176
176
|
} : story.Component ? preparedStory = story : preparedStory = {
|
|
177
177
|
Component: story
|
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
import CJS_COMPAT_NODE_URL_t3g1exktcfr from 'node:url';
|
|
2
|
+
import CJS_COMPAT_NODE_PATH_t3g1exktcfr from 'node:path';
|
|
3
|
+
import CJS_COMPAT_NODE_MODULE_t3g1exktcfr from "node:module";
|
|
4
|
+
|
|
5
|
+
var __filename = CJS_COMPAT_NODE_URL_t3g1exktcfr.fileURLToPath(import.meta.url);
|
|
6
|
+
var __dirname = CJS_COMPAT_NODE_PATH_t3g1exktcfr.dirname(__filename);
|
|
7
|
+
var require = CJS_COMPAT_NODE_MODULE_t3g1exktcfr.createRequire(import.meta.url);
|
|
8
|
+
|
|
9
|
+
// ------------------------------------------------------------
|
|
10
|
+
// end of CJS compatibility banner, injected by Storybook's esbuild configuration
|
|
11
|
+
// ------------------------------------------------------------
|
|
12
|
+
import {
|
|
13
|
+
UNPRINTABLE_DEFAULT
|
|
14
|
+
} from "./chunk-EHBEHVES.js";
|
|
15
|
+
import {
|
|
16
|
+
parseSvelteSource
|
|
17
|
+
} from "./chunk-CWKCHCWY.js";
|
|
18
|
+
import {
|
|
19
|
+
extractSvelteASTNodes,
|
|
20
|
+
getDefineMetaComponentValue,
|
|
21
|
+
getDefineMetaFirstArgumentObjectExpression
|
|
22
|
+
} from "./chunk-X4C3MPED.js";
|
|
23
|
+
import "./chunk-MHATCRYC.js";
|
|
24
|
+
|
|
25
|
+
// src/docgen/component-docgen/build-docgen.ts
|
|
26
|
+
import { realpathSync } from "node:fs";
|
|
27
|
+
import { readFile as readFile2 } from "node:fs/promises";
|
|
28
|
+
import { dirname, isAbsolute, join, relative, resolve as resolve2 } from "node:path";
|
|
29
|
+
import { preprocess } from "svelte/compiler";
|
|
30
|
+
import { getComponentIdFromEntry, getStoryImportPathFromEntry as getStoryImportPathFromEntry2 } from "storybook/internal/common";
|
|
31
|
+
import { extractComponentDescription } from "storybook/internal/csf-tools";
|
|
32
|
+
|
|
33
|
+
// src/docgen/component-docgen/api-description/api-description.ts
|
|
34
|
+
import ts from "typescript";
|
|
35
|
+
var IDENTIFIER = /^[A-Za-z_$][\w$]*$/;
|
|
36
|
+
function buildApiDescription(name, docgen) {
|
|
37
|
+
let { props, events = [], slots = [], omittedTypes = [] } = docgen;
|
|
38
|
+
if (!props.length && !omittedTypes.length && !events.length && !slots.length)
|
|
39
|
+
return;
|
|
40
|
+
let sanitizedName = name.replace(/\W+/g, ""), typePrefix = IDENTIFIER.test(sanitizedName) ? sanitizedName : `Component${sanitizedName}`, parts = [];
|
|
41
|
+
return (props.length > 0 || omittedTypes.length > 0) && parts.push(
|
|
42
|
+
...section(
|
|
43
|
+
"Props",
|
|
44
|
+
typePrefix,
|
|
45
|
+
docgen.typeParams,
|
|
46
|
+
props.flatMap((prop) => propLines(prop)),
|
|
47
|
+
// The props of `HTMLInputAttributes` and the like are too many to list one by one.
|
|
48
|
+
{ extendsTypes: omittedTypes }
|
|
49
|
+
)
|
|
50
|
+
), events.length > 0 && parts.push(
|
|
51
|
+
...section(
|
|
52
|
+
"Events",
|
|
53
|
+
typePrefix,
|
|
54
|
+
docgen.typeParams,
|
|
55
|
+
events.flatMap((event) => [
|
|
56
|
+
...docComment(event),
|
|
57
|
+
`${memberKey(event.name)}: ${event.typeText ?? "CustomEvent"}; // on:${event.name}`
|
|
58
|
+
])
|
|
59
|
+
)
|
|
60
|
+
), slots.length > 0 && parts.push(
|
|
61
|
+
...section(
|
|
62
|
+
"Slots",
|
|
63
|
+
typePrefix,
|
|
64
|
+
docgen.typeParams,
|
|
65
|
+
slots.flatMap((slot) => [
|
|
66
|
+
...docComment(slot),
|
|
67
|
+
`${memberKey(slot.name)}: ${slot.typeText ?? "{}"};`
|
|
68
|
+
]),
|
|
69
|
+
{ intro: "Each slot is typed with the props it passes to its content." }
|
|
70
|
+
)
|
|
71
|
+
), parts.join(`
|
|
72
|
+
`).trim();
|
|
73
|
+
}
|
|
74
|
+
function memberKey(name) {
|
|
75
|
+
return IDENTIFIER.test(name) ? name : JSON.stringify(name);
|
|
76
|
+
}
|
|
77
|
+
function propType(prop) {
|
|
78
|
+
let text = prop.typeText ?? "unknown";
|
|
79
|
+
return prop.optional ? unwrapParens(text.replace(/ \| undefined$/, "")) : text;
|
|
80
|
+
}
|
|
81
|
+
function unwrapParens(text) {
|
|
82
|
+
if (!text.startsWith("(") || !text.endsWith(")"))
|
|
83
|
+
return text;
|
|
84
|
+
let depth = 0;
|
|
85
|
+
for (let index = 0; index < text.length - 1; index++)
|
|
86
|
+
if (depth += text[index] === "(" ? 1 : text[index] === ")" ? -1 : 0, depth === 0)
|
|
87
|
+
return text;
|
|
88
|
+
return text.slice(1, -1);
|
|
89
|
+
}
|
|
90
|
+
function propLines(prop) {
|
|
91
|
+
let marker = prop.jsDocTags?.some((tag) => tag.name === "bindable") ? ` // bind:${prop.name}={...}` : "", extracted = prop.defaultValue?.text, defaultValue = extracted === void 0 ? prop.jsDocTags?.find((tag) => tag.name === "default")?.text : extracted === UNPRINTABLE_DEFAULT ? void 0 : withoutComments(extracted), defaultText = defaultValue ? ` = ${defaultValue.replace(/\s*\n\s*/g, " ")}` : "";
|
|
92
|
+
return [
|
|
93
|
+
...docComment(prop),
|
|
94
|
+
`${memberKey(prop.name)}${prop.optional ? "?" : ""}: ${propType(prop)}${defaultText};${marker}`
|
|
95
|
+
];
|
|
96
|
+
}
|
|
97
|
+
function withoutComments(code) {
|
|
98
|
+
if (!code.includes("//"))
|
|
99
|
+
return code;
|
|
100
|
+
let file = ts.createSourceFile("default.ts", `(${code})`, ts.ScriptTarget.Latest, !0), [statement] = file.statements;
|
|
101
|
+
return file.statements.length !== 1 || !ts.isExpressionStatement(statement) || !ts.isParenthesizedExpression(statement.expression) ? code : ts.createPrinter({ removeComments: !0 }).printNode(ts.EmitHint.Expression, statement.expression.expression, file);
|
|
102
|
+
}
|
|
103
|
+
function docComment(member) {
|
|
104
|
+
let description = member.description?.trim() ?? "", tagLines = (member.jsDocTags ?? []).filter(
|
|
105
|
+
(tag) => !["type", "default", "bindable"].includes(tag.name)
|
|
106
|
+
).map((tag) => `@${tag.name}${tag.text ? ` ${tag.text}` : ""}`), tagBody = tagLines.flatMap((tag) => tag.split(`
|
|
107
|
+
`));
|
|
108
|
+
return !description && tagLines.length === 0 ? [] : !description && tagBody.length === 1 ? [`/** ${tagLines[0]} */`] : !description.includes(`
|
|
109
|
+
`) && tagLines.length === 0 ? [`/** ${description} */`] : [
|
|
110
|
+
"/**",
|
|
111
|
+
...description ? description.split(`
|
|
112
|
+
`).map(commentLine) : [],
|
|
113
|
+
...tagLines.length ? [...description ? [" *"] : [], ...tagBody.map(commentLine)] : [],
|
|
114
|
+
" */"
|
|
115
|
+
];
|
|
116
|
+
}
|
|
117
|
+
function commentLine(line) {
|
|
118
|
+
return line.trim() ? ` * ${line}` : " *";
|
|
119
|
+
}
|
|
120
|
+
function section(kind, typePrefix, typeParams, lines, { intro, extendsTypes = [] } = {}) {
|
|
121
|
+
let alias = `export type ${typePrefix}${kind}${typeParams ? `<${typeParams}>` : ""} =`, extendsText = extendsTypes.join(" & ");
|
|
122
|
+
return [
|
|
123
|
+
`## ${kind}`,
|
|
124
|
+
"",
|
|
125
|
+
...intro ? [intro, ""] : [],
|
|
126
|
+
"```",
|
|
127
|
+
...lines.length ? [
|
|
128
|
+
`${alias} {`,
|
|
129
|
+
...lines.map((line) => ` ${line}`),
|
|
130
|
+
extendsText ? `} & ${extendsText}` : "}"
|
|
131
|
+
] : [`${alias} ${extendsText}`],
|
|
132
|
+
"```",
|
|
133
|
+
""
|
|
134
|
+
];
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// src/docgen/component-docgen/arg-types/create-server-arg-types.ts
|
|
138
|
+
function summary(type) {
|
|
139
|
+
if (type)
|
|
140
|
+
return "text" in type ? type.text : "types" in type ? type.types.filter((t) => t.type !== "null").map(summary).join(type.type === "union" ? " | " : " & ") : type.type;
|
|
141
|
+
}
|
|
142
|
+
function storybookType(type) {
|
|
143
|
+
if (!type)
|
|
144
|
+
return { name: "other", value: "unknown" };
|
|
145
|
+
switch (type.type) {
|
|
146
|
+
case "union":
|
|
147
|
+
case "intersection": {
|
|
148
|
+
let members = type.types.filter((member) => member.type !== "null");
|
|
149
|
+
return members.length === 1 ? storybookType(members[0]) : { name: "other", value: summary(type) ?? "unknown" };
|
|
150
|
+
}
|
|
151
|
+
case "array":
|
|
152
|
+
case "object":
|
|
153
|
+
return { name: "other", value: type.text === "{}" ? "object" : type.text };
|
|
154
|
+
case "function":
|
|
155
|
+
return { name: "function", raw: type.text };
|
|
156
|
+
case "literal":
|
|
157
|
+
return { name: "literal", value: type.value };
|
|
158
|
+
case "any":
|
|
159
|
+
return { name: "other", value: "any" };
|
|
160
|
+
case "null":
|
|
161
|
+
case "undefined":
|
|
162
|
+
case "void":
|
|
163
|
+
return { name: "other", value: type.type };
|
|
164
|
+
default:
|
|
165
|
+
return { name: type.type };
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
function withoutNullish(type) {
|
|
169
|
+
if (type?.type !== "union") return type;
|
|
170
|
+
let members = type.types.filter(
|
|
171
|
+
(member) => member.type !== "null" && member.type !== "undefined"
|
|
172
|
+
);
|
|
173
|
+
return members.length === 1 ? members[0] : { ...type, types: members };
|
|
174
|
+
}
|
|
175
|
+
function createServerArgTypes(docgen) {
|
|
176
|
+
let argTypes = {};
|
|
177
|
+
for (let prop of docgen.props) {
|
|
178
|
+
let typeSummary = summary(prop.type), type = withoutNullish(prop.type), slot = /^Snippet(?:<|$)/.test(prop.typeText ?? ""), callable = type?.type === "function" || type?.type === "union" && type.types.some((member) => member.type === "function"), literals = type?.type === "union" ? type.types : [], values = literals.flatMap(
|
|
179
|
+
(type2) => type2.type === "literal" && typeof type2.value != "boolean" ? [type2.value] : []
|
|
180
|
+
), enumValues = values.length > 0 && values.length === literals.length ? values : void 0, jsDocTags = {};
|
|
181
|
+
for (let tag of prop.jsDocTags ?? [])
|
|
182
|
+
(jsDocTags[tag.name] ??= []).push(tag.text);
|
|
183
|
+
let defaultValue = prop.defaultValue?.text ?? jsDocTags.default?.[0];
|
|
184
|
+
argTypes[prop.name] = {
|
|
185
|
+
name: prop.name,
|
|
186
|
+
description: prop.description,
|
|
187
|
+
type: {
|
|
188
|
+
...enumValues ? { name: "enum", value: enumValues } : slot ? { name: "other", value: typeSummary ?? "unknown" } : storybookType(prop.type),
|
|
189
|
+
required: prop.optional === !1 && defaultValue === void 0
|
|
190
|
+
},
|
|
191
|
+
// An enum has no control here, so inferControls picks radio or select as for every renderer.
|
|
192
|
+
...slot || callable ? { control: !1 } : enumValues ? {} : {
|
|
193
|
+
control: {
|
|
194
|
+
type: type?.type === "string" ? "text" : type?.type === "number" ? "number" : type?.type === "boolean" ? "boolean" : "object"
|
|
195
|
+
}
|
|
196
|
+
},
|
|
197
|
+
...enumValues ? { options: enumValues } : {},
|
|
198
|
+
table: {
|
|
199
|
+
type: { summary: typeSummary },
|
|
200
|
+
defaultValue: { summary: defaultValue },
|
|
201
|
+
category: slot ? "slots" : callable && /^on[A-Z]|^on[a-z]+$/.test(prop.name) ? "events" : "properties",
|
|
202
|
+
...Object.keys(jsDocTags).length > 0 ? { jsDocTags } : {}
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
for (let [category, members] of [
|
|
207
|
+
["events", docgen.events],
|
|
208
|
+
["slots", docgen.slots]
|
|
209
|
+
])
|
|
210
|
+
for (let member of members ?? [])
|
|
211
|
+
argTypes[`${category === "events" ? "event" : "slot"}_${member.name}`] = {
|
|
212
|
+
name: member.name,
|
|
213
|
+
description: member.description,
|
|
214
|
+
control: !1,
|
|
215
|
+
...category === "events" ? { action: member.name } : {},
|
|
216
|
+
table: { category, type: { summary: member.typeText } }
|
|
217
|
+
};
|
|
218
|
+
return argTypes;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// src/docgen/component-docgen/resolve-component/resolve-component.ts
|
|
222
|
+
import { readFileSync } from "node:fs";
|
|
223
|
+
import { readFile } from "node:fs/promises";
|
|
224
|
+
import { resolve } from "node:path";
|
|
225
|
+
import {
|
|
226
|
+
createMetaComponentResolver,
|
|
227
|
+
createModuleResolver,
|
|
228
|
+
getStoryImportPathFromEntry,
|
|
229
|
+
jsTsSourceExtensions
|
|
230
|
+
} from "storybook/internal/common";
|
|
231
|
+
import {
|
|
232
|
+
SourceEditor,
|
|
233
|
+
collectImportBindings,
|
|
234
|
+
loadCsf,
|
|
235
|
+
metaObject,
|
|
236
|
+
parseExpression,
|
|
237
|
+
parseModule,
|
|
238
|
+
propertyValue,
|
|
239
|
+
resolveReferencedValue,
|
|
240
|
+
staticKey,
|
|
241
|
+
unwrapExpression
|
|
242
|
+
} from "storybook/internal/csf-tools";
|
|
243
|
+
var resolveMetaComponent = createMetaComponentResolver({ extensions: [".svelte"] }), moduleResolver = createModuleResolver({
|
|
244
|
+
extensions: [".svelte", ...jsTsSourceExtensions],
|
|
245
|
+
// TypeScript projects import `./index.ts` as `./index.js`, as SvelteKit's packaging requires.
|
|
246
|
+
extensionAlias: { ".js": [".ts", ".js"] },
|
|
247
|
+
mainFields: ["svelte", "module", "main"],
|
|
248
|
+
conditionNames: ["svelte", "import", "module", "default"],
|
|
249
|
+
tsconfig: "auto"
|
|
250
|
+
});
|
|
251
|
+
function resolveImport(fromFile, specifier) {
|
|
252
|
+
try {
|
|
253
|
+
return moduleResolver.resolveFileSync(fromFile, specifier);
|
|
254
|
+
} catch {
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
function importedName(specifier) {
|
|
259
|
+
return specifier.type === "ImportDefaultSpecifier" ? "default" : specifier.type === "ImportNamespaceSpecifier" ? "*" : specifier.imported?.type === "Identifier" ? String(specifier.imported.name) : String(specifier.imported?.value);
|
|
260
|
+
}
|
|
261
|
+
function exportedName(node) {
|
|
262
|
+
return node.type === "Identifier" ? node.name : String(node.value);
|
|
263
|
+
}
|
|
264
|
+
function followExport(filePath, exportName, seen = /* @__PURE__ */ new Set()) {
|
|
265
|
+
if (filePath.endsWith(".svelte") && exportName === "default") return filePath;
|
|
266
|
+
let key = `${filePath}#${exportName}`;
|
|
267
|
+
if (seen.has(key)) return;
|
|
268
|
+
seen.add(key);
|
|
269
|
+
let program = moduleProgram(filePath);
|
|
270
|
+
if (!program) return;
|
|
271
|
+
let follow = (specifier, name) => {
|
|
272
|
+
let target = resolveImport(filePath, specifier);
|
|
273
|
+
return target ? followExport(target, name, seen) : void 0;
|
|
274
|
+
}, followLocal = (localName) => {
|
|
275
|
+
for (let declaration of program.body) {
|
|
276
|
+
if (declaration.type !== "ImportDeclaration" || declaration.importKind === "type") continue;
|
|
277
|
+
let imported = declaration.specifiers.find(
|
|
278
|
+
(candidate) => candidate.local.name === localName
|
|
279
|
+
);
|
|
280
|
+
if (imported) return follow(declaration.source.value, importedName(imported));
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
for (let statement of program.body) {
|
|
284
|
+
if (statement.type === "ExportDefaultDeclaration" && exportName === "default" && statement.declaration.type === "Identifier")
|
|
285
|
+
return followLocal(statement.declaration.name);
|
|
286
|
+
if (statement.type === "ExportAllDeclaration" && !statement.exported) {
|
|
287
|
+
let found = exportName !== "default" && statement.exportKind !== "type" && follow(statement.source.value, exportName);
|
|
288
|
+
if (found) return found;
|
|
289
|
+
}
|
|
290
|
+
if (statement.type !== "ExportNamedDeclaration" || statement.exportKind === "type") continue;
|
|
291
|
+
let specifier = statement.specifiers.find(
|
|
292
|
+
(candidate) => exportedName(candidate.exported) === exportName
|
|
293
|
+
);
|
|
294
|
+
if (!specifier || specifier.exportKind === "type") continue;
|
|
295
|
+
let localName = exportedName(specifier.local);
|
|
296
|
+
return statement.source ? follow(statement.source.value, localName) : followLocal(localName);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
function moduleEditor(filePath) {
|
|
300
|
+
try {
|
|
301
|
+
let file = readFileSync(filePath, "utf8");
|
|
302
|
+
if (!filePath.endsWith(".svelte")) return new SourceEditor(file, filePath);
|
|
303
|
+
let { source, ast } = parseSvelteSource(file), module = ast.module?.content;
|
|
304
|
+
return module && new SourceEditor(source.slice(module.start, module.end), `${filePath}.ts`);
|
|
305
|
+
} catch {
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
function moduleProgram(filePath) {
|
|
310
|
+
return moduleEditor(filePath)?.program;
|
|
311
|
+
}
|
|
312
|
+
function locate(storyPath, specifier, exportName, name) {
|
|
313
|
+
let filePath = resolveImport(storyPath, specifier), componentPath = filePath && followExport(filePath, exportName);
|
|
314
|
+
if (!componentPath)
|
|
315
|
+
throw new Error(`Could not resolve \`${specifier}\` export \`${exportName}\``);
|
|
316
|
+
return { componentPath, exportName, name };
|
|
317
|
+
}
|
|
318
|
+
async function resolveStoryComponent(entry) {
|
|
319
|
+
let importPath = getStoryImportPathFromEntry(entry);
|
|
320
|
+
if (!importPath) return;
|
|
321
|
+
let storyPath = resolve(importPath), file = await readFile(storyPath, "utf8");
|
|
322
|
+
if (storyPath.endsWith(".svelte")) {
|
|
323
|
+
let { source, ast } = parseSvelteSource(file), nodes = await extractSvelteASTNodes({ ast, filename: storyPath }), component = getDefineMetaComponentValue({ nodes, filename: storyPath });
|
|
324
|
+
if (!component) return;
|
|
325
|
+
for (let statement of ast.module?.content.body ?? []) {
|
|
326
|
+
if (statement.type !== "ImportDeclaration") continue;
|
|
327
|
+
let localName2 = component.type === "MemberExpression" ? component.object.name : component.name, specifier = statement.specifiers.find(
|
|
328
|
+
(candidate) => candidate.local.name === localName2
|
|
329
|
+
);
|
|
330
|
+
if (!specifier) continue;
|
|
331
|
+
if (component.type !== "MemberExpression")
|
|
332
|
+
return locate(
|
|
333
|
+
storyPath,
|
|
334
|
+
String(statement.source.value),
|
|
335
|
+
importedName(specifier),
|
|
336
|
+
component.name
|
|
337
|
+
);
|
|
338
|
+
if (specifier.type === "ImportNamespaceSpecifier")
|
|
339
|
+
return locate(
|
|
340
|
+
storyPath,
|
|
341
|
+
String(statement.source.value),
|
|
342
|
+
component.property.name,
|
|
343
|
+
component.property.name
|
|
344
|
+
);
|
|
345
|
+
let module = ast.module?.content;
|
|
346
|
+
if (!module) return;
|
|
347
|
+
let parsed = /* @__PURE__ */ new Map(), referenced = resolveReferencedValue(
|
|
348
|
+
{
|
|
349
|
+
editor: new SourceEditor(source.slice(module.start, module.end), `${storyPath}.ts`),
|
|
350
|
+
filePath: storyPath,
|
|
351
|
+
resolveModule: (fromFile, specifier2) => {
|
|
352
|
+
let target = resolveImport(fromFile, specifier2);
|
|
353
|
+
if (target) {
|
|
354
|
+
if (!parsed.has(target)) {
|
|
355
|
+
let editor = moduleEditor(target);
|
|
356
|
+
parsed.set(target, editor && { editor, filePath: target });
|
|
357
|
+
}
|
|
358
|
+
return parsed.get(target);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
},
|
|
362
|
+
component
|
|
363
|
+
);
|
|
364
|
+
if (!referenced) return;
|
|
365
|
+
let value = unwrapExpression(referenced.node);
|
|
366
|
+
if (value.type !== "Identifier") return;
|
|
367
|
+
let binding = collectImportBindings(referenced.ctx.editor.program).get(value.name);
|
|
368
|
+
return binding ? locate(
|
|
369
|
+
referenced.ctx.filePath,
|
|
370
|
+
binding.importId,
|
|
371
|
+
binding.importName,
|
|
372
|
+
component.property.name
|
|
373
|
+
) : void 0;
|
|
374
|
+
}
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
let resolved = resolveMetaComponent(
|
|
378
|
+
loadCsf(file, { makeTitle: () => entry.title }).parse(),
|
|
379
|
+
storyPath
|
|
380
|
+
);
|
|
381
|
+
if ("reason" in resolved) {
|
|
382
|
+
if (resolved.reason === "unreadable-component-expression")
|
|
383
|
+
throw new Error(`Cannot resolve the component \`${resolved.expression}\``);
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
let { importId, exportName, localName } = resolved.component;
|
|
387
|
+
return importId ? locate(storyPath, importId, exportName, localName) : void 0;
|
|
388
|
+
}
|
|
389
|
+
async function resolveStorySubcomponents(entry) {
|
|
390
|
+
let importPath = getStoryImportPathFromEntry(entry);
|
|
391
|
+
if (!importPath) return {};
|
|
392
|
+
let storyPath = resolve(importPath), file = await readFile(storyPath, "utf8"), { meta, program } = storyPath.endsWith(".svelte") ? await readSvelteMeta(file, storyPath) : readCsfMeta(file, entry), subcomponents = propertyValue(meta, "subcomponents");
|
|
393
|
+
if (subcomponents?.type !== "ObjectExpression") return {};
|
|
394
|
+
let bindings = collectImportBindings(program), resolved = {};
|
|
395
|
+
for (let property of subcomponents.properties) {
|
|
396
|
+
let name = property.type === "Property" ? staticKey(property) : void 0, value = property.type === "Property" ? property.value : void 0, binding = value?.type === "Identifier" ? bindings.get(value.name) : void 0;
|
|
397
|
+
if (!(!name || !binding))
|
|
398
|
+
try {
|
|
399
|
+
resolved[name] = locate(storyPath, binding.importId, binding.importName, name);
|
|
400
|
+
} catch {
|
|
401
|
+
continue;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
return resolved;
|
|
405
|
+
}
|
|
406
|
+
function readCsfMeta(source, entry) {
|
|
407
|
+
let csf = loadCsf(source, { makeTitle: () => entry.title }).parse();
|
|
408
|
+
return { meta: metaObject(csf), program: csf.program };
|
|
409
|
+
}
|
|
410
|
+
async function readSvelteMeta(file, storyPath) {
|
|
411
|
+
let { source, ast } = parseSvelteSource(file), nodes = await extractSvelteASTNodes({ ast, filename: storyPath }), { start, end } = getDefineMetaFirstArgumentObjectExpression({
|
|
412
|
+
nodes,
|
|
413
|
+
filename: storyPath
|
|
414
|
+
}), module = ast.module.content, meta = parseExpression(source.slice(start, end));
|
|
415
|
+
return {
|
|
416
|
+
meta: meta.type === "ObjectExpression" ? meta : void 0,
|
|
417
|
+
program: parseModule(source.slice(module.start, module.end), `${storyPath}.ts`).program
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
// src/docgen/component-docgen/build-docgen.ts
|
|
422
|
+
function pathFromStory(storyPath, componentPath) {
|
|
423
|
+
let fromStory = relative(dirname(realpathSync(resolve2(storyPath))), componentPath), path = join(dirname(storyPath), fromStory).replaceAll("\\", "/");
|
|
424
|
+
return isAbsolute(path) || path.startsWith("../") ? path : `./${path}`;
|
|
425
|
+
}
|
|
426
|
+
async function buildDocgenPayload(input, context) {
|
|
427
|
+
let path = getStoryImportPathFromEntry2(input.entry);
|
|
428
|
+
if (!path) return;
|
|
429
|
+
let base = {
|
|
430
|
+
id: getComponentIdFromEntry(input.entry),
|
|
431
|
+
name: input.entry.title.split("/").at(-1) ?? input.entry.title,
|
|
432
|
+
path,
|
|
433
|
+
jsDocTags: {},
|
|
434
|
+
renderer: "svelte"
|
|
435
|
+
};
|
|
436
|
+
try {
|
|
437
|
+
let component = await resolveStoryComponent(input.entry);
|
|
438
|
+
if (!component) return;
|
|
439
|
+
let docs = await componentDocs(component.componentPath, component.name, context), subcomponents = {};
|
|
440
|
+
for (let [name, subcomponent] of Object.entries(
|
|
441
|
+
await resolveStorySubcomponents(input.entry)
|
|
442
|
+
))
|
|
443
|
+
try {
|
|
444
|
+
subcomponents[name] = {
|
|
445
|
+
name,
|
|
446
|
+
path: pathFromStory(path, subcomponent.componentPath),
|
|
447
|
+
...await componentDocs(subcomponent.componentPath, name, context)
|
|
448
|
+
};
|
|
449
|
+
} catch {
|
|
450
|
+
continue;
|
|
451
|
+
}
|
|
452
|
+
return {
|
|
453
|
+
...base,
|
|
454
|
+
name: component.name,
|
|
455
|
+
...docs,
|
|
456
|
+
...Object.keys(subcomponents).length ? { subcomponents } : {}
|
|
457
|
+
};
|
|
458
|
+
} catch (error) {
|
|
459
|
+
return {
|
|
460
|
+
...base,
|
|
461
|
+
error: {
|
|
462
|
+
name: "Svelte docgen failed",
|
|
463
|
+
message: error instanceof Error ? error.message : String(error)
|
|
464
|
+
}
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
async function componentDocs(componentPath, name, context) {
|
|
469
|
+
let source = await readFile2(componentPath, "utf8"), documentationSource = await preprocess(source, { style: () => ({ code: "" }) }), docgen = context.componentMetaManager.generateDocgen(componentPath, {
|
|
470
|
+
server: !0,
|
|
471
|
+
documentationSource: documentationSource.code
|
|
472
|
+
});
|
|
473
|
+
return {
|
|
474
|
+
...extractComponentDescription(void 0, docgen.description, docgen.jsDocTags),
|
|
475
|
+
argTypes: createServerArgTypes(docgen),
|
|
476
|
+
apiDescription: buildApiDescription(name, docgen)
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
export {
|
|
480
|
+
buildDocgenPayload
|
|
481
|
+
};
|