Tailwind CSS v4.0 升级与 Astro 5.2 项目迁移记录

news2025/2/6 10:03:52

本文博客链接 https://ysx.cosine.ren/tailwind-update-v4-migrate

自用小记。

Tailwind CSS v4.0 - Tailwind CSS

  • 新的高性能引擎 - 完整构建的速度速度快 5 倍,增量构建的速度快于 100 倍以上 —— 以微秒为单位进行测量。
  • 为现代 Web 设计 - 建立在前沿的 CSS 特性之上,使用级联层、 @propertycolor-mix() 等前沿 CSS 特性注册自定义属性。
  • 简化的安装 - 较少的依赖项,零配置,只需要 CSS 文件中的一行代码。
  • 第一方 Vite 插件 - 紧密集成,可实现最高性能和最低配置。
  • 自动内容检测 - 所有模板文件都是自动发现的,无需配置。
  • 内置导入支持 - 无需额外工具即可捆绑多个 CSS 文件。
  • CSS-First 配置 - 重新设计的开发人员体验,您可以直接在 CSS 而不是 JavaScript 配置文件中自定义和扩展框架。
  • CSS 主题变量 - 您的所有 design tokens 都以原生 CSS 变量的形式暴露,因此您可以在任何地方访问它们。
  • 动态实用程序值和变体 - 不再需要猜测间距中存在哪些值,也不再需要为基本数据属性等扩展配置。
  • 现代化的 P3 调色板 - 重新设计的更加生动的调色板,充分利用了现代展示技术。
  • 容器查询 - 可使用容器查询根据容器大小为元素设计样式,不再需要插件
  • 新的 3D 变换实用程序 - 直接在 HTML 中的 3D 空间中变换元素,添加了用于进行 3D 变换的 API,例如 rotate-x-*rotate-y-*scale-z-*translate-z-* 和更多。
  • 扩展的梯度 API - 径向和圆锥梯度,插值模式等。现在,线性梯度将角度作为值支持,因此您可以使用bg-linear-45等实用程序以 45 度角创建梯度。将bg-gradient-*重命名为bg-linear-*
  • @starting-style support - 新的starting变体增加了对新的 CSS @starting-style 功能的支持,从而在首次显示元素时可以过渡元素属性:
  • not-* variant - 仅在元素不匹配另一个变体,自定义选择器或媒体或功能查询时样式。
  • 甚至更多的新实用程序和变体,包括对color-scheme的支持, field-sizing ,复杂的阴影, inert等。

更多新变体

  • 新的 inset-shadow-*inset-ring-* 实用程序 - 使得在一个元素上最多可以堆叠四层盒子阴影。
  • 新的 field-sizing 实用程序 - 用于自动化的文本列,而无需编写单行 JavaScript。
    • 使用field-sizing-content,允许表单控件根据内容调整其大小
    • 使用field-sizing-fixed 固定尺寸:
  • 新的 color-scheme - 参考 使用 light-dark() 的 CSS 配色方案相关颜色
  • 新的 font-stretch 实用程序 - 用于仔细调整支持不同宽度的可变字体。
  • 新的 inert 变体 - inert变体使您可以样式的元素标记为inert属性,这对于添加视觉提示很有用,可以清楚地表明内容的各个部分不是交互式的。
  • 新的 nth-* 变体 - 使用 nth-*nth-last-* 变体来根据孩子在列表中的位置设置样式,您可以在默认情况下将您想要的任何数字传递给这些数字,并使用任意值对 nth-[2n+1_of_li] 等更复杂的表达式使用任意值。 eg: nth-3:underline nth-last-5:underline nth-last-of-type-6:underline
    • 有关可用伪类变体的完整列表,请参见伪类参考资料。
  • 新的 in-* 变体 - 非常类似于 group-* ,但无需 group 类。
    • in-* 变体响应任何父代的状态变化,所以如果你想要更精细的控制,就需要使用 group 来代替。
  • 支持:popover-open - 使用现有的 open 式变体也针对 open popovers.
  • 新的后代变体- 用于造型所有后代元素,无论好坏。
    • * 一样,** 变体也可用于为元素的子元素设置样式。主要区别在于 ** 将对所有后代应用样式,而不仅仅是直接子代。当你将它与另一个变体结合使用,以缩小选择范围时,这一点尤其有用

