浏览器嵌套兼容处理

背景

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

相关推荐
zhifou12345618 分钟前
Vue基础(二)
前端·javascript·vue.js
两只羊ovo26 分钟前
手写一个最小版 Claude Code:从任务拆解到 Agent Loop 转起来
前端
给个offer养家糊口34 分钟前
抽离 elpis npm 包
前端
默_笙36 分钟前
🙃 我让爬虫终于看到了我的网站,后端同事说"这也行?"(下):App Router 全栈实战
前端·javascript
葡萄城技术团队42 分钟前
一个单元格放置两个日期选择器:用 SpreadJS CellButtons 录入日期范围
前端
马可家的菠萝2 小时前
自动保存已经有了,为什么笔记软件还需要“历史版本”?
前端·后端·架构
半个落月2 小时前
从 "use client" 到 Route Handler:用 Todos 理解 Next.js 水合与全栈请求
前端·react.js·next.js
qq_452396232 小时前
第七篇:《大型前端项目的模块化与目录结构设计》
前端
你脑门上的脚印2 小时前
Vue 项目从零实现语音转文字、文字转语音功能(完整可用 + 踩坑指南)
前端·vue.js
paopaokaka_luck3 小时前
基于springboot3+vue3的车间生产管理系统(Echarts图形化分析、BI报表)
java·前端·spring boot·学习·echarts