Sandrina Pereira Sandrina Pereira - 2 months ago 14
Sass (Sass) Question

gulp-clean-css cleaning / removing duplicated css when using @extend

I'm having a weird problem with @extend on scss. when i extend some %placeholder that have repetitive properties, the gulp-clean-css merge them, and I don't want that to happen.

Here's an example:
foo.scss

%one {
font-size: 16px;
width: 95%;
}

%two {
font-size: 1rem;
width: calc(100% - 10px);
}


.foo {
@extend %one;
@extend %two;
}


.foo2 {
font-size: 16px;
font-size: 1rem;
}


foo.min.css

.foo{font-size:1rem;width:calc(100% - 10px);}
.foo2{font-size:16px;font-size:1rem}


Why does this happen?

If it helps, this is mine gulp-task:

gulp.task('scss', function(){
console.log('start task scss');
gulp.src(folderStyles)
.pipe(sass.sync().on('error', sass.logError))
.pipe(rename({ suffix: '.min' }))
.pipe(cleanCSS({compatibility: 'ie9', noAdvanced: true}))
.pipe(gulp.dest(folderStyles));
logEnv();
});


Is there any other better plugin to use?
I don't want it to remove duplicated properties. If you ask me why, it's because of old browsers support than might not support
rem
or
calc
, or other "new fancy propertie" ;)

Thank you :)

Answer

I played around with this, and I think your setting is incorrect for 'noAdvanced':

Instead of:

.pipe(cleanCSS({compatibility: 'ie9', noAdvanced: true}))

Use:

.pipe(cleanCSS({compatibility: 'ie9', advanced: false}))

According to the docs:

advanced - set to false to disable advanced optimizations - selector & property merging, reduction, etc.

https://github.com/jakubpawlowicz/clean-css#how-to-use-clean-css-api

This isn't going to leave you with 1 .foo selector with two font-size declarations tho, it's just not going to merge the two, so you'll end up having:

.foo {
    font-size:16px;
    width:95%
}
.foo {
    font-size:1rem;
    width:calc(100% - 10px)
}
.foo2{
    font-size:16px;
    font-size:1rem
}

This answers your issue however kind of defeats the purpose of using something like clean-css. I'm curious as to why you would want to leave both font-size declarations in your CSS when the first one will be overridden anyway.

Comments