Gulp前端构建

走进gulp4的世界

2018-01-12  本文已影响17人  我是渐渐呀
更多内容欢迎来到博客 :https://imjianjian.github.io

gulp简介

安装

要使用gulp要保证你的电脑上要有node环境

官方建议你这么安装它

$ npm install gulp-cli -g
$ npm install gulp -D
$ touch gulpfile.js
$ gulp --help

更新

API

gulp4在gulp3的基础上新增了几个函数,但它的使用依旧简单方便。一共如下10个:

官方说明API说明:https://github.com/gulpjs/gulp/blob/master/docs/API.md

task(parallel and series)

gulp不再能够通过数组形式传入任务,再gulp4中你需要使用gulp.series()和gulp.parallel()来执行他们。例如:

gulp.task('default',gulp.parallel('taskA','taskB'));//并行执行
gulp.task('default',gulp.series('taskA','taskB'));//按顺序执行

parallel和series函数可以接受函数作为参数,这意味着我们可以将任务用独立函数表示:

funcion taskA(){...}
funcion taskB(){...}

gulp.task(taskA);//最简单的方式
gulp.task('i-am-taskB',taskB);//为taskB添加任务名
gulp.task('taskA-taskB', gulp.series(taskA, taskB));//序列执行
gulp.task('taskA-taskB', gulp.parallel(taskA,taskB));//并行运行

而且当我们执行序列化任务时,面板的输出也更加清晰

PS D:\github\resume> gulp
[21:49:59] Using gulpfile D:\project\gulpfile.js
[21:49:59] Starting 'default'...
[21:49:59] Starting 'copyfile'...
[21:49:59] Finished 'copyfile' after 90 ms
[21:49:59] Starting 'minifyhtml'...
[21:49:59] Finished 'minifyhtml' after 171 ms
[21:49:59] Starting 'minifyCSS'...
[21:50:00] Finished 'minifyCSS' after 353 ms
[21:50:00] Starting 'uglyfyJS'...
[21:50:01] Finished 'uglyfyJS' after 1.43 s
[21:50:01] Finished 'default' after 2.05 s

gulp.lastRun()

语法:
gulp.lastRun(taskName,timeResolution)
参数:
taskName:任务名
timeResolution(可选):返回的时间戳的精度

例如

gulp.lastRun('someTask', 1000) // 1426000004000.
gulp.lastRun('someTask', 100) // 1426000004300.

gulp.tree()

语法:
gulp.tree(option)
options:
{
    deep:true/false //是否遍历每个子任务
}

示例gulpfile.js

gulp.task('one', function(done) {
  // do stuff
  done();
});

gulp.task('two', function(done) {
  // do stuff
  done();
});

gulp.task('three', function(done) {
  // do stuff
  done();
});

gulp.task('four', gulp.series('one', 'two'));

gulp.task('five',
  gulp.series('four',
    gulp.parallel('three', function(done) {
      // do more stuff
      done();
    })
  )
);

实例输出

gulp.tree()

// output: [ 'one', 'two', 'three', 'four', 'five' ]

gulp.tree({ deep: true })

/*output: [
   {
      "label":"one",
      "type":"task",
      "nodes":[]
   },
   {
      "label":"two",
      "type":"task",
      "nodes":[]
   },
   {
      "label":"three",
      "type":"task",
      "nodes":[]
   },
   {
      "label":"four",
      "type":"task",
      "nodes":[
          {
            "label":"<series>",
            "type":"function",
            "nodes":[
               {
                  "label":"one",
                  "type":"task",
                  "nodes":[]
               },
               {
                  "label":"two",
                  "type":"task",
                  "nodes":[]
               }
            ]
         }
      ]
   },
   {
      "label":"five",
      "type":"task",
      "nodes":[
         {
            "label":"<series>",
            "type":"function",
            "nodes":[
               {
                  "label":"four",
                  "type":"task",
                  "nodes":[
                     {
                        "label":"<series>",
                        "type":"function",
                        "nodes":[
                           {
                              "label":"one",
                              "type":"task",
                              "nodes":[]
                           },
                           {
                              "label":"two",
                              "type":"task",
                              "nodes":[]
                           }
                        ]
                     }
                  ]
               },
               {
                  "label":"<parallel>",
                  "type":"function",
                  "nodes":[
                     {
                        "label":"three",
                        "type":"task",
                        "nodes":[]
                     },
                     {
                        "label":"<anonymous>",
                        "type":"function",
                        "nodes":[]
                     }
                  ]
               }
            ]
         }
      ]
   }
]
*/
上一篇下一篇

猜你喜欢

热点阅读