做一个大文件:调用gulp.parallel时出错

ifsvaxew  于 2022-12-08  发布在  Gulp
关注(0)|答案(2)|浏览(145)

我正在读《Getting Started With Gulp》(《Getting Started With Gulp》)一书(该书于2015年1月出版),但我意识到,由于Gulp的开发进展相当快,它可能已经有点过时了。
以下是我的吞咽档案:

// Modules & Plugins
var gulp = require('gulp');
var concat = require('gulp-concat');
var myth = require('gulp-myth');
var uglify = require('gulp-uglify'); // newly added
var jshint = require('gulp-jshint'); // newly added
var imagemin = require('gulp-imagemin');
// let's add two node.js modules:
var connect = require('connect');
// var serve = require('serve-static');
var browsersync = require('browser-sync');
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var plumber = require('gulp-plumber');

// Tasks
// styles task
gulp.task('styles', function(){
    return gulp.src('app/css/*.css')
    // now we add our pipes:
        .pipe(plumber())
        .pipe(concat('all.css'))
        .pipe(myth())
        .pipe(gulp.dest('dist'));
});

// scripts task
gulp.task('scripts', function() {
    return gulp.src('app/js/*.js')
        .pipe(jshint())
        .pipe(jshint.reporter('default'))
        .pipe(concat('all.js'))
        .pipe(uglify())
        .pipe(gulp.dest('dist'));
});

// images task
gulp.task('images', function() {
    return gulp.src('app/img/*')
      .pipe(imagemin())
      .pipe(gulp.dest('dist/img'));
});

// browsersync Task:
gulp.task('browsersync', function(cb) {
    return browsersync({
      server: {
        baseDir:'./'
      }
    }, cb);
});

// browserify Task:
gulp.task('browserify', function() {
    return browserify('./app/js/app.js')
      .bundle()
      .pipe(source('bundle.js'))
      .pipe(gulp.dest('dist'));
});

// watch task:
gulp.task('watch', function() {
  gulp.watch('app/css/*.css',
   gulp.series('styles', browsersync.reload));
  gulp.watch('app/js/*.js',
   gulp.series('scripts', browsersync.reload));
  gulp.watch('app/img/*',
   gulp.series('images', browsersync.reload));
});

// Default Task
gulp.task('default', ['styles', 'scripts', 'images', 'browsersync', 'browserify', 'watch']);

编辑:在更改最后一行gulp.task后,它运行了,但我仍然得到以下错误:

[13:51:21] Starting 'watch'...
[13:51:21] 'watch' errored after 146 μs
[13:51:21] TypeError: undefined is not a function
    at Gulp.<anonymous> (/Volumes/BigMan/Code/javascript/gulp-book/gulpfile.js:79:9)
    at module.exports (/Volumes/BigMan/Code/javascript/gulp-book/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:34:7)
    at Gulp.Orchestrator._runTask (/Volumes/BigMan/Code/javascript/gulp-book/node_modules/gulp/node_modules/orchestrator/index.js:273:3)
    at Gulp.Orchestrator._runStep (/Volumes/BigMan/Code/javascript/gulp-book/node_modules/gulp/node_modules/orchestrator/index.js:214:10)
    at Gulp.Orchestrator.start (/Volumes/BigMan/Code/javascript/gulp-book/node_modules/gulp/node_modules/orchestrator/index.js:134:8)
    at /usr/local/lib/node_modules/gulp/bin/gulp.js:129:20
    at process._tickCallback (node.js:355:11)
    at Function.Module.runMain (module.js:503:11)
    at startup (node.js:129:16)
    at node.js:814:3
zd287kbt

zd287kbt1#

我正在阅读同一本书,发现它不起作用。我注意到你所要做的就是把Gulp更新到4.0
为什么不工作
为了实现Gulp的并行和串行功能,必须安装Gulp 4.0

如何安装

全局安装

npm uninstall gulp -g
npm install gulpjs/gulp-cli#4.0 -g

yarn global add gulpjs/gulp.git#4.0

从项目内部

npm uninstall gulp
npm install gulpjs/gulp.git#4.0 --save-dev

yarn add gulpjs/gulp.git#4.0

我还找到了一堆教程,讨论Gulp4.0的新特性
我认为这是因为这本书是在考虑即将到来的特性的情况下写的。我认为有时候不能假设项目会发展得那么快。我仍然不明白的是gulp.parallel和仅仅传递一个数组之间的区别。
http://fettblog.eu/gulp-4-parallel-and-series/
还有一件事:
在gulp 4中,必须为监视任务指定parallel/series:

gulp.task('watch', function() {
    gulp.watch('app/css/*.css', gulp.parallel('styles'));
    gulp.watch('app/jss/*.js', gulp.parallel('scripts'));
    gulp.watch('app/img/*', gulp.parallel('images'));
})
mccptt67

mccptt672#

我不知道gulp.parallel或gulp.series是从哪里来的,但是你应该可以通过将代码更改为以下代码来完成同样的事情:

// watch task:
gulp.task('watch', function() {
    gulp.watch('app/css/*.css', ['styles', browsersync.reload]);
    gulp.watch('app/js/*.js', ['scripts', browsersync.reload]);
    gulp.watch('app/img/*', ['images', browsersync.reload]);
});

gulp.task('default', ['styles', 'scripts', 'images', 'browsersync', 'browserify', 'watch']);

据我所知,.series和.parallel不存在。一旦我做了这些更新,它就可以工作了。
如果您需要连续运行任务,我已经在本模块中获得了很好的运气:https://github.com/OverZealous/run-sequence。您可以将监视任务更改为:

var runSequence = require('run-sequence');

// watch task:
gulp.task('watch', function() {
  gulp.watch('app/css/*.css', runSequence('styles', browsersync.reload));
  gulp.watch('app/js/*.js', runSequence('scripts', browsersync.reload));
  gulp.watch('app/img/*', runSequence('images', browsersync.reload));
});

相关问题