我正在尝试在 next.js 14 项目中配置 tailwindcss。我已经在使用 module.css 文件,但是现在我想转向 tailwindcss。我现在不想将旧组件转换为 tailwind,只想使用 tailwindcss 构建新组件。我安装了 tailwindcss 并尝试配置它,但是,我收到以下警告,并且未应用 tailwind 样式
warn - No utility classes were detected in your source files. If this is unexpected, double-check the `content` option in your Tailwind CSS configuration.
warn - https://tailwindcss.com/docs/content-configuration
这是我的 tailwind.config.js 文件
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
'./pages/**/*.{html,js,tsx}',
'./components/**/*.{html,js,tsx}',
],
theme: {
extend: {},
},
plugins: [
require('daisyui'),
],
}
我的文件结构是 FE/src/*
module.css 和 global.css 样式位于 FE/src/styles 下
我也有 postcss.config.js 文件
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
};
这是我的 next.config.mjs 文件
/** @type {import('next').NextConfig} */
const nextConfig = {
images: {
remotePatterns: [
{
protocol: 'https',
hostname: '*',
port: '',
pathname: '/**',
},
{
protocol: 'https',
hostname: 'placehold.co',
port: '',
pathname: '/**',
},
],
},
swcMinify: true,
compiler: {
styledComponents: true, // If you are using styled-components
},
webpack: (config) => {
return config;
}
};
export default nextConfig;
这是我的 package.json 文件
{
"name": "Missari",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint",
},
"dependencies": {
"@babel/core": "^7.26.0",
"@babel/node": "^7.26.0",
"@babel/preset-env": "^7.26.0",
"@prisma/client": "^5.20.0",
"autoprefixer": "^10.4.20",
"axios": "^1.7.7",
"chart.js": "^4.4.4",
"cloudinary": "^2.5.1",
"css-loader": "^7.1.2",
"daisyui": "^4.12.14",
"dompurify": "^3.1.7",
"i": "^0.3.7",
"ical": "^0.8.0",
"next": "^14.2.14",
"npm": "^10.8.3",
"pino-pretty": "^11.2.2",
"postcss": "^8.4.49",
"prisma": "^5.20.0",
"react": "^18.3.1",
"react-chartjs-2": "^5.2.0",
"react-dom": "^18.3.1",
"react-icons": "^5.3.0",
"react-quill": "^2.0.0",
"style-loader": "^4.0.0",
"tailwindcss": "^3.4.15",
"zustand": "^5.0.0-rc.2"
},
"devDependencies": {
"@types/ical": "^0.8.3",
"@types/json-schema": "^7.0.15",
"@types/node": "^20.16.10",
"@types/react": "^18",
"@types/react-dom": "^18",
"eslint": "^8",
"eslint-config-next": "14.2.9",
"ts-node": "^10.9.2",
"typescript": "^5.6.2"
}
}
如何使用 tailwindcss 配置 next.js 项目,同时保留 module.css 样式?
您的
content
路径似乎不正确。你说你的项目结构是FE/src/*
,其中,我假设你的项目根是FE
,你的组件在src
。然后你需要配置你的 content
文件全局,例如:
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
// An example of very specific path.
'./src/path/to/component.js',
// More generalized.
'./src/path/to/components/**/*.{html,js,jsx,tsx}',
],