浏览器嵌套兼容处理

背景

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

相关推荐
ikoala4 分钟前
Codex 怎么买、怎么充值?先把这两套计费搞清楚
前端·javascript·后端
wuhen_n27 分钟前
RAG 优化实战:检索精准度提升全方案
前端·langchain·ai编程
Mike_jia30 分钟前
DataEase:人人可用的开源BI神器,企业数据决策民主化实战指南
前端
lichenyang45333 分钟前
从一次“重新发送 / 重新生成”开始,聊聊流式聊天状态机到底解决了什么问题
前端
前端Hardy34 分钟前
一个时代结束了:npm 终于对 install 脚本下手了
前端·javascript·后端
撑死胆大的35 分钟前
2026开发变局:国标落地后,软件开发彻底换赛道
前端·低代码·ai·大模型
悟空瞎说1 小时前
最新 React Native 推送通知完整实战指南
前端
GuWenyue1 小时前
前端异步请求踩坑?3种方式搞定Ajax数据交互,从XHR到async/await
前端·javascript·设计模式
李白的天不白1 小时前
pnpm 启动前端项目
前端