繁体   English   中英

Gulp任务发布

[英]Gulp task to make a release

我是Gulp的新手。 我正在尝试将发行版本发布到dist文件夹。 项目结构如下:

_untitled/
 └── app/
     └── img/
     └── jade/
     └── script/
     └── style/
     └── vendor/
     └── 404.html
     └── index.html
 └── node_modules/
 └── gulpfile.js
 └── package.json

这是我的gulpfile.js

var gulp = require('gulp');
var $ = require('gulp-load-plugins')();

//-------------------- jade
gulp.task('jade', function () {
    return gulp.src('app/jade/*.jade')
    .pipe($.jade({
        pretty: true
    }))
    .on('error', console.log)
    .pipe(gulp.dest('app'))
    .pipe($.size());
});

//-------------------- style
gulp.task('style', function () {
    return gulp.src('app/style/sass/main.scss')
    .pipe($.rubySass({
        style: 'expanded',
        'sourcemap=none': true,
        noCache: true
    }))
    .pipe($.autoprefixer({
            browsers: ['last 2 versions']
        }))
    .pipe(gulp.dest('app/style'))
    .pipe($.size());
});

//-------------------- script
gulp.task('script', function () {
    return gulp.src('app/script/**/*.js')
    .pipe($.jshint())
    .pipe($.jshint.reporter(require('jshint-stylish')))
    .pipe($.size());
});

//-------------------- htmlDist
gulp.task('htmlDist', function () {
    return gulp.src('app/**/*.html')
    .pipe(gulp.dest('dist'))
    .pipe($.size());
});

//-------------------- styleDist
gulp.task('styleDist', function () {
    return gulp.src('app/style/**/*.css')
    .pipe($.concat('main.css'))
    .pipe($.csso())
    .pipe(gulp.dest('dist/style'))
    .pipe($.size());
});

//-------------------- scriptDist
gulp.task('scriptDist', function () {
    return gulp.src('app/script/**/*.js')
    .pipe($.uglify())
    .pipe(gulp.dest('dist/script'))
    .pipe($.size());
});

//-------------------- cleanDist
gulp.task('cleanDist', function () {
    var del = require('del');
    return del('dist');
});

//-------------------- build
gulp.task('build', ['jade', 'style', 'script']);

//-------------------- buildDist
gulp.task('buildDist', ['htmlDist', 'styleDist', 'scriptDist']);

//-------------------- release
gulp.task('release', ['cleanDist', 'build', 'buildDist']);

这样,当我输入gulp release我有一个ok dist文件夹,但index.html为空且大小为0 KB 然后,当我尝试再次gulp release时(第二次,已存在dist文件夹),我在dist文件夹中只有404.html文件,并且出现以下输出错误:

gulp release
[02:36:14] Using gulpfile D:\Coding\_untitled\gulpfile.js
[02:36:14] Starting 'cleanDist'...
[02:36:14] Finished 'cleanDist' after 52 ms
[02:36:14] Starting 'jade'...
[02:36:15] Starting 'style'...
[02:36:16] Starting 'script'...
[02:36:17] Starting 'htmlDist'...
[02:36:17] Starting 'styleDist'...
[02:36:17] Starting 'scriptDist'...
[02:36:17] all files 38 B
[02:36:17] Finished 'script' after 1.19 s
[02:36:17] all files 432 B
[02:36:17] Finished 'jade' after 2.88 s

stream.js:94
      throw er; // Unhandled stream error in pipe.
            ^
Error: ENOENT, chmod 'D:\Coding\_untitled\dist\script\main.js'

所有这些之后,如果我尝试gulp release (将排在第三位),则dist文件夹甚至都不会出现。 看起来当dist文件夹不存在时,此任务运行良好(不计算空的index.html ),然后它就出错了。 好吧,我需要执行此任务以首先删除整个dist文件夹,然后构建发行版。 单独运行gulp buildDist可以正常工作( index.html是正常的)。

这是我的第一篇文章,我已经尽力做到正确。 将不胜感激。

当您在gulp中指定依赖项时,它们将并行运行,而不是顺序运行。 这意味着您的jadestylescript任务 htmlDiststyleDistscriptDist任务同时运行。

您应该使用运行顺序包来确保任务按顺序运行。

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

gulp.task('build', function(callback) {
  runSequence('cleanDist', 'build', 'buildDist', callback);
});

根据Gulp文档 ,您不需要任何第三方包装,

只需创建相关任务:

var gulp = require('gulp');

// takes in a callback so the engine knows when it'll be done
gulp.task('one', function(cb) {
    // do stuff -- async or otherwise
    cb(err); // if err is not null and not undefined, the run will stop, and note that it failed
});

// identifies a dependent task must be complete before this one begins
gulp.task('two', ['one'], function() {
    // task 'one' is done now
});

gulp.task('default', ['one', 'two']);

暂无
暂无

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

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