12 个版本 (重大更新)
| 0.17.0 | 2023年6月9日 |
|---|---|
| 0.15.0 | 2023年2月20日 |
| 0.12.0 | 2022年12月27日 |
| 0.9.0 | 2022年11月5日 |
#6 in #nextjs
每月下载 103 次
在 2 个 crate 中使用 (通过 tailwind-parse)
10KB
198 行
stailwc (快速 Tailwind 编译器)
这是一个实验性的 SWC 编译器,可以将编译时 Tailwind 宏引入 SWC (和 nextjs) - 类似 twin macro。目标是提供与基于 babel 的替代方案相当好的编译时舒适性和灵活性,同时性能更佳。支持 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。
依赖关系
约 2MB
约 44K SLoC