sveld 0.35.0 → 0.35.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.
- package/README.md +46 -0
- package/lib/ComponentParser.d.ts +28 -0
- package/lib/ast-guards.d.ts +4 -0
- package/lib/browser.d.ts +39 -0
- package/lib/browser.js +290 -0
- package/lib/bundle.d.ts +8 -1
- package/lib/index.js +231 -372
- package/lib/parse-exports.d.ts +5 -1
- package/lib/parser/context.d.ts +9 -5
- package/lib/parser/props.d.ts +6 -0
- package/lib/parser/runes-detection.d.ts +16 -0
- package/lib/parser/scopes.d.ts +23 -5
- package/lib/parser/slots.d.ts +1 -1
- package/lib/parser/type-resolution.d.ts +5 -0
- package/lib/parser/typescript-casts.d.ts +2 -0
- package/lib/resolve-types.d.ts +1 -1
- package/package.json +6 -1
package/README.md
CHANGED
|
@@ -132,6 +132,7 @@ export default class Button extends SvelteComponentTyped<
|
|
|
132
132
|
- [CLI](#cli)
|
|
133
133
|
- [CI: API-drift checks (`--check`)](#ci-api-drift-checks---check)
|
|
134
134
|
- [Node.js](#nodejs)
|
|
135
|
+
- [Browser](#browser)
|
|
135
136
|
- [Config File](#config-file)
|
|
136
137
|
- [Publishing to NPM](#publishing-to-npm)
|
|
137
138
|
- [Available Options](#available-options)
|
|
@@ -568,6 +569,51 @@ sveld({
|
|
|
568
569
|
});
|
|
569
570
|
```
|
|
570
571
|
|
|
572
|
+
### Browser
|
|
573
|
+
|
|
574
|
+
`sveld/browser` is a Node-free subpath export for running `sveld` client-side — e.g. an in-browser Svelte playground or REPL that parses whatever `.svelte` source the user typed and renders docs for it live. It bundles with Vite, esbuild, webpack, or Rollup without a `node:fs`/`node:path` polyfill.
|
|
575
|
+
|
|
576
|
+
It covers parsing one component's source and rendering that result to any output format `sveld` supports (JSON, Markdown, `.d.ts`, Custom Elements Manifest). It does not cover project-wide glob scanning, the config file, or the CLI/Vite plugin (`sveld()`/`pluginSveld()`) — those walk the filesystem and only make sense in Node. Use the main `sveld` entry point for those.
|
|
577
|
+
|
|
578
|
+
```ts
|
|
579
|
+
import {
|
|
580
|
+
asNormalizedPath,
|
|
581
|
+
ComponentParser,
|
|
582
|
+
buildComponentApiDocument,
|
|
583
|
+
writeMarkdownCore,
|
|
584
|
+
writeTsDefinition,
|
|
585
|
+
buildCustomElementsManifest,
|
|
586
|
+
} from "sveld/browser";
|
|
587
|
+
|
|
588
|
+
const parser = new ComponentParser();
|
|
589
|
+
const moduleName = "Button";
|
|
590
|
+
const filePath = "Button.svelte";
|
|
591
|
+
const parsed = parser.parseSvelteComponent(source, { moduleName, filePath });
|
|
592
|
+
|
|
593
|
+
// `parseSvelteComponent` returns component metadata only; add `moduleName`
|
|
594
|
+
// and `filePath` yourself to match the `ComponentDocApi` shape the writers expect.
|
|
595
|
+
const component = { ...parsed, moduleName, filePath: asNormalizedPath(filePath) };
|
|
596
|
+
const components = new Map([[moduleName, component]]);
|
|
597
|
+
|
|
598
|
+
// JSON
|
|
599
|
+
const jsonDoc = buildComponentApiDocument(components);
|
|
600
|
+
|
|
601
|
+
// Markdown
|
|
602
|
+
const markdown = writeMarkdownCore(components);
|
|
603
|
+
|
|
604
|
+
// TypeScript definitions (per component)
|
|
605
|
+
const dts = writeTsDefinition(jsonDoc.components[0]);
|
|
606
|
+
|
|
607
|
+
// Custom Elements Manifest
|
|
608
|
+
const cem = buildCustomElementsManifest(components, {
|
|
609
|
+
resolveModulePath: (component) => component.filePath,
|
|
610
|
+
});
|
|
611
|
+
```
|
|
612
|
+
|
|
613
|
+
`ComponentParser` is stateful but reusable across parses — call `parseSvelteComponent` again on the same instance for the next component instead of constructing a new one each time.
|
|
614
|
+
|
|
615
|
+
See [`playground/`](playground) in this repo for a working example: it parses Svelte source typed into an editor and renders JSON, Markdown, TypeScript, and Custom Elements Manifest tabs, all client-side.
|
|
616
|
+
|
|
571
617
|
### Config File
|
|
572
618
|
|
|
573
619
|
Put a `sveld.config.js`, `sveld.config.mjs`, or `sveld.config.ts` at your project root to set defaults for the CLI and the programmatic `sveld()` API.
|
package/lib/ComponentParser.d.ts
CHANGED
|
@@ -52,6 +52,16 @@ export type ModernRunesTypeNode = {
|
|
|
52
52
|
typeName?: unknown;
|
|
53
53
|
types?: ModernRunesTypeNode[];
|
|
54
54
|
members?: ModernRunesTypeMember[];
|
|
55
|
+
/** Declaration-side `<T, U = Default>` (interfaces/type aliases). */
|
|
56
|
+
typeParameters?: {
|
|
57
|
+
params?: Array<{
|
|
58
|
+
name?: string;
|
|
59
|
+
}>;
|
|
60
|
+
};
|
|
61
|
+
/** Reference-side `<string, number>` concrete arguments (`TSTypeReference`). */
|
|
62
|
+
typeArguments?: {
|
|
63
|
+
params?: ModernRunesTypeNode[];
|
|
64
|
+
};
|
|
55
65
|
};
|
|
56
66
|
export type ModernRunesTypeMember = {
|
|
57
67
|
type?: string;
|
|
@@ -82,6 +92,13 @@ export interface ParsedComponentTypeScriptMetadata {
|
|
|
82
92
|
canonicalPropNames: string[];
|
|
83
93
|
localTypeDeclarations: string[];
|
|
84
94
|
typeImportStatements: string[];
|
|
95
|
+
/**
|
|
96
|
+
* Whether `canonicalPropsType` mentions one of the component's own
|
|
97
|
+
* `<script generics="...">` parameters (e.g. `Props<T>`). The semantic
|
|
98
|
+
* resolver has no binding for `T`, so `resolveTypes` must leave this
|
|
99
|
+
* component's props as their AST-derived text rather than expand them.
|
|
100
|
+
*/
|
|
101
|
+
referencesComponentGenerics?: boolean;
|
|
85
102
|
}
|
|
86
103
|
export declare const PARSED_COMPONENT_TYPE_SCRIPT_METADATA: unique symbol;
|
|
87
104
|
export declare function getParsedComponentTypeScriptMetadata(component: {
|
|
@@ -251,6 +268,17 @@ export interface SlotPropValue {
|
|
|
251
268
|
replace: boolean;
|
|
252
269
|
}
|
|
253
270
|
export type SlotProps = Record<string, SlotPropValue>;
|
|
271
|
+
/**
|
|
272
|
+
* Internal representation of {@link ComponentSlot} used while parsing.
|
|
273
|
+
*
|
|
274
|
+
* `slot_props` is either raw TS type text (from a JSDoc `@slot`/`@snippet` tag,
|
|
275
|
+
* used as-is) or a structured {@link SlotProps} map (from template parsing,
|
|
276
|
+
* formatted into TS type text once at the end of the parse). Keeping it
|
|
277
|
+
* structured until then avoids a JSON.stringify/JSON.parse round-trip per slot.
|
|
278
|
+
*/
|
|
279
|
+
export type InternalComponentSlot = Omit<ComponentSlot, "slot_props"> & {
|
|
280
|
+
slot_props?: string | SlotProps;
|
|
281
|
+
};
|
|
254
282
|
/**
|
|
255
283
|
* Event that is forwarded from a child component or element.
|
|
256
284
|
*
|
package/lib/ast-guards.d.ts
CHANGED
|
@@ -7,6 +7,10 @@ export declare function isMemberExpression(node: unknown): node is MemberExpress
|
|
|
7
7
|
export declare function isObjectExpression(node: unknown): node is ObjectExpression;
|
|
8
8
|
export declare function isCallExpression(node: unknown): node is CallExpression;
|
|
9
9
|
export declare function isCallExpressionNamed(node: unknown, calleeName: string): node is CallExpression;
|
|
10
|
+
/** Unwraps `expr as Type` / `expr satisfies Type`, returning the innermost expression. */
|
|
11
|
+
export declare function unwrapTypeCastExpression(node: unknown): unknown;
|
|
12
|
+
/** The type node from `expr as Type` / `expr satisfies Type`, if `node` is such a cast. */
|
|
13
|
+
export declare function getTypeCastAnnotation(node: unknown): unknown;
|
|
10
14
|
export declare function isNewExpression(node: unknown): node is NewExpression;
|
|
11
15
|
export declare function isNewExpressionNamed(node: unknown, calleeName: string): node is NewExpression;
|
|
12
16
|
export declare function isFunctionDeclaration(node: unknown): node is FunctionDeclaration;
|
package/lib/browser.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser-safe entry point.
|
|
3
|
+
*
|
|
4
|
+
* Everything exported here avoids Node built-ins (`node:fs`, `node:path`, ...),
|
|
5
|
+
* so it bundles for the browser (Vite, esbuild, webpack, Rollup) without a
|
|
6
|
+
* polyfill. It covers parsing a single component's source and rendering that
|
|
7
|
+
* result to JSON, Markdown, TypeScript definitions, or a Custom Elements
|
|
8
|
+
* Manifest — everything except the filesystem-driven project scanning
|
|
9
|
+
* (`sveld()`/`pluginSveld()`) and CLI, which only make sense in Node.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { asNormalizedPath, ComponentParser, buildComponentApiDocument } from "sveld/browser";
|
|
14
|
+
*
|
|
15
|
+
* const parser = new ComponentParser();
|
|
16
|
+
* const diagnostics = { moduleName: "Button", filePath: "Button.svelte" };
|
|
17
|
+
* const parsed = parser.parseSvelteComponent(source, diagnostics);
|
|
18
|
+
*
|
|
19
|
+
* // `parseSvelteComponent` returns component metadata only; add the fields
|
|
20
|
+
* // `ComponentDocApi` needs (`moduleName`, `filePath`) yourself.
|
|
21
|
+
* const component = {
|
|
22
|
+
* ...parsed,
|
|
23
|
+
* moduleName: diagnostics.moduleName,
|
|
24
|
+
* filePath: asNormalizedPath(diagnostics.filePath),
|
|
25
|
+
* };
|
|
26
|
+
*
|
|
27
|
+
* const doc = buildComponentApiDocument(new Map([[component.moduleName, component]]));
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export { asNormalizedPath, type NormalizedPath } from "./brands";
|
|
31
|
+
export { default as ComponentParser, type SerializedComponentEvent } from "./ComponentParser";
|
|
32
|
+
export type { SveldDiagnostic, SveldDiagnosticKind } from "./diagnostics";
|
|
33
|
+
export type { ComponentDocApi, ComponentDocs } from "./plugin";
|
|
34
|
+
export { type BuildComponentApiDocumentOptions, buildComponentApiDocument, COMPONENT_API_SCHEMA_VERSION, type ComponentApiDocument, } from "./writer/document-model";
|
|
35
|
+
export type { AppendType, MarkdownWriterBase, TocLine } from "./writer/MarkdownWriterBase";
|
|
36
|
+
export { renderComponentsToMarkdown } from "./writer/markdown-render-utils";
|
|
37
|
+
export { type BuildCustomElementsManifestOptions, buildCustomElementsManifest, type CemAttribute, type CemClassDeclaration, type CemClassField, type CemCustomElementExport, type CemEvent, type CemExport, type CemJavaScriptExport, type CemModule, type CemSlot, type CemType, type CustomElementsManifest, } from "./writer/writer-custom-elements-core";
|
|
38
|
+
export { BrowserWriterMarkdown, type WriteMarkdownCoreOptions, writeMarkdownCore, } from "./writer/writer-markdown-core";
|
|
39
|
+
export { formatTsProps, getContextDefs, getTypeDefs, type WriteTsDefinitionOptions, writeTsDefinition, } from "./writer/writer-ts-definitions-core";
|