cssgulpminifygulp-clean-css

Gulp Task: CleanCSS and Paths


I've created a task with Gulp that is supposed to:

  1. Join multiple CSS files;
  2. Minify + remove unnecessary CSS;
  3. Fix paths for url() directives and others;
  4. Generate source maps;

My current code for this is:

var gulp = require("gulp"),
    concat      = require("gulp-concat"),
    cleanCSS    = require("gulp-clean-css"),
    sourcemaps  = require("gulp-sourcemaps");

var styleList = [
    "Resources/Include/ionicons/css/ionicons.css",
    "Resources/base.css",
    "Resources/extra.css",
];

gulp.task("deploy-css", function() {
    gulp.src(styleList)
    .pipe(sourcemaps.init())
    .pipe(concat("style.min.css"))
    .pipe(cleanCSS({
            debug: true, 
            compatibility: "ie8", 
            keepSpecialComments : 0,
            target: "Resources/",
            relativeTo: "Resources/"
        })
    )
    .pipe(sourcemaps.write())
    .pipe(gulp.dest("Resources/"))
});

url() path example, taken from file Resources/Include/ionicons/css/ionicons.css:

@font-face { font-family: "Ionicons"; src: url("../fonts/ionicons.eot?v=2.0.0");

This is my current file structure:

./Resources/style.min.css // -> Final processed file
./Resources/base.css
./Resources/extras.css
./Resources/Include/ // -> Original CSS files with URL (installed via Bower)

Test folder: https://dl.dropboxusercontent.com/u/2333896/gulp-path-test.zip (install and then run with gulp deploy-css).

Almost everything works as expected, except for when CSS files include references to images or fonts using the url() option. After running the task (and style.min.css created) those references are broken - no change was made to the paths found on the original CSS files.

Isn't cleanCSS supposed to check where the referenced files are and fix the paths automatically? Aren't the options target and relativeTo used to control that?

How can I fix this? Thank you.


Solution

  • I managed to fix the issue, my main problems were a misplaced concat operation breaking gulp-clean-css rebase feature and wrong target and relativeTo options. Apparently I didn't think much about the previous workflow.

    var gulp = require("gulp"),
        concat      = require("gulp-concat"),
        cleanCSS    = require("gulp-clean-css"),
        sourcemaps  = require("gulp-sourcemaps");
    
    var styleList = [
        "Resources/Include/ionicons/css/ionicons.css",
        "Resources/base.css",
        "Resources/extra.css",
        "Resources/Include/teste/base.css"
    ];
    
    gulp.task("deploy-css", function() {
        gulp.src(styleList)
        .pipe(sourcemaps.init())
        .pipe(cleanCSS({
                compatibility: "ie8", 
                keepSpecialComments : 0,
                target: "Resources",
                relativeTo: ""
            })
        )
        .pipe(concat("style.min.css", {newLine: ""}))
        .pipe(sourcemaps.write())
        .pipe(gulp.dest("Resources"))
    });
    

    This new workflow works as:

    1. Optimize all individual CSS files - including rebasing urls;
    2. Contact individual optimized files into the final file - (note newLine: "" avoids line breaks in the file);
    3. Write the file.