Skip to content

less代码打包后组件样式错误覆盖 #4978

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
drafish opened this issue Jul 6, 2020 · 10 comments · Fixed by #6637
Closed

less代码打包后组件样式错误覆盖 #4978

drafish opened this issue Jul 6, 2020 · 10 comments · Fixed by #6637
Assignees

Comments

@drafish
Copy link

drafish commented Jul 6, 2020

What happens?

比如有A、B两个页面,都引用了组件C,A、B都写了自己的样式去覆盖C组件中的样式,最终打包出来的结果是A.css、B.css中都有C组件的样式。因为C组件的样式定义在最前面,可以被A、B组件的样式正确覆盖,所以单独加载A、B页面都没问题。但是从A切到B,再切回A,就会发现A的样式被B中的C组件样式覆盖了。因为从A切到B的时候,B.css会加到标签中,因为B.css在A.css后面,所以B.css中的C组件样式优先级高于A.css中的样式,这样就产生了错误覆盖。

这个bug其实之前 @youxfans 同学已经提过一个issue了,他发现less代码打包后会产生重复代码,而我发现的这个问题正是这个重复代码引起的,而且看起来挺严重的。重复代码还只是影响到文件体积,但是样式错误覆盖就影响页面渲染了,希望这个bug能尽快修复。

Mini Showcase Repository(REQUIRED)

https://github.com/drafish/umi-demo

How To Reproduce

Steps to reproduce the behavior: 1. 2.

Expected behavior 1. 2.

Context

  • Umi Version:
  • Node Version:
  • Platform:
@DAOLIDEWONIU
Copy link

+1

@binyellow
Copy link

+1

1 similar comment
@xuxji
Copy link

xuxji commented Dec 23, 2020

+1

@xuxji
Copy link

xuxji commented Dec 23, 2020

研究了下,参考官网提示,摸索了有个效的解决方案,希望也对遇到相同问题的大家有帮助。

配置里增加splitChunks
我这里被覆盖主要是antd的样式,所以把antd作为vendor分离出去,并且设置高priority,这样在页面加载时会优先加载antdVendor的样式,而common则是自定义样式。reuseExistingChunk表示是否使用已有的chunk,如果为true 表示复用已分离的chunk。

  chainWebpack(config) {
    config.optimization.splitChunks({
      cacheGroups: {
        common: {
          name: 'common',
          test: /\.(css|less)$/,
          chunks: 'async',
          minChunks: 1,
          minSize: 0,
          priority: 10,
          reuseExistingChunk: true,
        },
        antdVendor: {
          test: /[\\/]node_modules[\\/](antd)[\\/]/,
          name: "antdVendor",
          priority: 20,
        },
      },
    });
  },

@ChenYCL
Copy link

ChenYCL commented Jan 26, 2021

image
使用楼上的无效果,还是会覆盖,但是改了别名后,发现页面样式会直接侵入 style 注册,看了下qiankun的import-entry-html的配置,其实希望能自动控制动态link style顺序

这样没有被覆盖了

@ycjcl868 ycjcl868 self-assigned this Feb 7, 2021
@hanwenbo
Copy link

+1

@ycjcl868
Copy link
Contributor

我排查下

@wulinjie122
Copy link

后来解决了吗?我遇到了同样的问题,这是我的路由配置:

routes: [
    {
      path: '/',
      component: '../layouts/index',
      routes: [
        { path: '/', component: '../pages/index' },
        { path: '/article', component: '../pages/Article'},
        { path: '/girl', component: '../pages/Girl'},
        { path: '/cp', component: '../pages/Cp'},
      ]
    }
  ],

article组件和girl有同名的css,但是这两个组件都import了自己当前文件夹下的less文件,最后渲染后两个less文件都被引入进来了,比较困惑。

.filters .btn {
  border-color: transparent;
  font-size: .8rem;
  padding: 8px 15px;
  margin: 3px;
  background: rgba(52, 58, 64, 0.05) !important;
  color: #343a40 !important;
}

@Leonewu
Copy link

Leonewu commented Mar 4, 2021

这个问题目前有解决方案吗?我是按需加载的时候,antd 样式会被重复打包一遍,并且和组件的样式在同一个 css 文件,刚好这部分被重复打包的样式就在组件样式的后面,所以就被覆盖了

@leroy-magpie
Copy link

研究了下,参考官网提示,摸索了有个效的解决方案,希望也对遇到相同问题的大家有帮助。

配置里增加splitChunks 我这里被覆盖主要是antd的样式,所以把antd作为vendor分离出去,并且设置高priority,这样在页面加载时会优先加载antdVendor的样式,而common则是自定义样式。reuseExistingChunk表示是否使用已有的chunk,如果为true 表示复用已分离的chunk。

  chainWebpack(config) {
    config.optimization.splitChunks({
      cacheGroups: {
        common: {
          name: 'common',
          test: /\.(css|less)$/,
          chunks: 'async',
          minChunks: 1,
          minSize: 0,
          priority: 10,
          reuseExistingChunk: true,
        },
        antdVendor: {
          test: /[\\/]node_modules[\\/](antd)[\\/]/,
          name: "antdVendor",
          priority: 20,
        },
      },
    });
  },

还需要添加配置

chunks: ['antdVendor', 'common', 'umi']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet