diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml new file mode 100644 index 00000000..ac671da8 --- /dev/null +++ b/.github/workflows/check.yml @@ -0,0 +1,47 @@ +name: Check + +on: [push] + +defaults: + run: + shell: bash + +env: + CI: true + +permissions: + contents: read + +jobs: + test: + name: Test + runs-on: ubuntu-latest + timeout-minutes: 2 + steps: + # https://github.com/actions/checkout + - uses: actions/checkout@v4 + + - name: Enable corepack (for pnpm) + # https://nodejs.org/api/corepack.html + run: corepack enable + + - name: Setup Node.js + # https://github.com/actions/setup-node + uses: actions/setup-node@v4 + with: + cache: pnpm + node-version-file: '.nvmrc' + + - name: Install Node.js dependencies with pnpm + # https://pnpm.io/cli/install + run: > + pnpm install + --frozen-lockfile + + - name: Build package + run: > + pnpm build + + - name: Run svelte-check + run: > + pnpm check diff --git a/.github/workflows/chromatic.yml b/.github/workflows/chromatic.yml index abe26ee9..5ce9df25 100644 --- a/.github/workflows/chromatic.yml +++ b/.github/workflows/chromatic.yml @@ -1,23 +1,49 @@ -name: "Chromatic" +name: Chromatic + on: push + jobs: publish: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: fetch-depth: 0 - - name: Install Node.js - uses: actions/setup-node@v3 + + - name: Enable corepack (for pnpm) + # https://nodejs.org/api/corepack.html + run: corepack enable + + - name: Use Node.js + uses: actions/setup-node@v4 with: - node-version: 20 - - name: Install dependencies - uses: pnpm/action-setup@v2 - with: - version: 8 - run_install: true - - name: Publish to Chromatic - uses: chromaui/action@v1 + cache: 'pnpm' + node-version-file: '.nvmrc' + + - name: Install Node.js dependencies with pnpm + # https://pnpm.io/cli/install + run: > + pnpm install + --frozen-lockfile + + - name: Build package + run: > + pnpm run build + + - name: Upload to Chromatic + uses: chromaui/action@v11 with: token: ${{ secrets.GITHUB_TOKEN }} projectToken: ${{ secrets.CHROMATIC_PROJECT_TOKEN }} + onlyChanged: true + exitOnceUploaded: true + + - name: Upload "Examples" to Chromatic + uses: chromaui/action@v11 + env: + EXAMPLES_ONLY: true + with: + token: ${{ secrets.GITHUB_TOKEN }} + projectToken: ${{ secrets.CHROMATIC_PROJECT_TOKEN_EXAMPLES_ONLY }} + onlyChanged: true + exitOnceUploaded: true diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index ea6cfd0f..95c2a95d 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -7,26 +7,30 @@ jobs: runs-on: ubuntu-latest if: "!contains(github.event.head_commit.message, 'ci skip') && !contains(github.event.head_commit.message, 'skip ci')" steps: - - uses: actions/checkout@v3 - - - name: Prepare repository - run: git fetch --unshallow --tags + - uses: actions/checkout@v4 + with: + fetch-depth: 0 - name: 'Set git user' run: | git config --global user.name 'storybook-bot' git config --global user.email '32066757+storybook-bot@users.noreply.github.com' - - name: Use Node.js 20.x - uses: actions/setup-node@v3 - with: - node-version: 20.x + - name: Enable corepack (for pnpm) + # https://nodejs.org/api/corepack.html + run: corepack enable - - name: Install dependencies - uses: pnpm/action-setup@v2 + - name: Use Node.js + uses: actions/setup-node@v4 with: - version: 8 - run_install: true + cache: 'pnpm' + node-version-file: '.nvmrc' + + - name: Install Node.js dependencies with pnpm + # https://pnpm.io/cli/install + run: > + pnpm install + --frozen-lockfile - name: Create Release env: diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 00000000..46a4a706 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,44 @@ +name: Test + +on: [push] + +defaults: + run: + shell: bash + +env: + CI: true + +permissions: + contents: read + +jobs: + test: + name: Test + runs-on: ubuntu-latest + timeout-minutes: 2 + steps: + # https://github.com/actions/checkout + - uses: actions/checkout@v4 + + - name: Enable corepack (for pnpm) + # https://nodejs.org/api/corepack.html + run: corepack enable + + - name: Setup Node.js + # https://github.com/actions/setup-node + uses: actions/setup-node@v4 + with: + cache: pnpm + node-version-file: '.nvmrc' + + - name: Install Node.js dependencies with pnpm + # https://pnpm.io/cli/install + run: > + pnpm install + --frozen-lockfile + + - name: Run tests with Vitest + # https://vitest.dev/guide/cli.html + run: > + pnpm vitest run diff --git a/.gitignore b/.gitignore index 099dd511..63df3116 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +.vite-inspect/ +.svelte-kit/ dist/ node_modules/ storybook-static/ @@ -5,4 +7,7 @@ build-storybook.log package-lock.json .DS_Store .env -*.log \ No newline at end of file +*.log + +# .d.ts files generated by `svelte-package` +examples/**/*.d.ts diff --git a/.nvmrc b/.nvmrc new file mode 100644 index 00000000..3c032078 --- /dev/null +++ b/.nvmrc @@ -0,0 +1 @@ +18 diff --git a/.prettierignore b/.prettierignore index 1521c8b7..7027b798 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1 +1,3 @@ dist +tests/__compiled__ +pnpm-lock.yaml diff --git a/.prettierrc b/.prettierrc deleted file mode 100644 index 51060f63..00000000 --- a/.prettierrc +++ /dev/null @@ -1,8 +0,0 @@ -{ - "printWidth": 100, - "tabWidth": 2, - "bracketSpacing": true, - "trailingComma": "es5", - "singleQuote": true, - "arrowParens": "always" -} \ No newline at end of file diff --git a/.storybook/main.ts b/.storybook/main.ts index bc28ab10..c2e18187 100644 --- a/.storybook/main.ts +++ b/.storybook/main.ts @@ -1,19 +1,23 @@ import type { StorybookConfig } from '@storybook/svelte-vite'; +const examplesOnly = process.env.EXAMPLES_ONLY === 'true'; + const config: StorybookConfig = { + stories: [ + '../**/*.mdx', + { + directory: '../examples', + files: '**/*.stories.@(ts|svelte)', + titlePrefix: examplesOnly ? undefined : 'Examples', + }, + !examplesOnly && { + directory: '../tests/stories', + files: '**/*.stories.@(ts|svelte)', + titlePrefix: 'Tests', + }, + ].filter(Boolean) as StorybookConfig['stories'], framework: '@storybook/svelte-vite', - stories: [{ - directory: '../stories', - files:'**/*.stories.svelte', - titlePrefix:'Demo' - }], - addons: [ - '../dist/preset/index.js', - '@storybook/addon-essentials', - '@storybook/addon-interactions', - ], - docs: { - autodocs: 'tag' - } + addons: ['../dist/preset.js', '@storybook/addon-essentials', '@storybook/addon-interactions'], }; + export default config; diff --git a/CHANGELOG.md b/CHANGELOG.md index c9809349..d417fad5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -149,7 +149,7 @@ #### πŸ› Bug Fix -- Allow configuration of filename patterns besides *.stories.svelte [#140](https://github.com/storybookjs/addon-svelte-csf/pull/140) ([@j3rem1e](https://github.com/j3rem1e)) +- Allow configuration of filename patterns besides \*.stories.svelte [#140](https://github.com/storybookjs/addon-svelte-csf/pull/140) ([@j3rem1e](https://github.com/j3rem1e)) #### Authors: 1 diff --git a/ERRORS.md b/ERRORS.md new file mode 100644 index 00000000..ee50a44a --- /dev/null +++ b/ERRORS.md @@ -0,0 +1,327 @@ +# Errors + +This document is a list of known errors that this addon throws. + +## `PARSER_EXTRACT_SVELTE` + +### `SB_SVELTE_CSF_PARSER_EXTRACT_SVELTE_0001` + +No **[module context]** was found in the stories file. + +This often happens if you call `defineMeta(...)` in a regular instance script (` +``` + +[module context]: https://svelte.dev/docs/svelte-components#script-context-module + +### `SB_SVELTE_CSF_PARSER_EXTRACT_SVELTE_0002` + +A **default or namespace import** was used to import from this addon package, which is not supported. Only named imports are supported (this only applies to imports from `@storybook/addon-svelte-csf`). + +Change your import to a named import instead: + +```diff +- import svelteCsf from "@storybook/addon-svelte-csf"; ++ import { defineMeta } from "@storybook/addon-svelte-csf"; +``` + +### `SB_SVELTE_CSF_PARSER_EXTRACT_SVELTE_0003` + +No import of `defineMeta` from this addon package was found in the **[module context]**. + +You might have forgotten to import it: + +```diff + +``` + +### `SB_SVELTE_CSF_PARSER_EXTRACT_SVELTE_0004` + +No variable declaration from the `defineMeta()` call was found. While you might have called `defineMeta()`, its result needs to be assigned to a variable: + +```diff + +``` + +### `SB_SVELTE_CSF_PARSER_EXTRACT_SVELTE_0005` + +No **destructured** `Story` component was found in the variable declaration with the `defineMeta()` call. + +The `Story` component might have been incorrectly created: + +```diff + +``` + +### `SB_SVELTE_CSF_PARSER_EXTRACT_SVELTE_0006` + +The **first argument** to the `defineMeta()` call was invalid. +It must be a valid **object expression** with the same structure as [the Default export in CSF](https://storybook.js.org/docs/api/csf#default-export). + +```diff + +``` + +### `SB_SVELTE_CSF_PARSER_EXTRACT_SVELTE_0007` + +A `` component received an invalid `children` prop. If set, the `children` prop must be a reference to a [snippet](https://svelte-5-preview.vercel.app/docs/snippets) defined in the root scope file. Eg.: + +```svelte +{#snippet template()} + πŸš€ +{/snippet} + + +``` + +This error indicates that the `children` prop was passed, but it was not correctly referencing a snippet. + +### `SB_SVELTE_CSF_PARSER_EXTRACT_SVELTE_0008` + +`setTemplate()` was called to set a default snippet, but the argument passed was not a reference to a root-level snippet in the file. + +Below is a demonstration of correct usage: + +```svelte + + +{#snippet template()} + +{/snippet} +``` + +## `PARSER_EXTRACT_COMPILED` + +> [!NOTE] +> The errors in this category are most likely internal bugs during parsing of the compiled output rather than caused by users' invalid code. + +### `SB_SVELTE_CSF_PARSER_EXTRACT_COMPILED_0001` + +The import of `defineMeta` from this addon could not be found while parsing the _compiled_ code. + +If you get this error, please open a bug report with detailed reproduction steps including the code that caused the error. + +https://github.com/storybookjs/addon-svelte-csf/issues/new + +### `SB_SVELTE_CSF_PARSER_EXTRACT_COMPILED_0002` + +A variable declaration with the `defineMeta` call could not be found while parsing the _compiled_ code. + +If you get this error, please open a bug report with detailed reproduction steps including the code that caused the error. + +https://github.com/storybookjs/addon-svelte-csf/issues/new + +### `SB_SVELTE_CSF_PARSER_EXTRACT_COMPILED_0003` + +A default export could not be found while parsing the _compiled_ code. The Svelte compiler should automatically default export a component, but this couldn't be found for some reason. + +If you get this error, please open a bug report with detailed reproduction steps including the code that caused the error. + +https://github.com/storybookjs/addon-svelte-csf/issues/new + +### `SB_SVELTE_CSF_PARSER_EXTRACT_COMPILED_0004` + +A `Story` identifier could not be found while parsing the _compiled_ code. + +If you get this error, please open a bug report with detailed reproduction steps including the code that caused the error. + +https://github.com/storybookjs/addon-svelte-csf/issues/new + +### `SB_SVELTE_CSF_PARSER_EXTRACT_COMPILED_0005` + +A main function component could not be found while parsing the _compiled_ code. + +If you get this error, please open a bug report with detailed reproduction steps including the code that caused the error. + +https://github.com/storybookjs/addon-svelte-csf/issues/new + +### `SB_SVELTE_CSF_PARSER_EXTRACT_COMPILED_0006` + +A Story-component's props could not be extracted as an object expression from the _compiled_ code. + +If you get this error, please open a bug report with detailed reproduction steps including the code that caused the error. + +https://github.com/storybookjs/addon-svelte-csf/issues/new + +## `PARSER_ANALYSE_DEFINE_META` + +### `SB_SVELTE_CSF_PARSER_ANALYSE_DEFINE_META_0001` + +The `component` property in `defineMeta` was not referencing an imported Svelte component. +It expected an identifier to a Svelte component but got something else. + +Ensure you're using the correct syntax, following the example below: + +```svelte + +``` + +### `SB_SVELTE_CSF_PARSER_ANALYSE_DEFINE_META_0002` + +Our parser spotted an invalid schema on the variable declaration from the `defineMeta` call. +You most likely forgot to destructure the return value. + +```diff +- const Story = defineMeta({ ++ const { Story } = defineMeta({ + component: Button, +}); +``` + +### `SB_SVELTE_CSF_PARSER_ANALYSE_DEFINE_META_0003` + +The addon automatically adds a `meta` identifier to the return value of `defineMeta()` during compilation. +But for some reason, this couldn't be found in the compiled output. + +If you see this error, please open a [bug report](https://github.com/storybookjs/addon-svelte-csf/issues/new). + +While you create an issue, please provide original code of the stories file that caused this error. +It will help us investigate the occurred issue better. + +### `SB_SVELTE_CSF_PARSER_ANALYSE_DEFINE_META_0004` + +When analysing the object passed to `defineMeta({ ... })`, invalid properties were found. The following properties must be **static string literals**, but got something else: + +- `title` +- `name` + +Dynamically generating these properties with functions or with template strings is not supported. + +### `SB_SVELTE_CSF_PARSER_ANALYSE_DEFINE_META_0005` + +When analysing the object passed to `defineMeta({ ... })`, invalid properties were found. The `tags` property must be a **static array of static string literals**, but got something else + +Dynamically generating the array or the entries with functions or with template strings is not supported. + +### `SB_SVELTE_CSF_PARSER_ANALYSE_DEFINE_META_0006` + +When analysing the object passed to `defineMeta({ ... })`, invalid properties were found. The `tags` property must be a **static array of static string literals**, but got something else + +Dynamically generating the array or the entries with functions or with template strings is not supported. + +## `PARSER_ANALYSE_STORY` + +### `SB_SVELTE_CSF_PARSER_ANALYSE_STORY_0001` + +When analysing one of the `` definitions, a **static literal string** was expected but found something else. + +The following props to the `Story` component **must** be a **static literal string**: + +- `name` +- `exportName` + +Dynamically generating the string is not supported, eg. with a function or with template string. + +Examples of valid syntax: + +```svelte + + + +``` + +### `SB_SVELTE_CSF_PARSER_ANALYSE_STORY_0002` + +When analysing one of the `` definitions, a **static array of static literal strings** was expected but found something else. + +The `tags` prop to the `Story` component **must** be a **static array of static literal strings**. + +Dynamically generating the array or the strings is not supported, eg. with a function or with template strings. + +Examples of valid syntax: + +```svelte + +``` + +### `SB_SVELTE_CSF_PARSER_ANALYSE_STORY_0003` + +When analysing one of the `` definitions, a **static array of static literal strings** was expected but found something else. + +The `tags` prop to the `Story` component **must** be a **static array of static literal strings**. + +Dynamically generating the array or the strings is not supported, eg. with a function or with template strings. + +Examples of valid syntax: + +```svelte + +``` + +### `SB_SVELTE_CSF_PARSER_ANALYSE_STORY_0004` + +When analysing one of the `` definitions, no `name` or `exportName` was found. + +Please ensure that every `` component uses one or both of these attributes, see example below: + +```svelte + + + + + +``` + +### `SB_SVELTE_CSF_PARSER_ANALYSE_STORY_0005` + +The `exportName` prop in a `` component is not a valid JavaScript variable name. + +**It must be a valid JavaScript variable name.** +It must start with a letter, `$` or `_`, followed by letters, numbers, `$` or `_`. +Reserved words like `default` are also not allowed (see ). + +### `SB_SVELTE_CSF_PARSER_ANALYSE_STORY_0006` + +Multiple `` components have duplicate export names. + +**This can happen when `exportName` is implicitly derived by `name` attribute.** + +Complex names will be simplified to a `PascalCased`, valid JavaScript variable name, +eg. `Some story name!!` will be converted to `SomeStoryName`. + +You can fix this collision by providing a unique `exportName` prop with``. + +See more in [the `exportName` API docs](./README.md#custom-export-name). diff --git a/MIGRATION.md b/MIGRATION.md new file mode 100644 index 00000000..4b65d64b --- /dev/null +++ b/MIGRATION.md @@ -0,0 +1,268 @@ +# Migration + +This document is a guide how to migrate the usage of addon from a specific version to next one. + +## From `v4` to `v5` + +### Version compatibility + +**This new iteration of the addon will require Svelte 5.**\ +While Svelte 5 itself largely supports the Svelte 4 syntax, this means that your actual components most likely don’t need to change, but as you’ll see below, your stories file will require migration to the new snippet syntax. + +| Dependency | Version | +| ---------------------------------------------------------------------------------------------------------------------- | -------- | +| [Storybook](https://github.com/storybookjs/storybook) | `v8.0.0` | +| [Svelte](https://github.com/sveltejs/svelte) | `v5.0.0` | +| [Vite](https://github.com/vitejs/vite) | `v5.0.0` | +| [`@sveltejs/vite-plugin-svelte`](https://github.com/sveltejs/vite-plugin-svelte/tree/main/packages/vite-plugin-svelte) | `v4.0.0` | + +> [!IMPORTANT] +> Removed support for Webpack. + +--- + +### `` component removed in favor of `defineMeta` + +Before: + +```svelte + + + +``` + +After: + +```svelte + +``` + +Difference: + +```diff +- + +- +``` + +--- + +### `export meta` removed in favor of `defineMeta` + +Before: + +```svelte + + + +``` + +After: + +```svelte + +``` + +Difference: + +```diff + +``` + +--- + +### `` slots replaced with snippets + +1. [Inline snippet](./README.md#inline-snippet) +1. [Shared snippet](./README.md#shared-snippet) + +--- + +### `` directive `let:args` replaced with snippets first argument + +Before: + +```svelte + +