浏览器嵌套兼容处理

背景

使用tailwindcss 替代sass less 使用嵌套语法如何处理

问题

1. 内部系统win7操作系统 chrome最高支持108.

2. caniuse 图

css 复制代码
.a{
   .b
}

.a{
    & .b{
    }
}

.a{
    &.b{
    }
}

@nest .a .b { color: red; }

分析

  1. postcss-preset-env

里面用到 postcss-nesting

  1. postcss-nested 兼容 类Sass嵌套

  2. postcss-nesting

    处理 标准Css嵌套

  3. @tailwindcss/nesting

  4. tailwindcss/nesting

js 复制代码
    1.引用 postcss-nested
    const _postcss = /*#__PURE__*/ _interop_require_default(require("postcss"));
const _postcssnested = /*#__PURE__*/ _interop_require_default(require("postcss-nested"));
    2.处理 screen apply 兼容tailwindcss
       root.walkAtRules("screen", (rule)=>{
            rule.name = "media";
            rule.params = `screen(${rule.params})`;
        });
        root.walkAtRules("apply", (rule)=>{
            rule.before(_postcss.default.decl({
                prop: "__apply",
                value: rule.params,
                source: rule.source
            }));
            rule.remove();
        });
    
    3.调用 postcss-nested
     (0, _postcss.default)([
            plugin
        ]).process(root, result.opts).sync();
    4。清空脏修改
          root.walkDecls("__apply", (decl)=>{
            decl.before(_postcss.default.atRule({
                name: "apply",
                params: decl.value,
                source: decl.source
            }));
            decl.remove();
        });

1.node_modules/tailwindcss/nesting' is not supported resolving ES modules imported esm

使用tailwindcss/nesting/index

2.postcss-nested 与 tailwindcss 冲突

使用tailwindcss/postcss-nested

最终方案

js 复制代码
export default {
  plugins: [
    dep.postcssImport(),
    dep.tailwindcssNesting(dep.postcssNesting),
    dep.tailwindcssNesting(dep.postcssNested),
    dep.tailwindcss(
      dep.path.resolve(path.config, "./style/postcss/tailwindcss/base.ts")
    ),
    dep.postcssPresetEnv({
      stage: 0,
      features: { "nesting-rules": false },
    }),
    placeCssValue(),
    dep.autoprefixer(),
  ],
};

注意插件执行顺序 导入->nest->tw->preset->autoprefix autoprefix可能多余未验证

相关推荐
疯子****1 天前
【无标题】
前端·clawdbot
RichardLau_Cx1 天前
【保姆级实操】MediaPipe SDK/API 前端项目接入指南(Web版,可直接复制代码)
前端·vue·react·webassembly·mediapipe·手部追踪·前端计算机视觉
不爱写程序的东方不败1 天前
APP接口测试流程实战Posman+Fiddler
前端·测试工具·fiddler
晚霞的不甘1 天前
Flutter for OpenHarmony构建全功能视差侧滑菜单系统:从动效设计到多页面导航的完整实践
前端·学习·flutter·microsoft·前端框架·交互
黎子越1 天前
python相关练习
java·前端·python
北极糊的狐1 天前
若依项目vue前端启动键入npm run dev 报错:不是内部或外部命令,也不是可运行的程序或批处理文件。
前端·javascript·vue.js
XRJ040618xrj1 天前
Nginx下构建PC站点
服务器·前端·nginx
We་ct1 天前
LeetCode 289. 生命游戏:题解+优化,从基础到原地最优
前端·算法·leetcode·矩阵·typescript
有诺千金1 天前
VUE3入门很简单(4)---组件通信(props)
前端·javascript·vue.js
2501_944711431 天前
Vue-路由懒加载与组件懒加载
前端·javascript·vue.js