简体   繁体   English

gulp无法正常工作

[英]gulp not working correctly

I have a few questions regarding my gulpfile.js setup. 我对gulpfile.js设置有一些疑问。

Here is my gulp file setup: 这是我的gulp文件设置:

var gulp = require('gulp');
var sass = require('gulp-sass');
var uglify = require('gulp-uglify');
var concat = require('gulp-concat');

var preserveFirstComment = function() {
    var set = false;

    return function() {
        if (set) return false;
            set = true;
            return true;
    };
};

    gulp.task('styles', function() {
    return gulp.src('./sass/**/*.scss')
    .pipe(sass().on('error', sass.logError))
    .pipe(gulp.dest('./css/'));
});

    gulp.task('styles:watch', function() {
    gulp.watch('sass/**/*.scss',['styles']);
});

    gulp.task('uglify', function() {
        gulp.src('lib/marked.js')
            .pipe(uglify({preserveComments: preserveFirstComment()}))
            .pipe(concat('marked.min.js'))
            .pipe(gulp.dest('.'));
    });

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

Once I try to run the my project with gulp all it does is run these 5 lines below and then stops and goes back to the command line prompt. 一旦尝试使用gulp运行我的项目,它要做的就是在下面的这5行中运行,然后停止并返回命令行提示符。 I don't understand why this is happening. 我不明白为什么会这样。 I am a beginner in using gulp as this is just a basic setup from a tutorial I used to setup my project. 我是使用gulp的初学者,因为这只是我用来设置项目的教程中的基本设置。

C:\Users\Gemini\Documents\workspace\ca_>gulp
[20:28:16] Using gulpfile ~\Documents\workspace\ca_\Gulpfile.js
[20:28:16] Starting 'uglify'...
[20:28:16] Finished 'uglify' after 21 ms
[20:28:16] Starting 'default'...
[20:28:16] Finished 'default' after 13 μs

C:\Users\Gemini\Documents\workspace\ca_>

Link: Getting Started with Gulp 链接: Gulp入门

When you only enter gulp on the command line Gulp executes the default task which in your case depends on the uglify task and not on styles:watch . 当您仅在命令行上输入gulp时,Gulp会执行default任务,您的情况取决于uglify任务,而不是styles:watch That's why you only see uglify and default in Gulp's output. 这就是为什么您只能在Gulp的输出中看到uglifydefault的原因。

If you want to execute the styles:watch task you have to tell Gulp explicitly so: 如果要执行styles:watch任务,则必须明确告诉Gulp这样:

C:\Users\Gemini\Documents\workspace\ca_> gulp styles:watch

To add to Sven's answer above, you can add many Gulp tasks to start watching on a gulp watch command. 要添加到上述Sven的答案中,您可以添加许多Gulp任务以开始使用gulp watch命令进行gulp watch

gulp.task('watch', function() {
   gulp.watch('./css/', ['styles']);
   gulp.watch('.', ['uglify']);
   // more watch things here
});

To run a gulp task, the command used is gulp <TASK NAME> and when we run just gulp , the default task is run. 要运行gulp任务,使用的命令是gulp <TASK NAME>而当我们仅运行gulp ,将运行默认任务。 If, in case a default task is not defined, an error is shown mentioning Task 'default' is not in your gulpfile . 如果在未定义默认任务的情况下显示错误,则说明Task 'default' is not in your gulpfile

In this case, gulp runs the default task which is 'uglify' and that is the reason why you are not able to watch the changes in the SASS. 在这种情况下,gulp将运行默认任务“ uglify”,这就是为什么您无法查看SASS中的更改的原因。 For watching the changes in the SASS, you could modify the default task to: 为了观察SASS中的更改,您可以将默认任务修改为:

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

This would help you out. 这将帮助您。

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

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