Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cssmin not correctly handling @import

I am using cssmin on files containing @imports. cssmin is recursively importing local files correctly, but for imports that point to a URL the imports are left inline. This makes the resulting minified CSS invalid because @ rules must be at the beginning of the file. Does anyone know a good solution or workaround to this problem?

like image 916
user3204380 Avatar asked Jan 16 '14 21:01

user3204380


2 Answers

I have exactly the same problem with cssmin and @import, and i found a solution with grunt concat:

  1. Create a concat grunt task that:
  2. Put @import url in the begining of mified css file and replaces references of @imports url for "".
  3. Execute task concat:cssImport after cssmin task.

Grunt task Code: to execute (concat:cssImport)

 grunt.initConfig({     
   concat: {
      cssImport: {
            options: {

               process: function(src, filepath) {
                return "@import url(http://fonts.googleapis.com/css?family=Roboto:400,300,900);"+src.replace('@import url(http://fonts.googleapis.com/css?family=Roboto:400,300,900);', '');
              }
           }
         },
            files: {
                'your_location_file_origin/file.full.min.css': ['your_location_file_destination/file.full.min.css']
            }
        } )}

My inspiration comes from https://github.com/gruntjs/grunt-contrib-concat#custom-process-function.

like image 84
Alex Carod Avatar answered Oct 09 '22 05:10

Alex Carod


I added the processImport: false option to grunt.

'cssmin': {
  'options': {
    'processImport': false
  }
}
like image 33
Fırat KÜÇÜK Avatar answered Oct 09 '22 07:10

Fırat KÜÇÜK