迁移记录

对于现有项目,我们发布了一份全面的升级指南,并构建了一个自动升级工具,以尽快和无痛地使用最新版本。

一个小项目先试试水,执行自动化迁移指令。

npx @tailwindcss/upgrade@next

迁移输出如下

≈ tailwindcss v4.0.2
│ Searching for CSS files in the current directory and its subdirectories…
│ ↳ Linked `./tailwind.config.js` to `./src/styles/theme/shadcn.css`
│ Migrating JavaScript configuration files…
│ ↳ The configuration file at `./tailwind.config.js` could not be automatically migrated to the new CSS configuration format, so your CSS has been
│   updated to load your existing configuration file.
│ Migrating templates…
│ ↳ Migrated templates for configuration file: `./tailwind.config.js`
│ Migrating stylesheets…
│ ↳ Migrated stylesheet: `./src/styles/theme/shadcn.css`
│ Migrating PostCSS configuration…
│ ↳ Installed package: `@tailwindcss/postcss`
│ ↳ Removed package: `autoprefixer`
│ ↳ Removed package: `postcss-import`
│ ↳ Migrated PostCSS configuration: `./postcss.config.js`
│ Updating dependencies…
│ ↳ Updated package: `prettier-plugin-tailwindcss`
│ ↳ Updated package: `tailwindcss`
│ Verify the changes and commit them to your repository.
npm verb exit 0
npm info ok

可以看出自动完成了大部分工作,包括 flex-grow -> grow / flex-shrink -> shrink 等类名更新,这就是成功迁移了,嗯,貌似表面上没出什么意外,再试试另一个 astro 5.1 项目的升级,先莽一下更新脚本。

% sudo npx @tailwindcss/upgrade@next
npm verb cli /usr/local/bin/node /usr/local/lib/node_modules/npm/bin/npm-cli.js
npm info using npm@10.5.0
npm info using node@v20.12.2
npm verb title npm exec @tailwindcss/upgrade@next
npm verb argv "exec" "--" "@tailwindcss/upgrade@next"
npm verb logfile logs-max:10 dir:/Users/cosine/.npm/_logs/2025-02-03T10_01_41_235Z-
npm verb logfile /Users/cosine/.npm/_logs/2025-02-03T10_01_41_235Z-debug-0.log
npm sill logfile start cleaning logs, removing 1 files
npm sill logfile done cleaning log files
npm http fetch GET 200 https://registry.npmjs.org/@tailwindcss%2fupgrade 489ms (cache updated)
≈ tailwindcss v4.0.2

│ Searching for CSS files in the current directory and its subdirectories…

│ ↳ Linked `./tailwind.config.mjs` to `./src/styles/global/tailwind.css`

│ Migrating JavaScript configuration files…

│ ↳ Could not load the configuration file: undefined is not a function

npm verb exit 1
npm verb code 1

很好,报错 Could not load the configuration file: undefined is not a function , 查了下是有插件不兼容。(Could not load the configuration file: v is not a function · tailwindlabs/tailwindcss · Discussion #15781 · GitHub)

看了一下之前迁移成功的项目里有这两个插件:tailwindcss-animate@tailwindcss/typography

出问题的 astro 项目里多了一个 tailwind-clip-path 插件,注释后就正常执行了。

// tailwind.config.js
module.exports = {
  plugins: [
    require("@tailwindcss/container-queries"),
    require("tailwindcss-animate"),
    // require("tailwind-clip-path"),
    require("@tailwindcss/typography"),
  ],
};

然后起一下试试:

> cos-space@0.0.1 dev /Users/cosine/Documents/Programming/cos-space
> astro dev

Package subpath './nesting/index.js' is not defined by "exports" in /Users/cosine/Documents/Programming/cos-space/node_modules/.pnpm/@astrojs+tailwind@5.1.4_astro@5.1.1_@types+node@22.10.5_jiti@2.4.2_rollup@4.29.1_terser@5.37._5qh5alcn5ztelwntajptz64i4q/node_modules/tailwindcss/package.json imported from /Users/cosine/Documents/Programming/cos-space/node_modules/.pnpm/@astrojs+tailwind@5.1.4_astro@5.1.1_@types+node@22.10.5_jiti@2.4.2_rollup@4.29.1_terser@5.37._5qh5alcn5ztelwntajptz64i4q/node_modules/@astrojs/tailwind/dist/index.js
  Stack trace:
    at exportsNotFound (node:internal/modules/esm/resolve:304:10)
    at packageResolve (node:internal/modules/esm/resolve:837:14)
    at defaultResolve (node:internal/modules/esm/resolve:1157:11)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:359:25)
    at ModuleLoader.import (node:internal/modules/esm/loader:322:34)
 ELIFECYCLE  Command failed with exit code 1.

