Prevent cssMin from removing svg styles

When running the grunt.js cssMin task https://github.com/gruntjs/grunt-contrib-cssmin

The svg css properties are removed.

For example:

.testClass {
   r: 4;
   width: 3px;
   margin-left: 18px !important;
}

      

Receives conversion to

.testClass {
   width: 3px;
   margin-left: 18px !important;
}

      

How can I prevent this?

+3


source to share


2 answers


grunt-contrib-cssmin

uses clean-css

internally as specified in options , and any original ( clean-css

) parameters are "passed to clean-css".

clean-css

optimizes groups for levels for convenience. There are two options for managing the deletion of rules found under level 2:

restructureRules: false, // should disable all removals
skipProperties: []       // specify individual rules to skip

      

This should do it:

cssmin: {
  options: {
    skipProperties: ['r']
  }
}

      

Strike>


Starting with clean-css 4.2.0 there will be a comment block method for skipping fragments:

/* clean-css ignore:start */
.testClass {
   r: 4;
   width: 3px;
   margin-left: 18px !important;
}
/* clean-css ignore:end */

      

The note 4.2

has not yet been released.


After a bit of testing, none of the above functions seem to work, although they "should" according to the documentation.
The only alternative I have is to replace grunt-contrib-cssmin

with grunt-postcss cssnano

(this is what I use with grunt to minimize):



npm install grunt-postcss cssnano

      

grunt.initConfig({
  postcss: {
    processors: [
      require('cssnano')({
        // options here...
      })
    ]
  },
});

grunt.loadNpmTasks('grunt-postcss');
grunt.registerTask('postcss', ["postcss"]);

      

In practice, I use more postcss

addons.
Here a practical example of the autoprefixer

, pixrem

, postcss-flexbox

, and cssnano

:

module.exports = function(grunt) {
    grunt.initConfig({
        postcss: {
            options: {
                processors: [
                    require('pixrem'),
                    require('autoprefixer')({browsers: ['> 0%']}),
                    require('postcss-flexboxfixer'),
                    require('cssnano')({
                      autoprefixer:false,
                      zindex: false
                    })
                ]
            },
            jack: {
                files: [{
                    expand:true,
                    flatten:true,
                    cwd: 'assets/',
                    src: ['scss/*.css', '!**/variables.css'],
                    dest:'assets/css/'
                }]
            }
        },
        watch: {
            styles: {
                files: [
                    'assets/scss/*.css'
                ],
                tasks:['postcss:jack']
            }
        }
    });
    grunt.loadNpmTasks('grunt-postcss');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.registerTask('default', ["watch:styles"]);
    grunt.registerTask('jack', ["postcss:jack"]);
};

      

I purposefully registered a task that only runs the plugin postcss

:

grunt jack

      

Don't forget that you need to install each addon to use with postcss

. For the above you will need:

npm install grunt-postcss cssnano pixrem autoprefixer postcss-flexboxfixer

      

... and most likely you will want to change files

to match what you have.

This time I tested. The property r

adds to the thumbnail file:

.testClass{r:20;width:3px;margin-left:18px!important}

      

+2


source


You should be able to prevent this by setting the restructuring preferences to false:



options: {
  restructuring: false
},

      

+2


source







All Articles