简体   繁体   English

Gulp任务运行缓慢

[英]Gulp tasks running slow

I'm getting poor performance from my gulp build. 我的gulp构建表现不佳。

Node v0.10.29 Windows 8.1 x64 节点v0.10.29 Windows 8.1 x64

Gulpfile: Gulpfile:

// based off: https://github.com/kriasoft/SPA-Seed.Front-end/blob/master/gulpfile.js

var gulp = require('gulp');
var gulpif = require('gulp-if');
var gutil = require('gulp-util');
var rimraf = require('rimraf');
var tsc = require('gulp-typescript-compiler');
var sass = require('gulp-sass');
var uglify = require('gulp-uglify');
var es = require('event-stream');
var plumber = require('gulp-plumber');
var path = require('path');
var argv = require('yargs')
    .default('target', 'local')
    .argv;

// a cache for Gulp tasks
var task = {};

// object used to help define source locations
var sources = {};

sources.commonIgnore = [
    '!vendor{,/**}',
    '!node_modules{,/**}',
    '!gulpfile.js'
]

var isLocal = argv.target === 'local';
var isBeta = argv.target === 'beta';
var isProduction = argv.target === 'production';

var destRoot = '../dist';
var destPath = path.join(destRoot, argv.target);

var typescriptOptions = {
    sourcemap: isLocal
};

// by default, only uglify if not local
var isUgly = !isLocal;

// if the "ugly" switch was included, always uglify
if (argv.ugly) {
    isUgly = true;
};

gulp.task('clean', function (cb) {
    rimraf(destPath, cb);
});

// Copy file types that do not require compilation
// or other build step (such as uglify)
sources.includes = [
    '**/*.htm*',
    '**/*.woff',
    '**/*.otf',
    '**/*.eot',
    '**/*.ttf'];
gulp.task('includes', task.includes = function () {
    var includeSources = sources.includes.concat(sources.commonIgnore);
    return gulp.src(includeSources)
        .pipe(gulp.dest(destPath));
});
gulp.task('includes-clean', ['clean'], task.includes);

// Copy vendor specific files
gulp.task('vendor', task.vendor = function () {
    return gulp.src(['vendor/**'])
        .pipe(gulp.dest(destPath + '/vendor'));
});
gulp.task('vendor-clean', ['clean'], task.vendor);

// Copy image files
sources.images = [
    '**/*.jpg',
    '**/*.png',
    '**/*.svg'];
gulp.task('images', task.images = function () {
    var imageSources = sources.images.concat(sources.commonIgnore);

    // TODO: compress images
    return gulp.src(imageSources)
        .pipe(gulp.dest(destPath));
});
gulp.task('images-clean', ['clean'], task.images);

// Copy script files
sources.typescript = ['**/*.ts'];
sources.javascript = ['**/*.js'];
gulp.task('scripts', task.scripts = function () {
    var typescriptSources = sources.typescript.concat(sources.commonIgnore);
    var javascriptSources = sources.javascript.concat(sources.commonIgnore);

    return es.concat(
        gulp.src(typescriptSources)
        .pipe(gulpif(isLocal, gulp.dest(destPath)))
        .pipe(plumber())
        .pipe(tsc(typescriptOptions))
        .pipe(gulpif(isUgly, uglify()))
        .pipe(gulp.dest(destPath)),

        gulp.src(javascriptSources)
        .pipe(plumber())
        .pipe(gulpif(isUgly, uglify()))
        .pipe(gulp.dest(destPath))
    );
});

gulp.task('scripts-clean', ['clean'], task.scripts);

// Copy style files
sources.sass = ['**/*.scss'];
sources.css = ['**/*.css'];
gulp.task('styles', task.styles = function () {
    var sassSources = sources.sass.concat(sources.commonIgnore);
    var cssSources = sources.css.concat(sources.commonIgnore);

    return es.concat(

        gulp.src(sassSources)
        .pipe(plumber())
        .pipe(sass())
        .pipe(gulp.dest(destPath)),

        gulp.src(cssSources)
        .pipe(gulp.dest(destPath))
    );
});
gulp.task('styles-clean', ['clean'], task.styles);

gulp.task('watch', task.watch = function () {

    var sassSources = sources.sass.concat(sources.commonIgnore);
    gulp.watch(sassSources, ['styles']);

    var cssSources = sources.css.concat(sources.commonIgnore);
    gulp.watch(cssSources, ['styles']);

    var typescriptSources = sources.typescript.concat(sources.commonIgnore);
    gulp.watch(typescriptSources, ['scripts']);

    var javascriptSources = sources.javascript.concat(sources.commonIgnore);
    gulp.watch(javascriptSources, ['scripts']);

    var imageSources = sources.images.concat(sources.commonIgnore);
    gulp.watch(imageSources, ['images']);

    var includeSources = sources.includes.concat(sources.commonIgnore);
    gulp.watch(includeSources, ['includes']);

    gulp.watch(['vendor/**/*'], ['vendor']);

    // Watch for changes in 'compiled' files
    gulp.watch(destPath + '/**', function (file) {
        var lr = require('gulp-livereload');
        gutil.log('File changed: ' + gutil.colors.magenta(file.path));
        lr.changed(file.path);
    });
});

gulp.task('build', [
    'includes-clean',
    'vendor-clean',
    'images-clean',
    'styles-clean',
    'scripts-clean']);

// launch a lightweight HTTP Server
gulp.task('run', ['build'], function (next) {
    var url = require('url'),
        fileServer = require('ecstatic')({ root: destPath, cache: 'no-cache', showDir: true });

    getAvailablePort(8080, function (port) {
        require('http').createServer()
            .on('request', function (req, res) {
                fileServer(req, res);
            })
            .listen(port, function () {
                gutil.log('Server is listening on ' + gutil.colors.magenta('http://localhost:' + port + '/'));
                next();
            });
    });
});

function getAvailablePort(startingPort, cb) {
    var server = require('http').createServer();
    var port = startingPort;

    server.on('error', function (err) {
        getAvailablePort(port + 1, cb);
    });

    server.listen(port, function (err) {
        server.once('close', function () {
            cb(port);
        });
        server.close();
    });
}

gulp.task('watch', ['run'], task.watch);

gulp.task('default', ['watch']);

Package.json: 的package.json:

{
  "name": "buildTest",
  "version": "0.0.1",
  "devDependencies": {
    "cordova-lib": "^0.21.6",
    "ecstatic": "^0.5.4",
    "event-stream": "^3.1.5",
    "gulp": "^3.8.5",
    "gulp-debug": "^0.3.0",
    "gulp-if": "^1.2.1",
    "gulp-livereload": "^2.1.0",
    "gulp-plumber": "^0.6.4",
    "gulp-sass": "^0.7.2",
    "gulp-typescript-compiler": "^1.0.0",
    "gulp-uglify": "^0.3.1",
    "gulp-util": "^3.0.0",
    "karma": "^0.12.16",
    "karma-chrome-launcher": "^0.1.4",
    "karma-jasmine": "^0.1.5",
    "mkdirp": "^0.5.0",
    "q": "^1.0.1",
    "rimraf": "^2.2.8",
    "typescript": "^1.0.1",
    "url": "^0.10.1",
    "yargs": "^1.2.6"
  }
}

The "includes-clean" task is a good place to start. “包含清洁”任务是一个很好的起点。 The task copies htm* and font files to my dist output. 该任务将htm *和字体文件复制到我的dist输出。 For my current test project, it matches on 7 files which total 32kb. 对于我目前的测试项目,它匹配7个文件,总共32kb。 Shouldn't take long, but I'm seeing an average of 15 seconds to complete the task. 不应该花很长时间,但我看到平均15秒完成任务。

I suspect the way I handle the commonIgnore list might be the issue. 我怀疑我处理commonIgnore列表的方式可能是问题。 If gulp is traversing all of the node_modules and vendor folder (even though they should be ignored), it might cause the slowdown. 如果gulp遍历所有node_modules和vendor文件夹(即使它们应该被忽略),它可能会导致速度减慢。

I think you're right about the way you're handling excludes. 我认为你处理排除的方式是正确的。 The comments in the node_glob source imply that the negated patterns will be tested against everything that matches the non-negated patterns. node_glob源中的node_glob意味着将针对与非否定模式匹配的所有内容测试否定模式。 I think you should move your source files under a dev or src directory so that you don't have to exclude node_modules , etc. 我认为你应该将你的源文件移动到devsrc目录下,这样你就不必排除node_modules等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM