CSS and JS minification doesn't work with gulp-filter, gulp-csso, gulp-uglify

佐手、 提交于 2019-12-06 05:16:04

Just for reference useref also changed with v3.0.

Here's my code I got working with the latest versions of gulp-filters and gulp-useref. Might be handy for anyone working through JP's gulp course...

gulp.task('optimise', ['inject', 'fonts', 'images'], function() {
    log('Optimising the JavaScript, CSS, HTML');
       
    // $.useref looks for  <!-- build:js js/app.js -->  in index.html and compiles until  <!-- endbuild -->
    var templateCache = config.temp + config.templateCache.file;
    var cssFilter = $.filter('**/*.css', {restore: true});
    var jsFilter = $.filter('**/*.js', {restore: true});   
       
    return gulp
        .src(config.index)
        .pipe($.plumber())                                        
        .pipe($.inject(gulp.src(templateCache, {read: false}), {
            starttag: '<!-- inject:templates:js -->'                
        }))
        .pipe($.useref({searchPath: './'}))
        .pipe(cssFilter)
        .pipe($.csso())
        .pipe(cssFilter.restore)
        .pipe(jsFilter)
        .pipe($.uglify())
        .pipe(jsFilter.restore)                                                                                     
        .pipe(gulp.dest(config.build));

});

Note: I also corrected the spelling of 'optimise' in the function name as I'm English :)

Well, apparently filters no longer work that way, so I'm using gulp-if for that. Under the same premises, the working code is:

gulp.task('optimize', ['inject'], function () {

    var assets = $.useref.assets({searchPath: ''});

    return gulp
        .src(config.indexFile)
        .pipe($.rename('test.jsp'))
        .pipe($.plumber())
        .pipe(assets)
        .pipe($.if('*.css', $.csso()))
        .pipe($.if('**/lib.js', $.uglify({preserveComments: 'license', mangle: false})))
        .pipe($.if('**/app.js', $.ngAnnotate()))
        .pipe($.if('**/app.js', $.uglify()))
        .pipe(assets.restore())
        .pipe($.useref())
        .pipe(gulp.dest(config.indexLocation))
        ;
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!