Gulp.js - 无法将生成的文件从一个文件夹复制到另一个文件夹

时间:2022-02-03 16:04:07
gulp.task('minify-css', function () {

   gulp.src(['src/test/test.css])
   .pipe(concat('test1.css'))
   .pipe(minifyCSS())
   .pipe(gulp.dest('src/source/'))
   .pipe(filesize())

});
gulp.task('copy-css',['minify-css'], function () {

   gulp.src('src/source/*.css')
   .pipe(gulp.dest('src/dest/'));

});

It seems that the first time I run 'gulp copy-css'

看来我第一次运行'gulp copy-css'

Starting 'minify-css'...
[18:54:39] Finished 'minify-css' after 61 ms
[18:54:39] Starting 'copy-css'...
[18:54:39] Finished 'copy-css' after 1.86 ms

but the copy operation doesn't execute probably because it executes even before the file to be copied is not generated

但是复制操作可能不会执行,因为它甚至在没有生成要复制的文件之前执行

Even though I have mentioned minify-css as dependency for copy-css task, it seems it is not following that convention in this case.

尽管我已经提到minify-css作为copy-css任务的依赖,但在这种情况下它似乎没有遵循该约定。

When gulp copy-css is run another time, this time the file is copied because the file is already generated from previously executed command. But this would beat the purpose when the code is being used in production.

当gulp copy-css再次运行时,这次复制文件,因为该文件已经从先前执行的命令生成。但是,当代码在生产中使用时,这将超过目的。

3 个解决方案

#1


2  

change both the tasks as below

更改以下任务

gulp.task('minify-css', function () {
    return  gulp.src(['src/test/test.css])
   .pipe(concat('test1.css'))
   .pipe(minifyCSS())
   .pipe(gulp.dest('src/source/'))
   .pipe(filesize())    
});

gulp.task('copy-css',['minify-css'], function () {
   return gulp.src('src/source/*.css')
   .pipe(gulp.dest('src/dest/'));

});

add return so that next task runs after first one runs, else your copy-csss is running even before minify-css is finished.. hence the error...

添加return以便在第一个任务运行后运行下一个任务,否则你的copy-csss甚至在minify-css完成之前就会运行..因此错误......

#2


1  

I would check out this question, as it seems you might have to add return keyword before gulp.src(['src/test/test.css])...

我会查看这个问题,因为看起来你可能需要在gulp.src(['src / test / test.css]之前)添加return关键字...

#3


1  

Try with a callback in the on('finish') event:

尝试使用on('finish')事件中的回调:

gulp.task('minify-css', function (cb) {

   gulp.src(['src/test/test.css'])
    .pipe(concat('test1.css'))
    .pipe(minifyCSS())
    .pipe(gulp.dest('src/source/'))
    .pipe(filesize())
    .on('finish', function() {
        // All tasks should now be done
        return cb();
    })

});

gulp.task('copy-css',['minify-css'], function () {

   gulp.src('src/source/*.css')
     .pipe(gulp.dest('src/dest/'));

});

PS: You also go an syntax error in your gulp.src array at the very top, just a missing quote ' here gulp.src(['src/test/test.css])

PS:你也在你的gulp.src数组中的语法错误,只是缺少引用'这里gulp.src(['src / test / test.css])

#1


2  

change both the tasks as below

更改以下任务

gulp.task('minify-css', function () {
    return  gulp.src(['src/test/test.css])
   .pipe(concat('test1.css'))
   .pipe(minifyCSS())
   .pipe(gulp.dest('src/source/'))
   .pipe(filesize())    
});

gulp.task('copy-css',['minify-css'], function () {
   return gulp.src('src/source/*.css')
   .pipe(gulp.dest('src/dest/'));

});

add return so that next task runs after first one runs, else your copy-csss is running even before minify-css is finished.. hence the error...

添加return以便在第一个任务运行后运行下一个任务,否则你的copy-csss甚至在minify-css完成之前就会运行..因此错误......

#2


1  

I would check out this question, as it seems you might have to add return keyword before gulp.src(['src/test/test.css])...

我会查看这个问题,因为看起来你可能需要在gulp.src(['src / test / test.css]之前)添加return关键字...

#3


1  

Try with a callback in the on('finish') event:

尝试使用on('finish')事件中的回调:

gulp.task('minify-css', function (cb) {

   gulp.src(['src/test/test.css'])
    .pipe(concat('test1.css'))
    .pipe(minifyCSS())
    .pipe(gulp.dest('src/source/'))
    .pipe(filesize())
    .on('finish', function() {
        // All tasks should now be done
        return cb();
    })

});

gulp.task('copy-css',['minify-css'], function () {

   gulp.src('src/source/*.css')
     .pipe(gulp.dest('src/dest/'));

});

PS: You also go an syntax error in your gulp.src array at the very top, just a missing quote ' here gulp.src(['src/test/test.css])

PS:你也在你的gulp.src数组中的语法错误,只是缺少引用'这里gulp.src(['src / test / test.css])