vite + postcss-prefix-selector 增加统一作用域

在前端开发中,随着项目规模的扩大,特别是在使用 微前端 架构后,基座应用和子应用之间的样式冲突和作用域管理成为了一项挑战。为了解决这个问题,我们可以利用 Vite 构建工具和 PostCSS 插件 postcss-prefix-selector,通过增加统一的作用域前缀来有效地隔离样式,确保样式仅在特定组件或模块中生效。本文将详细介绍如何在 Vite 项目中使用 postcss-prefix-selector 插件,实现样式的统一作用域管理。

vite postcss-prefix-selector

1. 安装 Vite 项目

首先,确保你已经创建了一个基于 Vite 的项目。如果还没有,可以通过以下命令进行初始化:

pnpm create vite my-vite-project
cd my-vite-project
npm install

2. 安装 postcss-prefix-selector 插件

在 Vite 项目中使用 PostCSS 插件,需要先安装相关的依赖:

npm install postcss postcss-prefix-selector -D

3. 配置 PostCSS

使用使用以下两种方式之一配置 PostCSS:

  • 在项目根目录下创建 postcss.config.js 文件,配置 PostCSS 插件:
// postcss.config.js

module.exports = {
  plugins: [
    // require('postcss-pxtorem')({
    //   rootValue: 37.5, //1rem的大小
    //   propList: ['*'], //需要转换的属性
    //   selectorBlackList: ['.norem', '.vc-*'], //过滤掉不需要转换的类名
    //   exclude: /node_modules/i, //过滤掉node_modules文件夹下的文件
    // }),
    require('postcss-prefix-selector')({
      prefix: '.my-app',
      transform: function (prefix, selector, prefixedSelector) {
        // 这里可以排除一些特定的选择器
        if (selector === 'body' || selector === 'html') {
          return selector
        }
        return prefixedSelector
      },
      // exclude: ['.global'], // 排除全局样式的前缀添加
    }),
    // 其他 PostCSS 插件...
  ],
}
  • vite.config.js 中配置 PostCSS 插件:
// vite.config.js

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import postCssPrefixSelector from 'postcss-prefix-selector';

export default defineConfig({
  plugins: [
    vue(),
  ],
  css: {
    postcss: {
      plugins: [
        // postCssPxToRem({
        //   rootValue: 37.5, //1rem的大小
        //   propList: ['*'], //需要转换的属性
        //   selectorBlackList: ['.norem', '.vc-*'], //过滤掉不需要转换的类名
        //   exclude: /node_modules/i, //过滤掉node_modules文件夹下的文件
        // }),
        postCssPrefixSelector({
          prefix: '.my-app', // 添加的前缀
          transform(prefix, selector, prefixedSelector) {
            // 这里可以排除一些特定的选择器
            if (selector === 'body' || selector === 'html') {
              return selector
            }
            return prefixedSelector
          },
          // exclude: ['.global'], // 排除全局样式的前缀添加
        }),
      ],
    },
  },
})

4. 选择统一作用域容器

<div id="root"></div> 根节点或 App 组件中添加统一作用域前缀,以下是两种方式二选一即可:

  • index.html 文件中添加样式,添加统一作用域前缀:
<!-- index.html -->

<body>
  <div id="app" class="my-app"></div>
</body>
  • App.vue 文件中添加样式,添加统一作用域前缀:
<!-- App.vue -->

<template>
  <div class="my-app app-container"></div>
</template>

5. 在浏览器中查看效果

现在,你可以在你的样式文件中使用统一的作用域前缀,确保样式只在特定的范围内生效:

开发时样式文件:

/* style.css */

.app-title {
    font-size: 14px;
    font-weight: 500;
}

.app-desc {
    margin: 10px 0;
    line-height: 1.6;
}

浏览器中会看到:

.my-app .app-title {
    font-size: 14px;
    font-weight: 500;
}

.my-app .app-desc {
    margin: 10px 0;
    line-height: 1.6;
}

通过以上步骤,你成功地在 Vite 项目中使用了 postcss-prefix-selector 插件,实现了样式的统一作用域管理。这将有助于降低样式冲突的风险,提高项目的可维护性和可扩展性。


欢迎访问:天问博客

  • 35
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了在Vue3+TS+Vite项目中使用postcss-px-to-viewport-8-plugin,需要按照以下步骤进行操作: 1. 安装依赖: ```shell npm install postcss-px-to-viewport-8-plugin -D ``` 2. 在项目根目录下创建postcss.config.js文件,并添加以下内容: ```javascript const autoprefixer = require('autoprefixer'); const pxtoviewport = require('postcss-px-to-viewport-8-plugin'); module.exports = { plugins: [ autoprefixer(), pxtoviewport({ viewportWidth: 375, // 视窗的宽度,对应设计稿的宽度 viewportHeight: 667, // 视窗的高度,对应设计稿的高度 unitPrecision: 5, // 指定`px`转换为视窗单位值的小数位数 viewportUnit: 'vw', // 指定需要转换成的视窗单位,建议使用vw selectorBlackList: ['.ignore', '.hairlines'], // 指定不转换为视窗单位的类,可以自定义,可以无限添加,建议定义一至两个通用的类名 minPixelValue: 1, // 小于或等于`1px`不转换为视窗单位,你也可以设置为你想要的值 mediaQuery: false // 允许在媒体查询中转换`px` }) ] } ``` 3. 在vite.config.ts文件中添加postcss配置: ```typescript import { defineConfig } from 'vite'; import vue from '@vitejs/plugin-vue'; export default defineConfig({ plugins: [vue()], css: { postcss: { plugins: [ require('autoprefixer'), require('postcss-px-to-viewport-8-plugin')({ viewportWidth: 375, viewportHeight: 667, unitPrecision: 5, viewportUnit: 'vw', selectorBlackList: ['.ignore', '.hairlines'], minPixelValue: 1, mediaQuery: false }) ] } } }); ``` 4. 重启项目即可实现px转vw。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值