2

我正在用 Gulp 完成 johnpapa 的自动化课程,似乎碰到了一面奇怪的墙:当我尝试运行 CSS 和 JS 连接和缩小任务时,它无法进行缩小。

这是任务:

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

    var assets = $.useref.assets({searchPath: ''});
    var cssFilter = $.filter(['**/*.css'], {restore: true});
    var jsFilter = $.filter(['**/*.js'], {restore: true});

    return gulp
        .src(config.indexFile)
        .pipe($.rename('test.jsp'))
        .pipe($.plumber())
        .pipe(assets)
        .pipe(cssFilter)
        .pipe($.csso())
        .pipe(cssFilter.restore)
        .pipe(jsFilter)
        .pipe($.uglify())
        .pipe(jsFilter.restore)
        .pipe(assets.restore())
        .pipe($.useref())
        .pipe(gulp.dest(config.indexLocation))
        ;
});

inject是将 css 和 js 引用注入索引文件的任务(正常工作),$isrequire('gulp-load-plugins')({lazy: true})config.indexFileis index.jsp

我的文件结构(与课程中的不同)是:

- ModuleDir
    - dist
        - css
            - lib.css
            - app.css
        - fonts
        - images
        - js
            - lib.js
            - app.js
    - css
    - js
    - web-app
        - InnerDir
            - index.jsp
            - test.jsp
    - package.json, bower.json, etc. (all the required files)

基本上,index.jsp 针对 CSS 和 JS 库和应用程序资产进行处理,这些资产被缩小并连接成 lib.css、lib.js、app.css 和 app.js。之后,所有这些都被注入到名为 test.jsp 的 index.jsp 的副本中。

资产收集、连接和注入工作非常出色。缩小 - 不是那么多......

任何想法或指针将不胜感激。

4

2 回答 2

5

仅供参考 useref 也随 v3.0 更改。

这是我使用最新版本的 gulp-filters 和 gulp-useref 的代码。对于任何通过 JP 的 gulp 课程工作的人来说可能会很方便......

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));

});

注意:我还更正了函数名称中“优化”的拼写,因为我是英语 :)

于 2016-02-29T20:51:50.933 回答
3

好吧,显然过滤器不再以这种方式工作,所以我为此使用 gulp-if。在相同的前提下,工作代码是:

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))
        ;
});
于 2015-09-20T09:33:04.220 回答