Can';不要让GULP-SASS和GULP-IMAGEMIN正常工作

Can';不要让GULP-SASS和GULP-IMAGEMIN正常工作,sass,gulp,gulp-watch,gulp-sass,gulp-imagemin,Sass,Gulp,Gulp Watch,Gulp Sass,Gulp Imagemin,半年过去了,我一直在使用这个gulpfile.js(如下所示),它工作得非常完美。现在,在windows 8.1 64位上,我无法让它正常工作,因为几个小时。我在Firefox中使用LiveReload实时查看更改,并通过咆哮获得更改通知 整个源代码是: var gulp = require('gulp'), gutil = require('gulp-util'), notify = require('gulp-notify'), autoprefix = requir

半年过去了,我一直在使用这个gulpfile.js(如下所示),它工作得非常完美。现在,在windows 8.1 64位上,我无法让它正常工作,因为几个小时。我在Firefox中使用LiveReload实时查看更改,并通过咆哮获得更改通知

整个源代码是:

var gulp = require('gulp'),
    gutil = require('gulp-util'),
    notify = require('gulp-notify'),
    autoprefix = require('gulp-autoprefixer'),
    minifyCSS = require('gulp-minify-css'),
    exec = require('child_process').exec,
    sys = require('sys'),
    uglify = require('gulp-uglify'),
    changed = require('gulp-changed'),
    livereload = require('gulp-livereload'),
    rename = require('gulp-rename'),
    imagemin = require('gulp-imagemin'),
    cache = require('gulp-cache'),
    path = require('path'),
    watch = require('gulp-watch'),
    sass = require('gulp-sass');

// Which directory should Sass compile to?
var targetDir = 'resources/compiled';

      //  .pipe(minifyCSS())
gulp.task('scss', function () {
    gulp.src('resources/uncompiled/**/*.scss')
        .pipe(changed(targetDir))
        .pipe(sass({errLogToConsole: true}))
        .pipe(autoprefix('last 15 version'))
        .pipe(changed(targetDir))
  .on('error', gutil.log)
        .pipe(gulp.dest(targetDir));

    //! .sass files -> compile, minify, autoprefix and publish
    watch('resources/uncompiled/**/*.scss', function(files) {
        return files.pipe(changed(targetDir))
            .pipe(sass({errLogToConsole: true}))
            .pipe(autoprefix('last 15 version'))
   .on('error', gutil.log)
            .pipe(gulp.dest(targetDir))
            .pipe(notify({
                message: "File: <%= file.relative %>",
                title: "SASS compiled, minified and published."
            }));
    });

});

gulp.task('js', function() {
    gulp.src('resources/uncompiled/**/*.js')
        .pipe(changed(targetDir))
        .pipe(uglify())
        .pipe(changed(targetDir))
        .pipe(gulp.dest(targetDir))
        .pipe(notify({
            message: "File: <%= file.relative %>",
            title: "JavaScript minified and published."
        }));

    //! .js files -> compress and publish
    watch('resources/uncompiled/**/*.js', function(files) {
        return files.pipe(changed(targetDir))
            .pipe(uglify())
            .pipe(rename(function(path) {
                path.dirname = path.dirname.replace(/assets\//g, "");
            }))
            .pipe(changed(targetDir))
            .pipe(gulp.dest(targetDir))
            .pipe(notify({
                message: "File: <%= file.relative %>",
                title: "JavaScript minified and published."
            }));
    });
});

gulp.task('images', function() {
    gulp.src(['resources/uncompiled/**/*.png', 'resources/uncompiled/**/*.jpg', 'resources/uncompiled/**/*.ico', 'resources/uncompiled/**/*.gif'])
        .pipe(changed(targetDir))
        .pipe(cache(imagemin({ optimizationLevel: 5, progressive: true, interlaced: true })))
        .pipe(changed(targetDir))
        .pipe(gulp.dest(targetDir));

    //! image files -> compress and publish
    watch(['resources/uncompiled/**/*.png', 'resources/uncompiled/**/*.jpg', 'resources/uncompiled/**/*.ico', 'resources/uncompiled/**/*.gif'], function(files) {
        return files.pipe(changed(targetDir))
            .pipe(cache(imagemin({ optimizationLevel: 5, progressive: true, interlaced: true })))
            .pipe(rename(function(path) {
                path.dirname = path.dirname.replace(/assets\//g, "");
            }))
            .pipe(changed(targetDir))
            .pipe(gulp.dest(targetDir))
            .pipe(notify({
                message: "Image: <%= file.relative %>",
                title: "Image optimized and published."
            }));
    });
});


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

    // Create LiveReload server
    var server = livereload();

    // Watch any files in public dir, reload on change
    gulp.watch(['resources/compiled/**']).on('change', function(file) {
        server.changed(file.path);
    });
});

