Gulp-inject需要很长时间

时间:2016-01-25 12:20:39

标签: gulp gulp-inject

我有一个问题,我无法弄清楚原因是什么以及如何解决它,gulp注入需要很长时间而没有任何理由,没有任何警告或错误,只需要很长时间完成然后成功完成(你可以看到它花了大约70分钟完成任务!有时候花了80-120分钟):

25-Jan-2016 10:11:54    [10:11:54] Using gulpfile C:\[PATH]\gulpfile.js
25-Jan-2016 10:11:54    [10:11:54] Starting 'scripts'...
25-Jan-2016 10:11:54    [10:11:54] Starting 'styles'...
25-Jan-2016 10:11:55    [10:11:55] Starting 'partials'...
25-Jan-2016 10:11:55    [10:11:55] Starting 'fonts'...
25-Jan-2016 10:11:56    [10:11:56] Starting 'other'...
25-Jan-2016 10:11:58    [10:11:58] gulp-inject 127 files into index.scss.
25-Jan-2016 10:11:59    [10:11:59] Finished 'fonts' after 3.28 s
25-Jan-2016 10:11:59    [10:11:59] all files 447.2 kB
25-Jan-2016 10:11:59    [10:11:59] Finished 'scripts' after 5.27 s
25-Jan-2016 10:12:01    [10:12:01] Finished 'partials' after 5.82 s
25-Jan-2016 10:12:42    [10:12:42] Finished 'styles' after 48 s
25-Jan-2016 10:12:42    [10:12:42] Starting 'inject'...
25-Jan-2016 10:12:42    [10:12:42] gulp-inject 1 files into index.html.
25-Jan-2016 10:12:44    [10:12:44] gulp-inject 210 files into index.html.
25-Jan-2016 10:12:44    [10:12:44] Finished 'inject' after 2.29 s
25-Jan-2016 10:12:44    [10:12:44] Starting 'html'...
25-Jan-2016 10:12:51    [10:12:51] gulp-inject 1 files into index.html.
25-Jan-2016 11:20:00    [11:20:00] dist\ maps\styles\app-04813dcfd7.css.map 319.71 kB
25-Jan-2016 11:20:02    [11:20:02] dist\ maps\styles\vendor-67763da458.css.map 27.75 kB
25-Jan-2016 11:23:19    [11:23:19] dist\ maps\scripts\vendor-334967a88b.js.map 11.95 MB
25-Jan-2016 11:23:28    [11:23:28] dist\ maps\scripts\app-5e1af1a1bd.js.map 1.23 MB
25-Jan-2016 11:23:28    [11:23:28] dist\ styles\app-04813dcfd7.css 750.67 kB
25-Jan-2016 11:23:28    [11:23:28] dist\ styles\vendor-67763da458.css 69.68 kB
25-Jan-2016 11:23:28    [11:23:28] Finished 'other' after 1.19 h
25-Jan-2016 11:23:29    [11:23:29] dist\ scripts\vendor-334967a88b.js 2.89 MB
25-Jan-2016 11:23:30    [11:23:30] dist\ scripts\app-5e1af1a1bd.js 714.37 kB
25-Jan-2016 11:23:30    [11:23:30] dist\ index.html 1.44 kB
25-Jan-2016 11:23:30    [11:23:30] dist\ all files 17.96 MB
25-Jan-2016 11:23:30    [11:23:30] Finished 'html' after 1.18 h
25-Jan-2016 11:23:30    [11:23:30] Starting 'build'...
25-Jan-2016 11:23:30    [11:23:30] Finished 'build' after 21 μs
25-Jan-2016 11:23:30    Finished task 'Gulp build' with result: Success

我提供了代码,我希望找到解决此问题的任何解决方案或任何想法:

注入任务:

var path = require('path');
var gulp = require('gulp');
var conf = require('./conf');

var $ = require('gulp-load-plugins')();

var wiredep = require('wiredep').stream;
var _ = require('lodash');

var browserSync = require('browser-sync');

gulp.task('inject-reload', ['inject'], function ()
{
    browserSync.reload();
});
gulp.task('inject', ['scripts', 'styles'], function ()
{
    var injectStyles = gulp.src([
        path.join(conf.paths.tmp, '/serve/app/**/*.css'),
        path.join('!' + conf.paths.tmp, '/serve/app/vendor.css')
    ], {read: false});

    var injectScripts = gulp.src([
            path.join(conf.paths.src, '/app/**/*.module.js'),
            path.join(conf.paths.src, '/app/**/*.js'),
            path.join('!' + conf.paths.src, '/app/**/*.spec.js'),
            path.join('!' + conf.paths.src, '/app/**/*.mock.js'),
        ])
        .pipe($.angularFilesort()).on('error', conf.errorHandler('AngularFilesort'));

    var injectOptions = {
        ignorePath  : [conf.paths.src, path.join(conf.paths.tmp, '/serve')],
        addRootSlash: false
    };

    return gulp.src(path.join(conf.paths.src, '/*.html'))
        .pipe($.inject(injectStyles, injectOptions))
        .pipe($.inject(injectScripts, injectOptions))
        .pipe(wiredep(_.extend({}, conf.wiredep)))
        .pipe(gulp.dest(path.join(conf.paths.tmp, '/serve')));
}); 
如果需要,

配置文件:

var gutil = require('gulp-util');

/**
 *  The main paths of your project handle these with care
 */
exports.paths = {
    src : 'src',
    dist: 'dist',
    tmp : '.tmp',
    e2e : 'e2e'
};

/**
 *  Wiredep is the lib which inject bower dependencies in your project
 *  Mainly used to inject script tags in the index.html but also used
 *  to inject css preprocessor deps and js files in karma
 */
exports.wiredep = {
    directory: 'bower_components'
};

/**
 *  Common implementation for an error handler of a Gulp plugin
 */
exports.errorHandler = function (title)
{
    'use strict';

    return function (err)
    {
        gutil.log(gutil.colors.red('[' + title + ']'), err.toString());
        this.emit('end');
    };
};

1 个答案:

答案 0 :(得分:2)

首先:我在这里学会了听,从不使用gulp和npm模块而不将它们升级到最新版本。

我发现这个问题与gulp的过时和弃用插件有关,而不是gulp-inject本身:

  • gulp-useref从1.3到3.0(更新和迁移)过时了。
  • gulp-minify-css已弃用,已替换为gulp-cssnano
  • gulp-minify-html已弃用,已替换为gulp-htmlmin

我刚刚更新了插件,取代了已弃用的DANG问题。

修复后的构建任务:

gulp.task('html', ['inject', 'partials'], function ()
{
    var partialsInjectFile = gulp.src(path.join(conf.paths.tmp, '/partials/templateCacheHtml.js'), {read: false});
    var partialsInjectOptions = {
        starttag    : '<!-- inject:partials -->',
        ignorePath  : path.join(conf.paths.tmp, '/partials'),
        addRootSlash: false
    };

    var htmlFilter = $.filter('*.html', {restore: true});
    var jsFilter = $.filter('**/*.js', {restore: true});
    var cssFilter = $.filter('**/*.css', {restore: true});

    return gulp.src(path.join(conf.paths.tmp, '/serve/*.html'))
        .pipe($.inject(partialsInjectFile, partialsInjectOptions))
        .pipe(jsFilter)
        .pipe($.sourcemaps.init())
        .pipe($.ngAnnotate())
        .pipe($.uglify({preserveComments: $.uglifySaveLicense})).on('error', conf.errorHandler('Uglify'))
        .pipe($.sourcemaps.write('maps'))
        .pipe(jsFilter.restore)
        .pipe(cssFilter)
        .pipe($.sourcemaps.init())
        .pipe($.cssnano())
        .pipe($.sourcemaps.write('maps'))
        .pipe(cssFilter.restore)
        .pipe($.useref())
        .pipe(htmlFilter)
        .pipe($.htmlmin({
            removeComments      : true,
            collapseWhitespace  : true,
            removeAttributeQuotes: true
        }))
        .pipe(htmlFilter.restore)
        .pipe(gulp.dest(path.join(conf.paths.dist, '/')))
        .pipe($.size({
            title    : path.join(conf.paths.dist, '/'),
            showFiles: true
        }));
});