关于Vue+webpack使用unocss编写CSS,打包后CSS没加前缀

关于Vue+webpack使用unocss编写CSS,打包后CSS没加前缀,封装了一个插件去解决了这个问题

unocss-postcss-webpack-plugin

unocss在vite中使用配置,关于unocss在vite中使用,自行查阅官网 https://unocss.dev/integrations/vite ,vite使用unocss就不会出现这样的问题

js 复制代码
//vite.config.ts
import { fileURLToPath, URL } from 'node:url'
import UnoCSS from 'unocss/vite'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'


// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueJsx(),
    UnoCSS()
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  css: {
    postcss: {
      plugins: [
          require('autoprefixer')({
              overrideBrowserslist: [
                'Android >= 6', 'iOS >= 10', 'ie >= 11', 'Firefox >= 35', 'chrome >= 40','safari >= 6'
              ]
          })
      ]
  },
  }
})

使用

js 复制代码
<template>
  <div>
    <div class="w-200px h-200px bg-gray-400 flex items-center justify-center">vite中使用unocss的写法</div>
    <div class="box">vite中没有使用unocss的写法</div>
  </div>
</template>

<script setup lang="ts">

</script>

<style scoped>
.box{
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: red;
  width: 200px;
  height: 200px;
}
</style>

CSS展示


unocss在webpack中使用配置,关于unocss在webpack中使用,自行查阅官网 https://unocss.dev/integrations/webpack

js 复制代码
const { defineConfig } = require('@vue/cli-service');
const UnoCSS = require('@unocss/webpack').default
module.exports = defineConfig({
    lintOnSave: false,
    transpileDependencies: true,
    configureWebpack: {
        plugins: [
            UnoCSS({})
        ],
    },
    css: {
        loaderOptions: {
            postcss: {
                postcssOptions: {
                  plugins: [
                    require('autoprefixer')({
                        overrideBrowserslist: [
                          'Android >= 6', 'iOS >= 10', 'ie >= 11', 'Firefox >= 35', 'chrome >= 40','safari >= 6'
                        ]
                    })
                  ],
                },
            },
        }
    },
});
js 复制代码
<template>
  <div>
    <div class="w-200px h-200px bg-gray-400 flex items-center justify-center">webpack使用unocss的写法</div>
    <div class="box">webpack没有使用unocss的写法</div>
  </div>
</template>

<script setup lang="ts">

</script>

<style scoped>
.box{
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: red;
  width: 200px;
  height: 200px;
}
</style>

CSS展示


比较发现,在webpack中,unocss写法的css样式没有添加css前缀,详情可看这个issue https://github.com/unocss/unocss/issues/386

使用unocss-postcss-webpack-plugin插件 webpack中,unocss 添加 postcss 编译 ,配合@unocss/webpack使用,前提已在项目中配置了unocss相关配置,以及项目中的css是单独打包成独立的css文件

install

复制代码
npm i unocss-postcss-webpack-plugin -D

使用

js 复制代码
//vue.config.js
const { defineConfig } = require('@vue/cli-service');
const UnoCSS = require('@unocss/webpack').default
const unocssPostcssWebpackPlugin=require('unocss-postcss-webpack-plugin')
module.exports = {
     configureWebpack: {
        plugins: [
            UnoCSS({}),
            unocssPostcssWebpackPlugin(), 
        ],
    },
    css:{
        loaderOptions: {
            postcss: {
                postcssOptions: {
                  plugins: [
                    require('autoprefixer')({
                        overrideBrowserslist: [
                          'Android >= 6', 'iOS >= 10', 'ie >= 11', 'Firefox >= 35', 'chrome >= 40','safari >= 6'
                        ]
                    })
                  ],
                },
            },
        },
        extract:true,//这个主要是设置单独打包css,
    }
}

CSS展示

unocss编写的css样式也加上了css前缀

props

Prop Type Default description required
overrideBrowserslist Array 'Android \>= 6', 'iOS \>= 10', 'ie \>= 1,'Firefox \>= 35', 'chrome \>= 40','safari \>= 6' browserslist false
相关推荐
the_answer19 小时前
Webpack vs Vite 深度对比分析
前端·webpack
DarkLONGLOVE21 小时前
快速上手 Pinia!Vue3 极简状态管理使用教程
javascript·vue.js
宸翰1 天前
解决 uni-app App 端 vue-i18n 占位符丢失:封装跨端可用的 tf 格式化方法
前端·vue.js·uni-app
用户2136610035721 天前
VueRouter进阶-动态路由与嵌套路由
前端·vue.js
暴走的小呆2 天前
Vue 2 中 Object 的变化侦测:从 getter/setter 到 Dep、Watcher、Observer
vue.js
英勇无比的消炎药2 天前
TinyVue v-auto-tip: 文本超长自动提示的优雅方案
vue.js
时光足迹2 天前
腾讯云 TRTC UniApp SDK 从入门到上线
前端·vue.js·uni-app
时光足迹2 天前
uni-app 里把加密视频嵌入页面播放?我分析了 4 种方案,只有 1 种接近完美
前端·vue.js·uni-app
时光足迹2 天前
JPush UniApp UTS 插件完全参考手册:API、事件与厂商通道一网打尽
vue.js·ios·uni-app
时光足迹2 天前
极光推送全攻略(下):uni-app 代码实现与 iOS 排查实战
vue.js·ios·uni-app