很好,我就知道 astro 里的没那么简单迁移,查一下 astro/packages/integrations/tailwind/CHANGELOG.md at main · withastro/astro · GitHub Tailwind CSS 现在提供了一个 Vite 插件,这是在 Astro 中使用 Tailwind 4 的首选方法。卸载原来的 @astrojs/tailwind ,然后按照 tailwind 文档进行手动安装。

嗯,还得升一下 astro 版本到 Astro 5.2 | Astro

npx @astrojs/upgrade
pnpm rm @astrojs/tailwind
pnpm i tailwindcss @tailwindcss/vite

成功啦,对比一下配置文件

diff --git a/astro.config.mjs b/astro.config.mjs
index bf8e9b1..20513bd 100644
--- a/astro.config.mjs
+++ b/astro.config.mjs
@@ -1,22 +1,17 @@
 // @ts-check
 import react from '@astrojs/react';
-import tailwind from '@astrojs/tailwind';
 import { siteConfig } from './src/constants/site-config';
 import icon from 'astro-icon';
 import { defineConfig } from 'astro/config';
 import svgr from 'vite-plugin-svgr';
 import umami from '@yeskunall/astro-umami';
+import tailwindcss from '@tailwindcss/vite';

 // https://astro.build/config
 export default defineConfig({
   site: siteConfig.site,
   integrations: [
     react(),
-    tailwind({
-      // 允许在 Tailwind 的语法基础上编写嵌套的 CSS 声明
-      nesting: true,
-      applyBaseStyles: false,
-    }),
     icon({
       include: {
         gg: ['*'],
@@ -35,7 +30,7 @@ export default defineConfig({
     enabled: true,
   },
   vite: {
-    plugins: [svgr()],
+    plugins: [svgr(), tailwindcss()],
   },
   trailingSlash: 'never',
 });
diff --git a/tailwind.config.mjs b/tailwind.config.mjs
index a7de2d0..28e5899 100644
--- a/tailwind.config.mjs
+++ b/tailwind.config.mjs
@@ -140,10 +140,5 @@ export default {
       },
     },
   },
-  plugins: [
-    require('@tailwindcss/container-queries'),
-    require('tailwindcss-animate'),
-    require('tailwind-clip-path'),
-    require('@tailwindcss/typography'),
-  ],
+  plugins: [require('@tailwindcss/container-queries'), require('tailwindcss-animate'), require('@tailwindcss/typography')],
 };

原来的插件失效了,@plugin 引入也不行,不过可以直接写在 css 里问题不大,还有就是 layer 层级问题会导致一些样式继承的问题,总体来说这俩小项目迁起来倒是没什么问题,后面迁移就持续更新这篇文章吧。

总结升级问题解决方案

问题一:Astro PostCSS 配置冲突

Error: Package subpath './nesting/index.js' is not defined by "exports" in...

应对策略

  1. 升级 Astro 至 5.2+ 版本 npx @astrojs/upgrade
  2. 卸载 @astrojs/tailwind
  3. 清理旧版 PostCSS 插件依赖
  4. 按照 tailwind 文档进行手动安装,安装 tailwindcss @tailwindcss/vite / astro add tailwind

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2293720.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

K8S ReplicaSet 控制器

一、理论介绍 今天我们来实验 ReplicaSet 控制器(也叫工作负载)。官网描述如下: 1、是什么? ReplicaSet 副本集, 维护一组稳定的副本 Pod 集合。 2、为什么需要? 解决 pod 被删除了,不能自我恢…

基于springboot校园点歌系统

基于Spring Boot的校园点歌系统是一种专为校园场景设计的音乐点播平台,它能够丰富学生的校园生活,提升学生的娱乐体验。以下是对该系统的详细介绍: 一、系统背景与意义 在校园环境中,学生们对于音乐有着浓厚的兴趣,传…

【R语言】数据操作

一、查看和编辑数据 1、查看数据 直接打印到控制台 x <- data.frame(a1:20, b21:30) x View()函数 此函数可以将数据以电子表格的形式进行展示。 用reshape2包中的tips进行举例&#xff1a; library("reshape2") View(tips) head()函数 查看前几行数据&…

【C++】2.高并发内存池 -- 如何设计一个定长内存池

博客主题&#xff1a;如何设计一个定长内存池 个人主页&#xff1a;https://blog.csdn.net/sobercq CSDN专栏&#xff1a;https://blog.csdn.net/sobercq/category_12884309.html Gitee链接&#xff1a;https://gitee.com/yunshan-ruo/high-concurrency-memory-pool 文章目录 前…

Redis --- 使用Feed流实现社交平台的新闻流

要实现一个 Feed 流&#xff08;类似于社交媒体中的新闻流&#xff09;&#xff0c;通常涉及以下几个要素&#xff1a; 内容发布&#xff1a;用户发布内容&#xff08;例如文章、状态更新、图片等&#xff09;。内容订阅&#xff1a;用户可以订阅其他用户的内容&#xff0c;获…

游戏引擎学习第88天

仓库:https://gitee.com/mrxiao_com/2d_game_2 调查碰撞检测器中的可能错误 在今天的目标是解决一个可能存在的碰撞检测器中的错误。之前有人提到在检测器中可能有一个拼写错误&#xff0c;具体来说是在测试某个变量时&#xff0c;由于引入了一个新的变量而没有正确地使用它&…

c++中priority_queue的应用及模拟实现

1.介绍 priority_queue 是一种数据结构&#xff0c;它允许你以特定的顺序存储和访问元素。在 C 标准模板库&#xff08;STL&#xff09;中&#xff0c;priority_queue 是一个基于容器适配器的类模板&#xff0c;它默认使用 std::vector 作为底层容器&#xff0c;并且默认使用最…

游戏引擎 Unity - Unity 设置为简体中文、Unity 创建项目

Unity Unity 首次发布于 2005 年&#xff0c;属于 Unity Technologies Unity 使用的开发技术有&#xff1a;C# Unity 的适用平台&#xff1a;PC、主机、移动设备、VR / AR、Web 等 Unity 的适用领域&#xff1a;开发中等画质中小型项目 Unity 适合初学者或需要快速上手的开…

【Elasticsearch】geohex grid聚合

在 Elasticsearch 中&#xff0c;地理边界过滤是一种用于筛选地理数据的技术&#xff0c;它可以根据指定的地理边界形状&#xff08;如矩形、多边形等&#xff09;来过滤符合条件的文档。这种方法在地理空间数据分析中非常有用&#xff0c;尤其是在需要将数据限制在特定地理区域…

crewai框架第三方API使用官方RAG工具(pdf,csv,json)

最近在研究调用官方的工具&#xff0c;但官方文档的说明是在是太少了&#xff0c;后来在一个视频里看到了如何配置&#xff0c;记录一下 以PDF RAG Search工具举例&#xff0c;官方文档对于自定义模型的说明如下&#xff1a; 默认情况下&#xff0c;该工具使用 OpenAI 进行嵌…

算法 哈夫曼树和哈夫曼编码

目录 前言 一&#xff0c;二进制转码 二&#xff0c;哈夫曼编码和哈夫曼树 三&#xff0c;蓝桥杯 16 哈夫曼树 总结 前言 这个文章需要有一定的树的基础&#xff0c;没学过树的伙伴可以去看我博客树的文章 当我们要编码一个字符串转成二进制的时候&#xff0c;我们要怎么…

Sumatra PDF:小巧免费,满足多样阅读需求

Sumatra PDF是一款完全免费的本地阅读器软件&#xff0c;以小巧的体积和全面的功能受到用户青睐。如今&#xff0c;它已经更新到3.3版本&#xff0c;带来了更多实用功能&#xff0c;尤其是新增的注释功能&#xff0c;值得我们再次关注。 软件特色 轻量级体积&#xff1a;压缩…

TiDB 分布式数据库多业务资源隔离应用实践

导读 随着 TiDB 在各行业客户中的广泛应用 &#xff0c;特别是在多个业务融合到一套 TiDB 集群中的场景&#xff0c;各企业对集群内多业务隔离的需求日益增加。与此同时&#xff0c;TiDB 在多业务融合场景下的资源隔离方案日趋完善&#xff0c;详情可参考文章 《你需要什么样的…

105,【5】buuctf web [BJDCTF2020]Easy MD5

进入靶场 先输入试试回显 输入的值成了password的内容 查看源码&#xff0c;尝试得到信息 什么也没得到 抓包&#xff0c;看看请求与响应里有什么信息 响应里得到信息 hint: select * from admin where passwordmd5($pass,true) 此时需要绕过MD5&#xff08;&#xff09;函…

BFS(广度优先搜索)——搜索算法

BFS&#xff0c;也就是广度&#xff08;宽度&#xff09;优先搜索&#xff0c;二叉树的层序遍历就是一个BFS的过程。而前、中、后序遍历则是DFS&#xff08;深度优先搜索&#xff09;。从字面意思也很好理解&#xff0c;DFS就是一条路走到黑&#xff0c;BFS则是一层一层地展开。…

33.Word:国家中长期人才发展规划纲要【33】

目录 NO1.2样式​ NO3​ 图表 ​ NO4.5.6​ 开始→段落标记视图→导航窗格→检查有无遗漏 NO1.2样式 F12/另存为&#xff1a;Word.docx&#xff1a;考生文件夹样式的复制样式的修改 样式的应用&#xff08;没有相似/超级多的情况下&#xff09;——替换 [ ]通配符&#x…

gym-anytrading

参考&#xff1a;https://github.com/upb-lea/gym-electric-motor AnyTrading 是一组基于 reinforcement learning (RL) 的 trading algorithms&#xff08;交易算法&#xff09;的 OpenAI Gym 环境集合。 该项目主要用于foreign exchange (FOREX) 和 stock markets (股票市场)…

如何自定义软件安装路径及Scoop包管理器使用全攻略

如何自定义软件安装路径及Scoop包管理器使用全攻略 一、为什么无法通过WingetUI自定义安装路径&#xff1f; 问题背景&#xff1a; WingetUI是Windows包管理器Winget的图形化工具&#xff0c;但无法直接修改软件的默认安装路径。原因如下&#xff1a; Winget设计限制&#xf…

私有化部署 DeepSeek + Dify,构建你的专属私人 AI 助手

私有化部署 DeepSeek Dify&#xff0c;构建你的专属私人 AI 助手 概述 DeepSeek 是一款开创性的开源大语言模型&#xff0c;凭借其先进的算法架构和反思链能力&#xff0c;为 AI 对话交互带来了革新性的体验。通过私有化部署&#xff0c;你可以充分掌控数据安全和使用安全。…

Java 进阶 01 —— 5 分钟回顾一下 Java 基础知识

Java 进阶 01 —— 5 分钟回顾一下 Java 基础知识 Java 生态圈Java 跨平台的语言 Java 虚拟机规范JVM 跨语言的平台多语言混合编程两种架构 举例 JVM 的生命周期 虚拟机的启动虚拟机的执行虚拟机的退出 JVM 发展历程 Sun Classic VMExact VMHotSpotBEA 的 JRockitIBM 的 J9 …