Skip to content

如何在umi中使用twin.macro #12225

Closed Answered by fz6m
DemonSam asked this question in Q&A
Mar 23, 2024 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

如果你要使用 twin.macro ,就得关掉 mfsu: false ,因为 mfsu 不支持宏。

这是我可以跑通的一个最小例子:

  pnpm add -D babel-plugin-twin babel-plugin-macros
// .umirc.ts

  // 打开 tailwind 功能,确保你有 `tailwind.config.js` ,这可以是 `max g taillwindcss` 生成出来的
  tailwindcss: {},

  extraBabelPlugins: [
    'babel-plugin-twin',
    'babel-plugin-macros',
  ],
  mfsu: false,
import tw from 'twin.macro'

const Component = tw.div`border hover:border-black`

<Component>1111</Component>

使用 styled-components 时,基于上文配置再添加以下配置:

// .umirc.ts

  styledComponents: {},
// package.json

  "babelMacros": {
    "twin": {
      "preset": "styled-components",
    }
  }
// src/twin.d.ts

// copy https://github.com/ben-rogerson/twin.examples/blob/master/web…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@DemonSam
Comment options

Answer selected by DemonSam
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants