18 个版本 (破坏性)
| 0.17.0 | 2023 年 6 月 9 日 |
|---|---|
| 0.15.0 | 2023 年 2 月 20 日 |
| 0.12.0 | 2022 年 12 月 27 日 |
| 0.9.0 | 2022 年 11 月 5 日 |
#1723 in Web 编程
每月 59 次下载
2MB
65K SLoC
stailwc (快速 Tailwind 编译器)
这是一个实验性的 SWC 转换器,可以将编译时 Tailwind 宏引入 SWC(和 nextjs),类似于 twin 宏。目标是提供与基于 babel 的替代方案相当的开发时 ergonomics 和灵活性,同时性能有显著提升。支持 emotion 和 styled-components 的 CSS-in-JS,以及许多构建系统,如 SWC、nextjs、Vite 等。
兼容性图表
我们目前正在测试以下版本。其他版本可能仍然可以使用。
| stailwc | NextJS | Emotion | Styled Components | swc | Vite |
|---|---|---|---|---|---|
| 最新版 | 13.4.3 | 11.10.5 | 5.3.6 | 1.3.24 | 4.1.0 |
功能图表
| 功能 | Emotion | Styled Components |
|---|---|---|
tw JSX 属性 |
✅ | ✅ |
tw 模板标签 |
✅ | ✅ |
tw 组件语法 |
✅ | ✅ |
tw 组件扩展语法 |
✅ | ✅ |
| 全局样式 | ✅ | ✅ |
| 插件参数建议 | ✅ | ✅ |
入门
> npm add -D stailwc
> yarn add -D stailwc
> pnpm add -D stailwc
要开始使用 NextJS,请将以下内容放置在您的 next.config.js 中。对于其他框架/工具,请参阅示例。
next.config.js
const stailwc = require("stailwc/install");
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
swcMinify: true,
experimental: {
swcPlugins: [
stailwc({
engine: "emotion", // or "styled-components"
}),
],
},
compiler: {
emotion: true,
// or
styledComponents: true,
},
};
module.exports = nextConfig;
可选地,您还可以通过包含 <TailwindStyle /> 组件来包含 Tailwind 正规化器 + 表单插件。请参阅相关示例。
_document.tsx
import { TailwindStyle } from "stailwc";
export default function App({ Component, pageProps }) {
return (
<>
<TailwindStyle />
<Component {...pageProps} />
</>
);
}
类型
要使类型正常工作,您需要执行以下步骤。您需要将 stailwc.d.ts 添加到源文件夹的根目录。
用法
tw 标签
您可以通过两种方式与 stailwc 交互。第一种是通过 tw JSW 属性,第二种是通过 tw 模板标签。
import { useState } from "react";
export const ColorButton = () => {
const [clicked, setClicked] = useState(0);
return (
<button
tw="p-1 m-4 text-green bg-white hover:(bg-gray text-yellow md:text-red) border-3"
css={clicked % 2 == 0 ? tw`border-green` : tw`border-blue`}
onClick={() => setClicked(clicked + 1)}
>
Clicked {clicked} times
</button>
);
};
组件语法
您还可以使用 tw 模板标签创建样式化组件。这将自动为 emotion 和 styled-components 创建正确的语法。
export const StyledButton = tw.button`p-1 m-4 text-green bg-white hover:(bg-gray text-yellow md:text-red) border-3`;
export const ShadowButton = tw(StyledButton)`shadow-lg`;
示例
提供了针对 emotion 和 styled-components 的示例。您可以通过克隆仓库,并在示例目录下运行 yarn 后跟 yarn dev 来运行它们。您需要首先运行 stailwc。
依赖项
约 ~8–14MB
约 ~205K SLoC