tailwindcss 使用總結


安裝tailwindcss

  1. 安裝

npm install tailwindcss

  1. 引入
    scss文件引入,並導入main.js生效

引入如下:

// 注入
@tailwind base;
@tailwind components;
@tailwind utilities;

// 正常導入
@import "tailwindcss/base";
@import "tailwindcss/components";
@import "tailwindcss/utilities";

  1. 初始化tailwindcss配置文件

npx tailwindcss init

  1. 裁剪尺寸
    使用purgecss進行裁剪
const path = require("path");

// Purgecss 編譯時裁剪css樣式大小
const purgecss = require("@fullhuman/postcss-purgecss")({
  // Specify the paths to all of the template files in your project
  content: [
    "./src/**/*.html",
    "./src/**/*.vue",
    "./src/**/*.jsx",
    // etc.
  ],

  // Include any special characters you're using in this regular expression
  defaultExtractor: (content) => content.match(/[\w-/:]+(?<!:)/g) || [],
});

module.exports = {
  parser: require("postcss-comment"),
  plugins: [
    // 將tailwindcss作為postcss 插件引入
    require("tailwindcss"),

    require("postcss-import")({
      resolve(id, basedir, importOptions) {
        if (id.startsWith("~@/")) {
          return path.resolve(process.env.UNI_INPUT_DIR, id.substr(3));
        } else if (id.startsWith("@/")) {
          return path.resolve(process.env.UNI_INPUT_DIR, id.substr(2));
        } else if (id.startsWith("/") && !id.startsWith("//")) {
          return path.resolve(process.env.UNI_INPUT_DIR, id.substr(1));
        }
        return id;
      },
    }),

    // 根據平台差異進行不同的樣式處理
    ...(process.env.UNI_PLATFORM !== "h5"
      ? [
          // 使用postcss-class-name 包將小程序不支持的類名轉換為支持的類名
          require("postcss-class-rename")({
            "\\\\:": "--",
            "\\\\/": "--",
          }),
        ]
      : [
          require("autoprefixer")({
            remove: true,
          }),
        ]),
    require("@dcloudio/vue-cli-plugin-uni/packages/postcss"),

    // 添加purgecss處理
    ...(process.env.NODE_ENV === "production" ? [purgecss] : []),
    // purgecss,
  ],
};

上面可以用於uniapp 小程序開發

  1. 針對小程序支持的樣式進行樣式別名
...(process.env.UNI_PLATFORM !== "h5"
      ? [
          // 使用postcss-class-name 包將小程序不支持的類名轉換為支持的類名
          require("postcss-class-rename")({
            "\\\\:": "--",
            "\\\\/": "--",
          }),
        ]
      : [
          require("autoprefixer")({
            remove: true,
          }),
        ]),
    : []
)
  1. webstorm 代碼提示支持
    教程


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM