Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

svelte-lib-helpers

shinokada2.6kMIT0.4.30

A utility package for Svelte libraries.

svelte, sveltekit, prop, package, exports

readme

Buy Me a Coffee at ko-fi.com

Svelte Lib Helpers

Svelte Lib Helpers is a utility package designed to streamline various tasks when developing Svelte libraries. It offers a set of subcommands that simplify the process of managing exports, documentation, package distribution, and more.

Repo

Svelte-lib-helpers

Installation

Install Svelte Lib Helpers as a development dependency using npm/pnpm/yarn/bun:

pnpm i -D svelte-lib-helpers

Svelte Lib Helpers provides the following subcommands to enhance your Svelte library development workflow:

exports

The exports subcommand simplifies updating your package.json by adding or updating all Svelte files in src/lib. This enables efficient imports of individual components and reducing package size for developers.

Add the following to your package.json scripts section:

"gen:exports": "svelte-lib-helpers exports",

package

Copies your project's package.json to the dist directory, allowing for seamless distribution of your library.

Add the following to your package.json scripts section:

"copy:package": "svelte-lib-helpers package",

ts

Copies your project's tsconfig.json to the dist directory, allowing for seamless distribution of your library.

Add the following to your package.json scripts section:

"copy:ts": "svelte-lib-helpers ts",

Component documentation

Generates component documentation for all Svelte files within the src/lib directory. You need to set "homepage" value in your package.json file.

 "homepage": "https://flowbite-svelte.com/",

docspropvalue (for svelte 5)

Automatically generating inline documentation comments in Svelte 5 components.

Overview

This tool scans Svelte 5 components that use the $props() syntax and adds standardized documentation comments containing prop information and links to GitHub source code and documentation.

Usage

In your scripts add the following:

"gen:docspropvalue": "svelte-lib-helpers docspropvalue <githubLink>",

Example:

"gen:docspropvalue": "svelte-lib-helpers docspropvalue themesberg/flowbite-svelte-next",

Features

  • Automatically adds documentation comments to Svelte 5 components
  • Extracts props and their default values
  • Creates links to type definitions in your GitHub repository
  • Adds links to external documentation from package.json's "homepage" field
  • Handles complex prop definitions with nested objects and arrays
  • Updates existing documentation comments when run multiple times

Requirements

  • Svelte 5
  • A types.ts file in your source directory
  • "homepage" field in your package.json pointing to documentation
  • Svelte 5 components using $props() syntax

Generated Documentation Format

The tool adds comments in this format:

