You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Bun中使用Tailwind CSS?基于Bun.sh创建React应用的集成方案问询

用Bun搭建React + Tailwind CSS项目的可行方案

我完全懂你想用Bun快速搭React项目,同时用上Tailwind做样式开发的需求——虽然目前Tailwind没有针对Bun的官方集成指南,但手动配置的方法其实和传统npm/yarn流程大同小异,亲测可行!下面一步步来操作:

1. 用Bun初始化React项目

首先用Bun的官方命令生成React项目骨架:

bun create react my-bun-react-tailwind
cd my-bun-react-tailwind

2. 安装Tailwind相关开发依赖

Tailwind需要搭配PostCSS和Autoprefixer一起工作,直接用Bun安装它们:

bun add -D tailwindcss postcss autoprefixer

3. 生成配置文件

运行初始化命令,自动生成tailwind.config.js和postcss.config.js两个核心配置文件:

bunx tailwindcss init -p

4. 配置Tailwind的内容扫描范围

打开生成的tailwind.config.js,修改content数组,确保Tailwind能扫描到项目里所有的React组件和页面文件:

/** @type {import('tailwindcss').Config} */
export default {
  content: [
    "./index.html",
    "./src/**/*.{js,ts,jsx,tsx}", // 匹配src目录下所有后缀为js/ts/jsx/tsx的文件
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}

5. 在CSS中引入Tailwind指令

找到src/index.css,把里面的默认样式替换成Tailwind的基础指令:

@tailwind base;
@tailwind components;
@tailwind utilities;

6. 测试样式是否生效

修改src/App.jsx,写个带Tailwind样式的组件试试:

function App() {
  return (
    <div className="min-h-screen bg-gradient-to-br from-blue-500 to-purple-600 flex items-center justify-center">
      <h1 className="text-4xl font-bold text-white">Bun + React + Tailwind 搞定啦 🚀</h1>
    </div>
  );
}

export default App;

7. 启动项目验证效果

用Bun启动开发服务器:

bun run dev

打开浏览器访问本地地址,就能看到带有Tailwind渐变背景和文字样式的页面了。

额外说明

虽然你提到Bun目前还不适合生产环境,但这个集成方式是稳定的——本质上就是用Bun替代npm/yarn做包管理和脚本执行,Tailwind的核心配置逻辑和传统方式完全一致。如果遇到样式不生效的问题,优先检查tailwind.config.js里的content路径是否正确,或者有没有遗漏组件文件的后缀。

内容的提问来源于stack exchange,提问作者Tayfun Erbilen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 20:24:07