Gulp.js starts sass task but doesn't finish

旧时模样 提交于 2019-12-01 15:21:51

问题


My gulpfile.js

var gulp = require('gulp');

// plugins
var sass = require('gulp-sass');
var minifycss = require('gulp-minify-css');

// compile sass
gulp.task('sass', function() {
    return gulp.src('static/css/scss/main.scss')
        .pipe(sass())
        .pipe(minifycss())
        .pipe(gulp.dest('/static/css/main.css'));
});

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

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

When I run gulp I get the following output:

[gulp] Using gulpfile /var/www/example/gulpfile.js
[gulp] Starting 'watch'...
[gulp] Finished 'watch' after 21 ms
[gulp] Starting 'default'...
[gulp] Finished 'default' after 23 μs

Then when I save my main.scss file it outputs:

[gulp] Starting 'sass'...

At which point it just hangs and never finishes. What am I doing wrong?


回答1:


The return inside the sass task seemed to break it, updated my gulpfile.js to following to get it working:

var gulp = require('gulp');

// plugins
var sass = require('gulp-sass');
var minifycss = require('gulp-minify-css');

// compile sass
gulp.task('sass', function() {
    gulp.src('static/css/scss/main.scss')
        .pipe(sass())
        .pipe(minifycss())
        .pipe(gulp.dest('/static/css'));
});

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

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



回答2:


For anyone else who has this issue, I managed to fix this by removing circular imports.

For example...

Before:

file1.scss
@import 'file2';

file2.scss
@import 'file1';

After:

file1.scss
<removed import>

file2.scss
@import 'file1';

I didn't actually need the circular dependency, I was just copying/pasting dependencies to the tops of all my scss files.Note that if your files actually need to import one another you'd either need to refactor your files in such a way that you avoid circular dependencies.




回答3:


The reason my sass compilation was not working and the gulp process was hanging and pinging my processor was that I had an old reference in my .scss file but I had removed the file. There was never any output that complained - this was hard to track down - I got lucky.

@import 'file-that-has-been-removed-but-still-referenced';



来源:https://stackoverflow.com/questions/23584962/gulp-js-starts-sass-task-but-doesnt-finish

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!