<!--
@component
[Go to docs](https://your-docs-url.com)
## Type
[ComponentPropsType](https://github.com/org/repo/blob/main/src/lib/types.ts#L42)
## Props
@prop propName = defaultValue
@prop anotherProp
-->

docs5

The subcommand docs5 extracts prop definitions from your Svelte 5 components to generate documentation. It focuses on the prop destructuring syntax specific to Svelte 5 and creates a simple list of props with their default values.

Requirements

Your Svelte 5 component must use the $props() syntax for prop destructuring. The command supports various prop type annotations, including custom types.

How It Works

The command searches for the prop destructuring pattern in your Svelte files: javascriptCopylet { ... }: SomePropsType = $props()

It extracts all props defined within the curly braces. The extracted props are used to generate documentation comments in the Svelte file.

Output Format

The command generates a comment block in your Svelte file with the following structure.

From the following props structure:

  let {
    children: Snippet,
    footerType: 'sitemap' | 'default' | 'logo' | 'socialmedia' | undefined = 'default',
    class: string | undefined = '',
    ...attributes
  }: Props = $props();

To:

<!--
@component
[Go to docs](https://github.com/shinokada/svelte-lib-helpers#readme)
## Props
@prop children: Snippet
@prop footerType: 'sitemap' | 'default' | 'logo' | 'socialmedia' | undefined = 'default'
@prop class: string | undefined = ''
@prop ...attributes
-->

Notes

  • The command removes any existing @component comment blocks before adding the new one.
  • It preserves the prop definitions exactly as they appear in your code, including default values and type annotations.
  • Comments within the prop destructuring are removed from the documentation.
  • The documentation URL is taken from the homepage field in your package.json file.

Troubleshooting

If the command doesn't generate the expected output:

  1. Ensure your Svelte file uses the $props() syntax for prop destructuring.
  2. Check that your package.json has a valid homepage field.
  3. Verify that your Svelte files are in the correct directory structure.

Limitations

  • This script does not extract or display type information beyond what's in the prop destructuring.
  • It doesn't handle props defined outside of the $props() destructuring.

docs5FromType

Overview

The subcommand docs5FromType extracts prop types and default values from your Svelte components to generate documentation. It supports two common patterns for defining prop types:

  1. Types defined directly in the Svelte file
  2. Types imported from a separate TypeScript file (typically ./index.ts)

Format

Your component must use one of these patterns for prop definitions:

  • interface Props { ... }
  • interface Props extends SomeType { ... }
  • Types defined in index.ts in the same directory as the Svelte file

How It Works

  1. The command first looks for type definitions within the Svelte file itself.
  2. If no types are found in the Svelte file, it checks for imports from ./index.ts.
  3. It extracts prop names, types, and default values.
  4. The extracted information is used to generate documentation comments in the Svelte file.

Notes

  • Ensure your prop destructuring in the Svelte file matches the interface definition.
  • The command handles both let { ... }: Props = $props(); syntax for Svelte 5 and older syntax for previous versions.
  • Custom types used in your props should be defined in the same file or properly imported.

Troubleshooting

If the command doesn't generate the expected output:

  1. Check that your prop interface follows one of the supported patterns.
  2. Verify that all custom types are properly defined or imported.
  3. Ensure the Svelte file and any separate TypeScript files are in the correct locations.

Example

From the following props structure:

  interface Props {
    children: any;
    drawerStatus: boolean;
    toggleDrawer?: () => void;
    position?: 'fixed' | 'absolute';
    leftOffset?: string | undefined;
    width?: string;
    placement?: 'left' | 'right' | 'top' | 'bottom';
    transitionParams: drawerTransitionParamTypes;
  }

  let {
    children,
    drawerStatus,
    toggleDrawer,
    position = 'fixed',
    leftOffset = 'inset-y-0 start-0',
    width = 'w-80',
    placement = 'left',
    transitionParams,
    ...attributes
  }: Props = $props();

To:

<!--
@component
[Go to docs](https://github.com/shinokada/svelte-lib-helpers)
## Props
@props: children: any;
@props:drawerStatus: boolean;
@props:toggleDrawer?: () => void;
@props:position?:  'fixed' | 'absolute'; = 'fixed';
@props:leftOffset?:  string | undefined; = 'inset-y-0 start-0';
@props:width?:  string; = 'w-80';
@props:placement?:  'left' | 'right' | 'top' | 'bottom'; = 'left';
@props:transitionParams: drawerTransitionParamTypes;
-->

Or from the following structure:

  // defined type in index.ts
  import { type DropzoneProps as Props, dropzone } from '.';

  let { children, value = $bindable<string | undefined>(), files = $bindable<FileList | null>(), class: className, ...restProps }: Props = $props();

To:

<!--
@component
[Go to docs](https://github.com/shinokada/svelte-lib-helpers#readme)
## Props
@prop children
@prop value = $bindable<string | undefined>()
@prop files = $bindable<FileList | null>()
@prop class: className
@prop ...restProps
-->

Add the following to your package.json scripts section:

"gen:docs5FromType": "node ./index.js docs5FromType",

docs (svelte 4)

The docsand docsFromProps subcommands work for Svelte 4 structure. From the following props structure:

export let color: 'primary' | 'blue' | 'gray' | 'green' | 'red' | 'yellow' | 'pink' | 'purple' | 'white' | 'custom' | undefined = 'primary';
export let bg: string = 'text-gray-300';
export let customColor: string = '';
export let size: string | number = '8';

To:

<!--
@component
[Go to docs](https://github.com/shinokada/svelte-lib-helpers#readme)
## Props
@prop export let color: 'primary' | 'blue' | 'gray' | 'green' | 'red' | 'yellow' | 'pink' | 'purple' | 'white' | 'custom' | undefined = 'primary';
@prop export let bg: string = 'text-gray-300';
@prop export let customColor: string = '';
@prop export let size: string | number = '8';
-->

Or from the following structure:

  interface $$Props extends ComponentProps<TransitionFrame> {
    dismissable?: boolean;
    defaultClass?: string;
  }

  export let dismissable: $$Props['dismissable'] = false;
  export let defaultClass: $$Props['defaultClass'] = 'p-4 gap-3 text-sm';

To:

<!--
@component
[Go to docs](https://github.com/shinokada/svelte-lib-helpers#readme)
## Props
@prop export let dismissable: boolean = false
@prop export let defaultClass: string = 'p-4 gap-3 text-sm'
-->

Component data

The following commands will generate JSON files containing props, slots, and events information from all Svelte files in the src/lib directory, placing them in the routes/component-data directory.

component-data-prop-value (for svelte 5)

Automatically extracting and documenting props from Svelte 5 components.

Overview

This tool scans your Svelte 5 components that use the $props() syntax, extracts their prop definitions, and generates JSON documentation files. It links prop types to their GitHub source files when possible.

Usage

In your scripts:

"gen:component-data-prop-value": svelte-lib-helpers component-data-prop-value <githubLink>

Example:

"gen:component-data-prop-value": svelte-lib-helpers component-data-prop-value themesberg/flowbite-svelte-next

Options

  • --src: Custom source directory (default: ./src/lib)
  • --dest: Custom output directory (default: ./src/routes/component-data/)

Features

  • Extracts props from Svelte 5 components using $props() syntax
  • Identifies prop types and their documentation links
  • Handles both internal and external type imports
  • Supports generic types like HTMLAttributes<HTMLDivElement>
  • Preserves default prop values in documentation
  • Creates JSON documentation files for each component

Output Format

The tool generates JSON files with the following structure:

{
  "name": "ComponentName",
  "type": {
    "name": "TypeName",
    "link": "https://github.com/org/repo/blob/main/src/lib/types.ts#L42"
  },
  "props": [
    ["propName", "defaultValue"]
  ]
}

Requirements

  • Svelte 5
  • Svelte 5 components using $props() syntax
  • types.ts file in your source directory (optional)

component5-data

The component5-data subcommand works for Svelte 5 structure. This will generate a JSON file for each component with the following format from:

  // defined type in index.ts
  import { type CheckboxProps as Props, checkbox } from '.';

  let { children, aria_describedby, color = 'primary', custom, inline, tinted = false, rounded = false, group = $bindable([]), choices = [], checked = $bindable(false), classLabel, indeterminate, class: className, ...restProps }: Props = $props();

to:

{
  "name": "Checkbox",
  "slots": [],
  "events": [],
  "props": [
    [
      "children",
      "Snippet",
      ""
    ],
    [
      "aria_describedby",
      "string",
      ""
    ],
    [
      "color",
      "ColorType",
      "'primary'"
    ],
    [
      "custom",
      "boolean",
      ""
    ],
    [
      "inline",
      "boolean",
      ""
    ],
    [
      "tinted",
      "boolean",
      "false"
    ],
    [
      "rounded",
      "boolean",
      "false"
    ],
    [
      "group",
      "array",
      "$bindable([])"
    ],
    [
      "choices",
      "array",
      "[]"
    ],
    [
      "checked",
      "boolean",
      "$bindable(false)"
    ],
    [
      "classLabel",
      "string",
      ""
    ],
    [
      "indeterminate",
      "boolean",
      ""
    ],
    [
      "className",
      "",
      ""
    ]
  ]
}

runes-data

The runes-data subcommand works for Svelte 5 structure. This will generate a JSON file for each component with the following format from:

let {
    children: Snippet,
    footerType: 'sitemap' | 'default' | 'logo' | 'socialmedia' | undefined = 'default',
    class: string | undefined = '',
    ...attributes
  }: Props = $props();

to:

{"name":"Footer","props":[["children:Snippet",""],["footerType:'sitemap' | 'default' | 'logo' | 'socialmedia' | undefined",""],["class:string | undefined",""],["...attributes",""]]}

Example Usage

Below is an example of how you can integrate Svelte Lib Helpers subcommands into the scripts section of your package.json file within a SvelteKit project:

"scripts": {
  // ...
    "gen:exports": "node ./index.js exports",
    "gen:docs": "node ./index.js docs",
    "gen:docsFromProps": "node ./index.js docsFromProps",
    "gen:docs5": "node ./index.js docs5",
    "gen:docs5FromType": "node ./index.js docs5FromType",
    "gen:compo-data": "node ./index.js compo-data",
    "gen:componentData": "node ./index.js component-data",
    "gen:componentDataRunes": "node ./index.js component-data-runes",
    "gen:runes-data": "node ./index.js runes-data",
    "copy:package": "node ./index.js package",
    "lib-helpers": "npm run format && npm run gen:docs && npm run gen:compo-data && npm run package && npm run gen:exports && npm run copy:package",
    "package:publish": "standard-version && git push --follow-tags origin main && npm publish"
}

compo-data

The compo-data subcommand works for Svelte 4 structure. This will generate a JSON file for each component with the following format from:

export let color: 'primary' | 'blue' | 'gray' | 'green' | 'red' | 'yellow' | 'pink' | 'purple' | 'white' | 'custom' | undefined = 'primary';
export let bg: string = 'text-gray-300';
// more lines for events and slots

to:

{"name":"Spinner","slots":[],"events":[],"props":[["color","'primary' | 'blue' | 'gray' | 'green' | 'red' | 'yellow' | 'pink' | 'purple' | 'white' | 'custom' | undefined","'primary'"],["bg","string","'text-gray-300'"]]}

component-data

The component-data subcommand works for Svelte 4 structure. This will generate a JSON file for each component with the following format from:

interface $$Props extends ComponentProps<TransitionFrame> {
  dismissable?: boolean;
  defaultClass?: string;
}

export let dismissable: $$Props['dismissable'] = false;
export let defaultClass: $$Props['defaultClass'] = 'p-4 gap-3 text-sm';
// more lines for events and slots

to:

{"name":"Alert","slots":["icon","close-button"],"events":["on:close","on:click","on:change","on:keydown","on:keyup","on:focus","on:blur","on:mouseenter","on:mouseleave"],"props":[["dismissable","$$Props['dismissable']","false"],["defaultClass","$$Props['defaultClass']","'p-4 gap-3 text-sm'"]]}

License

This project is licensed under the MIT License. For details, please refer to the LICENSE file.

changelog

Changelog

0.4.30

Patch Changes

  • fix: extracting type link even if the type is imported from $lib

0.4.29

Patch Changes

  • fix: can use $derived in props for componentDataPropValue

0.4.28

Patch Changes

  • docs: update

0.4.27

Patch Changes

  • docs: update

0.4.26

Patch Changes

  • fix: componentDataPropValue to create a link for svelte/elements

0.4.25

Patch Changes

  • fix: update componentDataPropValue.js

0.4.24

Patch Changes

  • feat: add componentDataPropValue

0.4.23

Patch Changes

  • fix: addCompoDocsPropValue

0.4.22

Patch Changes

  • fix: add argument to docspropvalue

0.4.21

Patch Changes

  • fix: adding github.com/themesberg/ to type url

0.4.20

Patch Changes

  • feat: add addCompoDocsPropValue

0.4.19

Patch Changes

  • feat: add addCompoDocs5fromType2

0.4.18

Patch Changes

  • feat: add copy tsconfig.json

0.4.17

Patch Changes

  • feat: add copying tsconfig.json

0.4.16

Patch Changes

0.4.15

Patch Changes

0.4.14

Patch Changes

0.4.13

Patch Changes

0.4.12

Patch Changes

0.4.11

Patch Changes

0.4.10

Patch Changes

0.4.9

Patch Changes

0.4.8

Patch Changes

0.4.7

Patch Changes

0.4.6

Patch Changes

0.4.5

Patch Changes

  • chore: cleanup

0.4.4

Patch Changes

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

0.4.2 (2024-03-06)

Bug Fixes

  • update splitPropsInterface function for docs5FromType (e72c53e)

0.4.1

Patch Changes

0.4.0

Minor Changes

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

0.3.17 (2024-02-04)

0.3.16 (2024-02-04)

Features

Bug Fixes

  • remove exports from package (b70a319)

0.3.15 (2023-09-07)

0.3.14 (2023-09-07)

Bug Fixes

0.3.13 (2023-09-06)

Bug Fixes

0.3.12 (2023-09-06)

0.3.11 (2023-09-06)

Features

  • update extractEvents not to pick up events with = sign (43bb227)

0.3.10 (2023-09-04)

0.3.9 (2023-09-04)

Bug Fixes

0.3.8 (2023-08-28)

Bug Fixes

  • add ./ in front of names (88ae901)

0.3.7 (2023-08-28)

Bug Fixes

0.3.6 (2023-08-28)

Features

  • exportSvelteComponents works for dir and subdirs (ca5fbde)

0.3.5 (2023-08-24)

Features

0.3.4 (2023-08-22)

Bug Fixes

  • remove props key and filter .DS_Store file (f675e81)

0.3.3 (2023-08-22)

0.3.2 (2023-08-22)

0.3.1 (2023-08-22)

Features

Bug Fixes

0.2.8 (2023-08-21)

Features

  • read homepage value from package.json (769ce5d)

0.2.7 (2023-08-20)

Bug Fixes

0.2.6 (2023-08-20)

Features

  • update index and README (01bb46d)

0.2.5 (2023-08-20)

Features

  • create props dir if it does not exist (11cbb01)

0.2.4 (2023-08-20)

0.2.3 (2023-08-20)

0.2.2 (2023-08-20)

0.2.1 (2023-08-20)

Features

Bug Fixes

0.1.5 (2023-08-20)

Bug Fixes

0.1.4 (2023-08-20)

Bug Fixes

0.1.3 (2023-08-20)

Bug Fixes

0.1.2 (2023-08-20)

Features

0.1.1 (2023-08-20)

Features