2

I have a very basic gulpfile set up like this:

'use strict';

var gulp = require('gulp');
var sass = require('gulp-sass');
var notify = require('gulp-notify');
var browserSync = require('browser-sync').create();
var useref = require('gulp-useref');
var cssnano = require('gulp-cssnano');
var uglify = require('gulp-uglify');
var gulpIf = require('gulp-if');

/*----------  Config settings  ----------*/

var config = {
    devPath: './app/',
    publicPath: './dist/',
    sassDir: 'sass/',
    jsDir: 'js/',
    cssDir: 'css/',
    fontDir: 'fonts/',
    moduleDir: './node_modules',
}

/*----------  Define tasks  ----------*/

gulp.task('browserSync', function() {
    var htmlDir = config.publicPath;

    browserSync.init({
        server: {
            baseDir: htmlDir
        },
    })
})

gulp.task('sassTask', function () {
    var sassFiles = config.devPath + config.sassDir + '/*.scss';
    var destDir = config.publicPath + config.cssDir;

    return gulp.src(sassFiles)
        .pipe(sass()
            .on("error", notify.onError(function (error) {
                return "Error: " + error.message;
            })))
        .pipe(gulp.dest(destDir));
});

gulp.task('useref', function(){
    var htmlFiles = config.devPath + '*.html';
    var destDir = config.publicPath;

    return gulp.src(htmlFiles)
        .pipe(useref())
        // Minifies only if it's a JavaScript file
        .pipe(gulpIf('*.js', uglify()))
        // Minifies only if it's a CSS file
        .pipe(gulpIf('*.css', cssnano()))
        .pipe(gulp.dest(destDir))
        .pipe(browserSync.reload({
            stream: true
        }));
});

gulp.task('js', function() {
    var jsFiles = config.devPath + config.jsDir + '**/*.js';    // All files
    var destDir = config.publicPath + config.jsDir;

    return gulp.src(jsFiles)
                .pipe(gulp.dest(destDir));
});

gulp.task('fonts', function() {
    var fontFiles = config.devPath + config.fontDir + '**/*';   // All files
    var destDir = config.publicPath + config.fontDir;

    return gulp.src(fontFiles)
                .pipe(gulp.dest(destDir));
});

/*----------  Define watch  ----------*/

gulp.task('watch', ['browserSync', 'sassTask', 'js', 'useref', 'fonts'], function () {
    var sassFiles = config.devPath + config.sassDir + '**/*.scss';
    var jsFiles = config.devPath + config.jsDir + '**/*.js';
    var htmlFiles = config.devPath + '*.html';
    var fontFiles = config.devPath + config.fontDir + '**/*';

    gulp.watch(sassFiles, ['sassTask', 'useref']);
    gulp.watch(jsFiles, ['js', 'useref']);
    gulp.watch(htmlFiles, ['useref']);
    gulp.watch(fontFiles, ['fonts']);
});

gulp.task('default', ['browserSync', 'sassTask', 'useref', 'fonts']);

Everything is working fine when running 'gulp watch' (browser sync, sass, etc.) but the css minification isn't. The outputfile 'style.css' in the dist/css folder isn't minified. What am I doing wrong? Am I missing a module? Thanks in advance for your help.

My file structure:

├───app
│   ├───fonts
│   ├───js
│   └───sass
├───dist
│   ├───css
│   │   └───vendor
│   ├───fonts
│   ├───img
│   │   ├───common
│   │   └───layout
│   └───js
│       └───vendor
onerkript
  • 57
  • 11

1 Answers1

2

You should change this sub-task as follows:

gulp.task('useref', function(){
    var files = [
        config.devPath + '*.html', 
        config.publicPath + config.jsDir + '*.js',
        config.publicPath + config.cssDir + '*.css'
    ];
    var destDir = config.publicPath;

    return gulp.src(files)
        // Run useref only if it’s an HTML file
        .pipe(gulpIf('*.html', useref()))
        // Minifies only if it's a JavaScript file
        .pipe(gulpIf('*.js', uglify()))
        // Minifies only if it's a CSS file
        .pipe(gulpIf('*.css', cssnano()))
        .pipe(gulp.dest(destDir))
        .pipe(browserSync.reload({
            stream: true
        }));
});

The first change is in the htmlFiles (i changes it to files for convenience). You need to change the file pattern to match al desired files: *.html, *.js and *.css in the different folders used as sources, because you want to modify the generated files. Using node glob patterns you can easily match the files by file extension passing an array of file patterns, to match different files in different folders..

Then just add a gulpIf condition for the html files tasks to filter the desired file type (you already have the filter function for the other file types).

Hope it helps.

muecas
  • 4,265
  • 1
  • 8
  • 18
  • Thanks for your help. I tried to change my useref sub-task with yours but still no effect. – onerkript Jun 21 '18 at 11:55
  • @onerkript really? Does the html files get passed to the useref? Can you post you file structure? Probably the pattern should change to match subdirectories `**/{*.html,*.js,*.css}`. – muecas Jun 21 '18 at 12:00
  • @onerkript can you post the file structure? – muecas Jun 21 '18 at 12:11
  • Ok, I edited my original post with my file structure. – onerkript Jun 21 '18 at 12:22
  • @onerkript i modified the file pattern. It should work now. Otherwise we can split the pattern in an array. – muecas Jun 21 '18 at 12:24
  • Thanks again but now it throws an error: [14:28:11] 'useref' errored after 110 μs [14:28:11] ReferenceError: jsDir is not defined – onerkript Jun 21 '18 at 12:29
  • @onerkript sorry, my mistake, those var are inside the `config` object. Check it now. – muecas Jun 21 '18 at 12:36
  • Ok, your code now works but still no minification. The weird thing is that it seems sassTask is finished after useref, see: [Browsersync] Reloading Browsers... (buffered 2 events) [14:38:13] Starting 'sassTask'... [14:38:13] Starting 'useref'... [Browsersync] 2 files changed (index.html, main.js) [14:38:13] Finished 'useref' after 16 ms [14:38:13] Finished 'sassTask' after 23 ms – onerkript Jun 21 '18 at 12:51
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/173563/discussion-between-muecas-and-onerkript). – muecas Jun 21 '18 at 12:58
  • it's working! i changed devPath from your code to publicPath and voila! thanks for your help. – onerkript Jun 21 '18 at 15:01
  • I'm going crazy. Now minification/uglyfication work but the html isn't working since it's looking in the publicpath. :/ – onerkript Jun 21 '18 at 15:55