简体   繁体   English

如何使用 Svelte 在 Storybook 中设置 typescript?

[英]How can typescript be setup in Storybook with Svelte?

I'm trying to use Storybook with a Svelte component library.我正在尝试将 Storybook 与 Svelte 组件库一起使用。 Svelte is set up to use Rollup. Svelte 设置为使用 Rollup。 I wonder if Storybook's use of Webpack could have anything to do with my issue?我想知道 Storybook 使用 Webpack 是否与我的问题有关?

  • Storybook is working just fine as long as my svelte components are written in JS只要我的 svelte 组件是用 JS 编写的,Storybook 就可以正常工作
  • Svelte is set up to work with TS just fine Svelte 设置为与 TS 一起工作就好了
  • adding even a simple type declaration to a component's script breaks Storybook在组件的脚本中添加一个简单的类型声明会破坏 Storybook

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
                    ^

To configure Storybooks Webpack for this, I changed .storybook/main.js to this: (difference being adding webPackFinal ):为了为此配置 Storybooks Webpack,我将.storybook/main.js更改为:(不同之处在于添加了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.注意:Typescript/Svelte 不能很好地处理您可以使用文档插件创建的__.stories.mdx文档。 There may be some way to setup custom configuration for this but I certainly couldn't figure it out.可能有一些方法可以为此设置自定义配置,但我当然无法弄清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM