Gulp lint需要太多时间

时间:2016-09-02 13:11:31

标签: gulp eslint gulp-livereload

我的gulp文件中的linting和live reloading有问题。他们花了很多时间才完成。

这是我的gulp文件,我做错了什么:

    'use strict';

console.time("Loading plugins"); //start measuring

var gulp = require('gulp');
var connect = require('gulp-connect');
var open = require('gulp-open');
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var concat = require('gulp-concat');
var babelify = require('babelify');
var sass = require('gulp-sass');
var merge = require('merge-stream'); // Merge all styles (css, sass and less) in one big bundle
var lint = require("gulp-eslint");

var config = {
    port: 8001,
    devBaseUrl: 'http://localhost',
    paths: {
        html: "./src/*.html",
        externals: "./src/assets/externals/*.js",
        js: "./src/**/*.js",
        images: './src/assets/images/**/*',
        fonts: './src/assets/css/fonts/*',
        css: [
            "./src/assets/css/*",
        ],
        sass: './src/assets/css/*.scss',
        dist: "./dist",
        mainJS: "./src/main.js"
    }
};


gulp.task('connect', ['watch'], function () {
    connect.server({
        root: ['dist'],
        port: config.port,
        base: config.devBaseUrl,
        livereload: true,
        fallback: './dist/index.html'
    })
});

gulp.task('open', ['connect'], function () {
    gulp.src('dist/index.html')
        .pipe(open({uri: config.devBaseUrl + ":" + config.port + "/"}));
});


gulp.task('html', function () {
    gulp.src(config.paths.html)
        .pipe(gulp.dest(config.paths.dist))
        .pipe(connect.reload());
});


gulp.task('externals', function () {
    gulp.src(config.paths.externals)
        .on('error', console.error.bind(console))
        .pipe(concat('external.js'))
        .pipe(gulp.dest(config.paths.dist + '/externals'))
        .pipe(connect.reload());
});


gulp.task('js', function () {
    browserify(config.paths.mainJS)
        .transform('babelify', {presets: ['es2015', 'react']})
        .bundle()
        .on('error', console.error.bind(console))
        .pipe(source('bundle.js'))
        .pipe(gulp.dest(config.paths.dist + '/scripts'))
        .pipe(connect.reload());
});


gulp.task('images', function () {
    gulp.src(config.paths.images)
        .pipe(gulp.dest(config.paths.dist + '/images'));
});


gulp.task('styles', function () {
    gulp.src(config.paths.css)
        .pipe(sass())
        .pipe(concat('bundle.css'))
        .pipe(gulp.dest(config.paths.dist + '/css'))
        .pipe(connect.reload());

});

gulp.task('fonts', function () {
    gulp.src(config.paths.fonts)
        .pipe(gulp.dest(config.paths.dist + '/css/fonts'));
});

gulp.task('lint', function () {
    return gulp.src(config.paths.js)
        .pipe(lint())
        .pipe(lint.format());
});


gulp.task('watch', function () {
    gulp.watch(config.paths.js, ['js', 'lint']);
    gulp.watch(config.paths.css, ['styles']);
});

console.timeEnd('Loading plugins');

gulp.task('default', ['js', 'styles', 'lint', 'open', 'watch']);

lint需要将近20秒才能完成,并且在进行一些更改后,肝脏染色需要5-6秒才能刷新浏览器。

有什么建议吗?

3 个答案:

答案 0 :(得分:1)

Gulp ESLint插件通常很慢。我在某些时候将它与Grunt进行了比较(前一段时间),它慢了大约5-10倍。不知道为什么。

确保您运行的是最新版本的ESLint,并且您的node_modules文件夹下没有src目录。如果这样做,您可以使用eslint标志运行--debug以确保ESLint不会在node_modules目录中找到文件。如果出于某种原因,请添加.eslintignore文件并指定您不想在那里丢弃的所有内容。

一般来说,如果您想在编码时获得即时反馈,我建议您查看编辑器集成。几乎每个编辑器都有ESLint插件。它们直接在您编写代码的窗口中显示错误。

答案 1 :(得分:0)

开发时是否有必要检查代码?

我们使用另一种方法:

1)开发时不要检查代码,因为它很长,有时也不允许在调试时为某些东西创建“快速”模拟。

2)仅在提交前检查样式。如果出现问题,请修改样式并检查一切是否正常。 CI系统也可以控制你的提交。

所以,我的建议是从监视任务中删除lint。

答案 2 :(得分:0)

我们最近在我的团队中遇到了同样的问题。最好的解决方法是仅对修改后的文件运行ESLint,而不是所有js文件。

我们使用nodemon来监视已更改的文件,尽管gulp-watch也有同样的想法。

请参阅gulp-watch上的更改事件。

然后你只需要在更改的文件上运行一个lint函数。 您可能需要解析相对文件路径。

gulp.watch(config.paths.js, ['js'])
    .on('change', lintFile);

const lintFile = (file) => {
  return gulp.src(file)
             .pipe(eslint());
};