浏览器嵌套兼容处理

背景

使用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可能多余未验证

相关推荐
xiaoxiao无脸男9 分钟前
纯css:一个好玩的按钮边框动态动画
前端·css·css3
rookie_fly29 分钟前
基于Vue的数字输入框指令
前端·vue.js·设计模式
元直数字电路验证36 分钟前
ASP.NET Core Web APP(MVC)开发中无法全局配置 NuGet 包,该怎么解?
前端·javascript·ui·docker·asp.net·.net
rexling11 小时前
【Spring Boot】Spring Boot解决循环依赖
java·前端·spring boot
我有一棵树1 小时前
Vue 项目中全局样式的正确写法:不要把字体和主题写在 #app 上
前端·javascript·vue.js
Luna-player2 小时前
npm : 无法加载文件 C:\Program Files\nodejs\npm.ps1,因为在此系统上禁止运行脚本,解决方法
前端·npm·node.js
悢七2 小时前
windows npm打包无问题,但linux npm打包后部分样式缺失
linux·前端·npm
Felicity_Gao2 小时前
uni-app App升级功能实现
前端·学习·uni-app
CS Beginner2 小时前
【搭建】个人博客网站的搭建
java·前端·学习·servlet·log4j·mybatis
老程序员刘飞4 小时前
node.js 和npm 搭建项目基本流程
前端·npm·node.js