Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Storybook crashes with the error of p has already been declared #274

Open
JinIgarashi opened this issue Jan 29, 2025 · 6 comments
Open
Labels
bug Something isn't working next Related to the next version

Comments

@JinIgarashi
Copy link

JinIgarashi commented Jan 29, 2025

Describe the bug

I am trying to migrate storybook from svelte 4 to svelte 5 in my monorepo by using 5.0.0-next.23 version of the addon.

I did migration according to this guide (https://github.com/storybookjs/addon-svelte-csf/blob/next/MIGRATION.md).

After that, when I execute pnpm storybook, storybook is launched in a browser tab, but it crashes immediately. and it shows the following error in console.

pnpm storybook  

> @undp-data/[email protected] storybook /Users/j_igarashi/Documents/git/UNDP-Data/geohub/packages/svelte-undp-components
> storybook dev -p 6006

@storybook/core v8.5.2

WARN The "@storybook/addon-mdx-gfm" addon is meant as a migration assistant for Storybook 8.0; and will likely be removed in a future version.
WARN It's recommended you read this document:
WARN https://storybook.js.org/docs/writing-docs/mdx#markdown-tables-arent-rendering-correctly
WARN 
WARN Once you've made the necessary changes, you can remove the addon from your package.json and storybook config.
info => Serving static files from ././static at /
info => Starting manager..
info => Starting preview..
2:44:09 PM [vite-plugin-svelte] WARNING: The following packages have a svelte field in their package.json but no exports condition for svelte.

[email protected]
[email protected]
[email protected]

Please see https://github.com/sveltejs/vite-plugin-svelte/blob/main/docs/faq.md#missing-exports-condition for details.
The following Vite config options will be overridden by SvelteKit:
  - base
2:44:09 PM [vite-plugin-svelte] WARNING: The following packages have a svelte field in their package.json but no exports condition for svelte.

[email protected]
[email protected]
[email protected]

Please see https://github.com/sveltejs/vite-plugin-svelte/blob/main/docs/faq.md#missing-exports-condition for details.
╭──────────────────────────────────────────────────╮
│                                                  │
│   Storybook 8.5.2 for sveltekit started          │
│   1.6 s for manager and 247 ms for preview       │
│                                                  │
│    Local:            http://localhost:6006/      │
│    On your network:  http://192.168.1.9:6006/    │
│                                                  │
╰──────────────────────────────────────────────────╯
The TypeScript option verbatimModuleSyntax is now required when using Svelte files with lang="ts". Please add it to your tsconfig.json.
✘ [ERROR] /Users/j_igarashi/Documents/git/UNDP-Data/geohub/node_modules/.pnpm/@[email protected]_@[email protected][email protected]_prettier@3._rqinx6ckxdonhffg4jrbyvdgku/node_modules/@storybook/addon-svelte-csf/dist/runtime/Story.svelte:6:24 `p` has already been declared
https://svelte.dev/e/declaration_duplicate [plugin vite-plugin-svelte:optimize-svelte]

    ../../node_modules/.pnpm/@[email protected]_@[email protected][email protected]_prettier@3._rqinx6ckxdonhffg4jrbyvdgku/node_modules/@storybook/addon-svelte-csf/dist/runtime/Story.svelte:6:24:
      6 │        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
        ╵                         ^

  The plugin "vite-plugin-svelte:optimize-svelte" was triggered by this import

    ../../node_modules/.pnpm/@[email protected]_@[email protected][email protected]_prettier@3._rqinx6ckxdonhffg4jrbyvdgku/node_modules/@storybook/addon-svelte-csf/dist/index.js:1:27:
      1 │ import StoryComponent from './runtime/Story.svelte';~~~~~~~~~~~~~~~~~~~~~~~~

/Users/j_igarashi/Documents/git/UNDP-Data/geohub/node_modules/.pnpm/[email protected][email protected]/node_modules/storybook/bin/index.cjs:23
  throw error;
  ^

Error: Build failed with 1 error:
../../node_modules/.pnpm/@[email protected]_@[email protected][email protected]_prettier@3._rqinx6ckxdonhffg4jrbyvdgku/node_modules/@storybook/addon-svelte-csf/dist/runtime/Story.svelte:6:24: ERROR: [plugin: vite-plugin-svelte:optimize-svelte] /Users/j_igarashi/Documents/git/UNDP-Data/geohub/node_modules/.pnpm/@[email protected]_@[email protected][email protected]_prettier@3._rqinx6ckxdonhffg4jrbyvdgku/node_modules/@storybook/addon-svelte-csf/dist/runtime/Story.svelte:6:24 `p` has already been declared
https://svelte.dev/e/declaration_duplicate
    at failureErrorWithLog (/Users/j_igarashi/Documents/git/UNDP-Data/geohub/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:1476:15)
    at /Users/j_igarashi/Documents/git/UNDP-Data/geohub/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:945:25
    at /Users/j_igarashi/Documents/git/UNDP-Data/geohub/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:1354:9
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5) {
  errors: [Getter/Setter],
  warnings: [Getter/Setter]
}

Node.js v22.11.0
 ELIFECYCLE  Command failed with exit code 7.

I can see some error from addon-svelte-csf, but I am not sure what causes this error. I tried to make a blank sveltekit project with storybook from scratch and compared settings between official demo and ours, I could not figure out why this error is happening.

Does anyone have any idea how to solve this error?

Steps to reproduce the behavior

So far, it can be reproducible in our repo

  1. Clone https://github.com/UNDP-Data/geohub/tree/fix/4333-svelte-5-components
  2. Checkout fix/4333-svelte-5-components branch
  3. cd packages/svelte-undp-components
  4. pnpm install
  5. pnpm storybook
  6. browser is launched, but it crashes and error is shown in terminal.

Expected behavior

Storybook works without error as current version of svelte 4 does.

current svelte 4 version of storybook can be seen here (https://svelte-components.undpgeohub.org/)

Screenshots and/or logs

Image

Environment

  • OS: MacOS
  • Node.js version: v22.11.0
  • NPM version: 10.9.0
  • Browser (if applicable): arc
  • Browser version (if applicable): Version 1.78.1 (57736) (Chromium Engine Version 132.0.6834.84)
  • Device (if applicable): n/a

Additional context

addon-svelte-csf's *stories.svelte is mostly used under src/lib/components/maplibre folder, and other components use *.stories.ts to test svelte components.

When I remove *.stories.svelte files on main.ts (https://github.com/UNDP-Data/geohub/blob/develop/packages/svelte-undp-components/.storybook/main.ts) as below, storybook works fine.

import type { StorybookConfig } from '@storybook/sveltekit';

const config: StorybookConfig = {
-	stories: ['../src/**/*.mdx', '../src/**/*.stories.@(js|jsx|mjs|ts|tsx|svelte)'], // this failed
+	stories: ['../src/**/*.mdx', '../src/**/*.stories.@(js|jsx|mjs|ts|tsx)'],  // this works
 	addons: [
@JinIgarashi JinIgarashi added the bug Something isn't working label Jan 29, 2025
@xeho91
Copy link
Collaborator

xeho91 commented Jan 29, 2025

I've seen this error before when working on support for Svelte v5.

Reference: #195

In the previous case, the issue was that the props weren't destructured from the $props() rune statement. I'm not sure if that's the same case for you. So, we might have to go blind a little to figure out the reason.


After a quick inspection at your repository. I noticed there's both modern and legacy API used in *.stories.svelte files.

Can you update your config here: https://github.com/UNDP-Data/geohub/blob/39b1791004aef5cd73155469657ff99488a1e786/packages/svelte-undp-components/.storybook/main.ts#L9
with those instructions: https://github.com/storybookjs/addon-svelte-csf/blob/next/README.md#legacy-api ?

@xeho91 xeho91 added the next Related to the next version label Jan 29, 2025
@JinIgarashi
Copy link
Author

@xeho91 thanks for your comment. I migrated all legacyAPI to new version of addon. there is still same error when I enable legacyTemplate: true at main.ts.
However It works if I switch sveltePreprocess to vitePreprocess. Previously, I switched to sveltePreprocess because svelte-package hangs (sveltejs/kit#13122) when building package with vitePreprocess.

The bug of svelte-package with vitePreprocess seems not being solved yet. I could not switch back to vitePreprocess since building process will not finish forever.

@xeho91
Copy link
Collaborator

xeho91 commented Jan 29, 2025

Perhaps you can try to use vitePreprocess only for Storybook inside the ./.storybook/main.ts?

You'll likely be able to achieve it with using viteFinal

@JinIgarashi
Copy link
Author

@xeho91 Thanks. I did not know viteFinal. I saw the documentation that viteFinal apply additional changes on vite.config.ts, but vitePreprocess is set to preprocess property of svelte.config.js. Is there a way to use different svelte.config only for storybook?

@xeho91
Copy link
Collaborator

xeho91 commented Jan 29, 2025

Hm, on the second thought. I'm trying to think of a simpler solution/workaround for you.

I wonder if you can use environment variable STORYBOOK_ENV to conditionally choose between sveltePreprocess and vitePreprocess inside your svelte.config.js. My guts tells me this should do the trick?

EDIT: Sorry, this one is not an environmental variable, AFAIK. CC @JReinhold is there anything we can use for this case?

@JinIgarashi
Copy link
Author

@xeho91 Thanks. I got idea from you and modified svelte.config.js like below.

import adapter from '@sveltejs/adapter-auto';
import { sveltePreprocess } from 'svelte-preprocess';
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';

/** @type {import('@sveltejs/kit').Config} */
const config = {
	// Consult https://kit.svelte.dev/docs/integrations#preprocessors
	// for more information about preprocessors
	preprocess:
		process.env.STORYBOOK_BUILD === 'true'
			? vitePreprocess({ script: true })
			: sveltePreprocess({ scss: true }),

	kit: {
		// adapter-auto only supports some environments, see https://kit.svelte.dev/docs/adapter-auto for a list.
		// If your environment is not supported or you settled on a specific environment, switch out the adapter.
		// See https://kit.svelte.dev/docs/adapters for more information about adapters.
		adapter: adapter()
	}
};

export default config;

then, changed scripts in package.json as follows.

	"scripts": {
		"storybook": "STORYBOOK_BUILD=true storybook dev -p 6006",
		"build-storybook": "STORYBOOK_BUILD=true storybook build"
	},

Somehow managed to switch preprocess module for storybook build. and packaging with svelte-package also works.

I am not sure this is the best way to solve it though....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working next Related to the next version
Projects
None yet
Development

No branches or pull requests

2 participants