// What tasks does running gulp trigger?
gulp.task('default', ['scss', 'js','images', 'watch']);

遵守第3版。但它仍然没有解决它

我的问题是,目前我无法让脚本像以前一样工作,无法注意main.sass中的更改,无法将该文件压缩为.css文件,也无法注意是否已放置图像/js文件并相应地移动它们。图像部分并不那么重要,但是SASS却很重要。它应该相应地将其放在/compiled/css或/compiled/images中

每次尝试更改和保存sass文件时,我都会得到以下结果:

path.js:146
      throw new TypeError('Arguments to path.resolve must be strings');
      ^
TypeError: Arguments to path.resolve must be strings
    at Object.win32.resolve (path.js:146:13)
    at DestroyableTransform._transform (L:\xampp\htdocs\node_modules\gulp-changed\index.js:72:22)
    at DestroyableTransform.Transform._read (L:\xampp\htdocs\node_modules\gulp-changed\node_modules\through2\node_modules
\readable-stream\lib\_stream_transform.js:172:10)
    at DestroyableTransform.Transform._write (L:\xampp\htdocs\node_modules\gulp-changed\node_modules\through2\node_module
s\readable-stream\lib\_stream_transform.js:160:12)
    at doWrite (L:\xampp\htdocs\node_modules\gulp-changed\node_modules\through2\node_modules\readable-stream\lib\_stream_
writable.js:326:12)
    at writeOrBuffer (L:\xampp\htdocs\node_modules\gulp-changed\node_modules\through2\node_modules\readable-stream\lib\_s
tream_writable.js:312:5)
    at DestroyableTransform.Writable.write (L:\xampp\htdocs\node_modules\gulp-changed\node_modules\through2\node_modules\
readable-stream\lib\_stream_writable.js:239:11)
    at DestroyableTransform.Writable.end (L:\xampp\htdocs\node_modules\gulp-changed\node_modules\through2\node_modules\re
adable-stream\lib\_stream_writable.js:467:10)
    at File.pipe (L:\xampp\htdocs\node_modules\gulp-watch\node_modules\vinyl\index.js:103:14)
    at L:\xampp\htdocs\fusion\_designs\cms-design\gulpfile.js:33:22
    at write (L:\xampp\htdocs\node_modules\gulp-watch\index.js:123:9)
    at L:\xampp\htdocs\node_modules\gulp-watch\node_modules\vinyl-file\index.js:52:4
    at L:\xampp\htdocs\node_modules\gulp-watch\node_modules\vinyl-file\node_modules\graceful-fs\graceful-fs.js:76:16
    at fs.js:334:14
    at L:\xampp\htdocs\node_modules\gulp-watch\node_modules\vinyl-file\node_modules\graceful-fs\graceful-fs.js:42:10
    at L:\xampp\htdocs\node_modules\gulp-watch\node_modules\chokidar\node_modules\readdirp\node_modules\graceful-fs\grace
ful-fs.js:42:10
当然,我试图改变路径,试图在函数中直接使用路径,而不是传递变量,但没有效果。
有什么建议吗?我已经挣扎了4个小时了,我确实设法让它在一定程度上正常工作,但我不得不删除很多功能,每次都重新启动gulp。我也无法将图像“缩小”到最小。

好吧,几天后,我自己解决了这个问题

首先,我必须为每种类型设置一块表。我把“吞咽任务”改为只执行一件事

现在,检查每个.scss sass、.js javascript、.jpg/.png/.bmp图像文件是否有更改,如果有更改,将调用所需的任务,该任务将自行编译/缩小/移动所需的文件并发出(咆哮)通知

因为我使用的是Windows 8.1,所以我有操作系统通知,但我更喜欢使用Growl,因此使用GrowlNotification

my gulpfile.js的完整源代码:

var gulp = require('gulp'),
    gutil = require('gulp-util'),
    notify = require('gulp-notify'),
    autoprefix = require('gulp-autoprefixer'),
    minifyCSS = require('gulp-minify-css'),
    exec = require('child_process').exec,
    sys = require('sys'),
    uglify = require('gulp-uglify'),
    changed = require('gulp-changed'),
    livereload = require('gulp-livereload'),
    rename = require('gulp-rename'),
    imagemin = require('gulp-imagemin'),
    cache = require('gulp-cache'),
    path = require('path'),
    watch = require('gulp-watch'),
    growl = require('gulp-notify-growl'),
    sass = require('gulp-sass');

// Initialize the Growl notifier  instead of the default OS notifications
// meaning, this: https://i.imgur.com/ikkINZr.png 
// changes to this: https://i.imgur.com/zz9m9k6.png
var growlNotifier = growl({
    hostname : 'localhost' // IP or Hostname to notify, default to localhost 
});

// Which directory should the files compile to?
var targetDir = 'resources/compiled';

gulp.task('scss', function () {

    gulp.src('resources/uncompiled/**/*.scss')
        .pipe(changed(targetDir))
        //.pipe(minifyCSS())  //makes it hard to debug , to be used for in-production only
        .pipe(sass({errLogToConsole: true}))
        .pipe(autoprefix('last 15 version'))
        .pipe(changed(targetDir))
    .on('error', gutil.log)
        .pipe(gulp.dest(targetDir))        
        .pipe(growlNotifier({    // change to notify if you wish to use your default OS notifications
                message: "File: <%= file.relative %>",
                title: "SASS compiled, minified and published."
            }))
        .pipe(livereload());    //this is how we notify LiveReload for changes
});

gulp.task('js', function() {

    //needs testing, but it should be OK
    gulp.src('resources/uncompiled/**/*.js')
        .pipe(changed(targetDir))
        .pipe(uglify())
        .pipe(changed(targetDir))
    .on('error', gutil.log)
        .pipe(gulp.dest(targetDir))
        .pipe(notify({
                message: "File: <%= file.relative %>",
                title: "Javascript minified and published."
            }))
        .pipe(livereload());

});

gulp.task('images', function() {

    gulp.src(['resources/uncompiled/**/*.png', 'resources/uncompiled/**/*.jpg', 'resources/uncompiled/**/*.ico', 'resources/uncompiled/**/*.gif'])
        .pipe(changed(targetDir))
        //imagemin causes errors for me: http://pastebin.com/eDF3SeS6
        //.pipe(cache(imagemin({ optimizationLevel: 5, progressive: true, interlaced: true })))
        .pipe(changed(targetDir))
    .on('error', gutil.log)
        .pipe(gulp.dest(targetDir))
        .pipe(growlNotifier({
            message: "Image: <%= file.relative %>",
            title: "Image optimized and published."
        }))
        .pipe(livereload());

});

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

    livereload.listen(); //we need to tell gulp to listen for livereload notification
    //this is how we watch for changes in specific files and fire specific tasks
    gulp.watch('resources/uncompiled/**/*.scss', ['scss']); 
    gulp.watch('resources/uncompiled/**/*.js', ['js']); 
    gulp.watch(['resources/uncompiled/**/*.png', 'resources/uncompiled/**/*.jpg', 'resources/uncompiled/**/*.ico', 'resources/uncompiled/**/*.gif'], ['images']); 

});

