HBuilderX创建uniapp项目使用 tailwindcss

文章目录

一、创建package.json文件

js 复制代码
{
  "devDependencies": {
    "autoprefixer": "9",
    "postcss": "7",
    "postcss-rem-to-responsive-pixel": "^5.1.3",
    "tailwindcss": "npm:@tailwindcss/postcss7-compat",
    "weapp-tailwindcss-webpack-plugin": "^1.6.8",
    "webpack": "npm:webpack@webpack-4"
  }
}

二、打开终端 yarn / npm 安装依赖

三、创建 vue.config.js文件

js 复制代码
// 为了 tailwindcss jit 开发时的热更新
if (process.env.NODE_ENV === "development") {
  process.env.TAILWIND_MODE = "watch";
}

const {
  UniAppWeappTailwindcssWebpackPluginV4,
} = require("weapp-tailwindcss-webpack-plugin");

/**
 * @type {import('@vue/cli-service').ProjectOptions}
 */
const config = {
  //....
  configureWebpack: {
    plugins: [new UniAppWeappTailwindcssWebpackPluginV4()],
  },
  //....
};

module.exports = config;

四、创建postcss.config.js文件

js 复制代码
const path = require("path");

module.exports = {
  plugins: [
    require("autoprefixer")({
      remove: process.env.UNI_PLATFORM !== "h5",
    }),
    require("tailwindcss")({
      config: path.resolve(__dirname, "./tailwind.config.js"),
    }),
    // rem 转 rpx
    require("postcss-rem-to-responsive-pixel/postcss7")({
      rootValue: 32,
      propList: ["*"],
      transformUnit: "rpx",
    }),
  ],
};

五、创建tailwind.config.js文件

js 复制代码
const path = require("path");
const resolve = (p) => {
	return path.resolve(__dirname, p);
};

/** @type {import('@types/tailwindcss/tailwind-config').TailwindConfig} */
module.exports = {
	mode: "jit",
	purge: {
		content: [
			resolve("./index.html"),
			resolve("./pages/**/*.{vue,js,ts,jsx,tsx,wxml}"),
		],
	},
	darkMode: false, // or 'media' or 'class'
	theme: {
		colors: {
			white: '#ffffff',
			black: '#000000',
			main: '#333333',
			content: '#666666',
			muted: '#999999',
			light: '#e5e5e5',
			primary: {
				DEFAULT: '#4173ff'
			},
			success: '#5ac725',
			warning: '#f9ae3d',
			error: '#f56c6c',
			info: '#909399',
			page: '#f6f6f6'
		},
		fontSize: {
			xs: '24rpx',
			sm: '26rpx',
			base: '28rpx',
			lg: '30rpx',
			xl: '32rpx',
			'2xl': '34rpx',
			'3xl': '38rpx',
			'4xl': '40rpx',
			'5xl': '44rpx'
		}
	},
	variants: {},
	plugins: [],
	corePlugins: {
		preflight: false,
	},
};
//自定义配置

六、App.vue文件的style中引入tailwindcss

js 复制代码
<style>
	/*每个页面公共css */
	@import "tailwindcss/base";
	@import "tailwindcss/utilities";
</style>

相关推荐
断墨先生2 小时前
uniapp—android原生插件开发(3Android真机调试)
android·uni-app
guai_guai_guai4 小时前
uniapp
前端·javascript·vue.js·uni-app
阿伟来咯~9 小时前
一些 uniapp相关bug
uni-app·bug
瑶琴AI前端13 小时前
uniapp组件实现省市区三级联动选择
java·前端·uni-app
mosen86813 小时前
Uniapp去除顶部导航栏-小程序、H5、APP适用
vue.js·微信小程序·小程序·uni-app·uniapp
尚梦21 小时前
uni-app 封装刘海状态栏(适用小程序, h5, 头条小程序)
前端·小程序·uni-app
尚学教辅学习资料1 天前
基于SSM+uniapp的营养食谱系统+LW参考示例
java·uni-app·ssm·菜谱
Bessie2341 天前
微信小程序eval无法使用的替代方案
微信小程序·小程序·uni-app
qq22951165022 天前
小程序Android系统 校园二手物品交换平台APP
微信小程序·uni-app
qq22951165022 天前
微信小程序uniapp基于Android的流浪动物管理系统 70c3u
微信小程序·uni-app