【问题标题】:Debugging styled-components in typescript在打字稿中调试样式组件
【发布时间】:2018-06-06 04:24:27
【问题描述】:

考虑到使用 babel-plugin 包装 typescript 来调试 styled-component(例如,在 dev-tools 中查看组件名称)并不好,我怎样才能实现相同的功能?

谢谢

【问题讨论】:

    标签: reactjs typescript styled-components


    【解决方案1】:

    感谢您的建议 我最终在打字稿上添加了 babel,它工作正常。

    如果有人决定在这里做同样的事情,这就是 webpack 规则(虽然相当平庸):

      {
        test: /\.tsx?$/,
        use: [
          {
            loader: 'babel-loader',
            options: {
              presets: [
                ["@babel/env", {
                  "targets": ["> 1%"]
                }],
                "@babel/react"
              ],
              plugins: [
                // "lodash",
                "babel-plugin-styled-components",
                "babel-plugin-transform-react-stateless-component-name"
              ]
            }
          },
          {
            loader: 'ts-loader',
            options: {
              compilerOptions: {
                jsx: 'preserve',
                module: 'es2015'
              }
            }
          }
        ],
        include: SRC_ROOT
      }
    

    不得不跳过 .babelrc,因为storybook 在找到自定义 .babelrc 文件时开始产生错误。

    【讨论】:

    • 对于带有 react-scripts-ts 和 react-app-rewire 的 create-react-apps,我做了一个简单的plugin 来为样式化组件添加一个打字稿插件。
    猜你喜欢
    • 2021-06-07
    • 2020-06-27
    • 2020-08-02
    • 2018-02-09
    • 2018-03-10
    • 2016-10-10
    • 2021-11-18
    • 2020-11-02
    • 2021-12-30
    相关资源
    最近更新 更多