aspiring_data_analyst
aspiring_data_analyst

Reputation: 156

How can typescript be setup in Storybook with Svelte?

I'm trying to use Storybook with a Svelte component library. Svelte is set up to use Rollup. I wonder if Storybook's use of Webpack could have anything to do with my issue?

Example:

I changed this:

<script lang="ts">
  export let text = ''
  export let sent = true
</script>

to this:

<script lang="ts">
  export let text: string
  export let sent: boolean
</script>

I get this error:

Module build failed (from ./node_modules/svelte-loader/index.js):
Error: ParseError: Unexpected token (2:17)
1: <script lang="ts">
2:   export let text: string
                    ^

Upvotes: 8

Views: 2256

Answers (1)

aspiring_data_analyst
aspiring_data_analyst

Reputation: 156

To configure Storybooks Webpack for this, I changed .storybook/main.js to this: (difference being adding webPackFinal):

module.exports = {
  webpackFinal: async (config) => {
    const svelteLoader = config.module.rules.find( (r) => r.loader && r.loader.includes('svelte-loader'))
    svelteLoader.options.preprocess = require('svelte-preprocess')()
    return config
  },
  "stories": [
    // "../src/**/*.stories.mdx",
    "../src/**/*.stories.@(js|jsx|ts|tsx)"
  ],
  "addons": [
    "@storybook/addon-links",
    { name: "@storybook/addon-essentials", options: { docs: false } }
  ]
}

NOTE: Typescript/Svelte will not play nicely with the __.stories.mdx docs you can create with the docs addon. There may be some way to setup custom configuration for this but I certainly couldn't figure it out.

Upvotes: 8

Related Questions