// What tasks does running gulp trigger?
gulp.task('default', ['scss', 'js','images', 'watch']);
var gulp=require('gulp'),
gutil=require('gulp-util'),
notify=需要('gulp-notify'),
autoprefix=require('gulp-autoprefixer'),
minifyCSS=require('gulp-minify-css'),
exec=require('child_process')。exec,
sys=require('sys'),
uglify=需要('gulp-uglify'),
更改=需要('gulp-changed'),
livereload=需要('gulp-livereload'),
重命名=需要('gulp-rename'),
imagemin=require('gulp-imagemin'),
cache=require('gulp-cache'),
path=require('path'),
watch=require('gulp-watch'),
咆哮=需要(“咕噜-咕噜-咕噜”),
sass=需要(“咕噜咕噜咕噜声”);
//初始化咆哮通知程序,而不是默认操作系统通知
//这意味着:https://i.imgur.com/ikkINZr.png 
//对此的更改:https://i.imgur.com/zz9m9k6.png
变量growlNotifier=咆哮({
主机名:“localhost”//要通知的IP或主机名,默认为localhost
});
//文件应该编译到哪个目录?
var targetDir='resources/compiled';
吞咽任务('scss',函数(){
gulp.src('resources/uncompiled/***.scss')
.管道(已更改(targetDir))
//.pipe(minifyCSS())//使调试变得困难,仅用于生产中
.pipe(sass({errLogToConsole:true}))
.pipe(autoprefix(“最后15版”))
.管道(已更改(targetDir))
.on('error',gutil.log)
.管道(吞咽目标(targetDir))
.pipe(growlNotifier({//如果希望使用默认操作系统通知,请更改为通知
信息:“文件:”,
标题:“SASS编译、缩小和发布。”
}))
.pipe(livereload());//这是我们通知livereload更改的方式
});
gulp.task('js',function(){
//需要测试,但应该可以
gulp.src('resources/uncompiled/***.js')
.管道(已更改(targetDir))
.管道(丑()
.管道(已更改(targetDir))
.on('error',gutil.log)
.管道(吞咽目标(targetDir))
.管道(通知({
信息:“文件:”,
标题:“Javascript缩小并发布。”
}))
.pipe(livereload());
});
gulp.task('images',function(){
gulp.src(['resources/uncompiled/***.png','resources/uncompiled/***.jpg','resources/uncompiled/***.ico','resources/uncompiled/***.gif']))
.管道(已更改(targetDir))
//imagemin为我导致错误:http://pastebin.com/eDF3SeS6
//.pipe(缓存(imagemin({optimizationLevel:5,progressive:true,interlaced:true}))
.管道(已更改(targetDir))
.on('error',gutil.log)
.管道(吞咽目标(targetDir))
.管道(生长指示器)({
信息:“图像:”,
标题:“图像优化和发布。”
}))
.pipe(livereload());
});
吞咽任务('watch',function(){
listen();//我们需要告诉gulp侦听livereload通知
//这就是我们如何监视特定文件中的更改并触发特定任务的方式
狼吞虎咽的看('resources/uncompiled/***.scss',['scss']);
gulp.watch('resources/uncompiled/***.js',['js']);
gulp.watch(['resources/uncompiled/***.png','resources/uncompiled/***.jpg','resources/uncompiled/***.ico','resources/uncompiled/***.gif'],['images']);
});
//运行gulp会触发哪些任务?
任务('default'、['scss'、'js'、'images'、'watch']);
path.js:146
      throw new TypeError('Arguments to path.resolve must be strings');
      ^
TypeError: Arguments to path.resolve must be strings
    at Object.win32.resolve (path.js:146:13)
    at DestroyableTransform._transform (L:\xampp\htdocs\node_modules\gulp-changed\index.js:72:22)
    at DestroyableTransform.Transform._read (L:\xampp\htdocs\node_modules\gulp-changed\node_modules\through2\node_modules
\readable-stream\lib\_stream_transform.js:172:10)
    at DestroyableTransform.Transform._write (L:\xampp\htdocs\node_modules\gulp-changed\node_modules\through2\node_module
s\readable-stream\lib\_stream_transform.js:160:12)
    at doWrite (L:\xampp\htdocs\node_modules\gulp-changed\node_modules\through2\node_modules\readable-stream\lib\_stream_
writable.js:326:12)
    at writeOrBuffer (L:\xampp\htdocs\node_modules\gulp-changed\node_modules\through2\node_modules\readable-stream\lib\_s
tream_writable.js:312:5)
    at DestroyableTransform.Writable.write (L:\xampp\htdocs\node_modules\gulp-changed\node_modules\through2\node_modules\
readable-stream\lib\_stream_writable.js:239:11)
    at DestroyableTransform.Writable.end (L:\xampp\htdocs\node_modules\gulp-changed\node_modules\through2\node_modules\re
adable-stream\lib\_stream_writable.js:467:10)
    at File.pipe (L:\xampp\htdocs\node_modules\gulp-watch\node_modules\vinyl\index.js:103:14)
    at L:\xampp\htdocs\fusion\_designs\cms-design\gulpfile.js:33:22
    at write (L:\xampp\htdocs\node_modules\gulp-watch\index.js:123:9)
    at L:\xampp\htdocs\node_modules\gulp-watch\node_modules\vinyl-file\index.js:52:4
    at L:\xampp\htdocs\node_modules\gulp-watch\node_modules\vinyl-file\node_modules\graceful-fs\graceful-fs.js:76:16
    at fs.js:334:14
    at L:\xampp\htdocs\node_modules\gulp-watch\node_modules\vinyl-file\node_modules\graceful-fs\graceful-fs.js:42:10
    at L:\xampp\htdocs\node_modules\gulp-watch\node_modules\chokidar\node_modules\readdirp\node_modules\graceful-fs\grace
ful-fs.js:42:10
var gulp = require('gulp'),
    gutil = require('gulp-util'),
    notify = require('gulp-notify'),
    autoprefix = require('gulp-autoprefixer'),
    minifyCSS = require('gulp-minify-css'),
    exec = require('child_process').exec,
    sys = require('sys'),
    uglify = require('gulp-uglify'),
    changed = require('gulp-changed'),
    livereload = require('gulp-livereload'),
    rename = require('gulp-rename'),
    imagemin = require('gulp-imagemin'),
    cache = require('gulp-cache'),
    path = require('path'),
    watch = require('gulp-watch'),
    growl = require('gulp-notify-growl'),
    sass = require('gulp-sass');

// Initialize the Growl notifier  instead of the default OS notifications
// meaning, this: https://i.imgur.com/ikkINZr.png 
// changes to this: https://i.imgur.com/zz9m9k6.png
var growlNotifier = growl({
    hostname : 'localhost' // IP or Hostname to notify, default to localhost 
});

// Which directory should the files compile to?
var targetDir = 'resources/compiled';

gulp.task('scss', function () {

    gulp.src('resources/uncompiled/**/*.scss')
        .pipe(changed(targetDir))
        //.pipe(minifyCSS())  //makes it hard to debug , to be used for in-production only
        .pipe(sass({errLogToConsole: true}))
        .pipe(autoprefix('last 15 version'))
        .pipe(changed(targetDir))
    .on('error', gutil.log)
        .pipe(gulp.dest(targetDir))        
        .pipe(growlNotifier({    // change to notify if you wish to use your default OS notifications
                message: "File: <%= file.relative %>",
                title: "SASS compiled, minified and published."
            }))
        .pipe(livereload());    //this is how we notify LiveReload for changes
});

gulp.task('js', function() {

    //needs testing, but it should be OK
    gulp.src('resources/uncompiled/**/*.js')
        .pipe(changed(targetDir))
        .pipe(uglify())
        .pipe(changed(targetDir))
    .on('error', gutil.log)
        .pipe(gulp.dest(targetDir))
        .pipe(notify({
                message: "File: <%= file.relative %>",
                title: "Javascript minified and published."
            }))
        .pipe(livereload());

});

gulp.task('images', function() {

    gulp.src(['resources/uncompiled/**/*.png', 'resources/uncompiled/**/*.jpg', 'resources/uncompiled/**/*.ico', 'resources/uncompiled/**/*.gif'])
        .pipe(changed(targetDir))
        //imagemin causes errors for me: http://pastebin.com/eDF3SeS6
        //.pipe(cache(imagemin({ optimizationLevel: 5, progressive: true, interlaced: true })))
        .pipe(changed(targetDir))
    .on('error', gutil.log)
        .pipe(gulp.dest(targetDir))
        .pipe(growlNotifier({
            message: "Image: <%= file.relative %>",
            title: "Image optimized and published."
        }))
        .pipe(livereload());

});

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

    livereload.listen(); //we need to tell gulp to listen for livereload notification
    //this is how we watch for changes in specific files and fire specific tasks
    gulp.watch('resources/uncompiled/**/*.scss', ['scss']); 
    gulp.watch('resources/uncompiled/**/*.js', ['js']); 
    gulp.watch(['resources/uncompiled/**/*.png', 'resources/uncompiled/**/*.jpg', 'resources/uncompiled/**/*.ico', 'resources/uncompiled/**/*.gif'], ['images']); 

});

// What tasks does running gulp trigger?
gulp.task('default', ['scss', 'js','images', 'watch']);