浏览器嵌套兼容处理

背景

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

相关推荐
喵爸的小作坊2 小时前
StreamPanel:一个让 SSE 调试不再痛苦的 Chrome 插件
前端·后端·http
star learning white2 小时前
xm C语言12
服务器·c语言·前端
tabzzz2 小时前
大道至简:万字漫谈前端性能监控
前端·javascript·性能优化
0思必得02 小时前
[Web自动化] CSS基础概念和介绍
前端·css·python·自动化·html·web自动化
小胖霞2 小时前
全栈系列(15)github Actions自动化部署前端vue
前端·node.js·github
未来魔导2 小时前
基于 Gin 框架的 大型 Web 项目推荐架构目录结
前端·架构·gin
foundbug9993 小时前
Modbus协议C语言实现(易于移植版本)
java·c语言·前端
Luna-player3 小时前
在前端中list.map的用法
前端·数据结构·list
用户47949283569153 小时前
面试官问 React Fiber,这一篇文章就够了
前端·javascript·react.js
小徐_23333 小时前
Gemini 3做粒子交互特效很出圈?拿 TRAE SOLO 来实现一波!
前端·ai编程·trae