gulp README

The project's README from github.com/gulpjs/gulp. As one markdown file: README.md.

The streaming build system

NPM version Downloads Build Status Coveralls Status

What is gulp?

Installation

Follow our Quick Start guide.

Roadmap

Find out about all our work-in-progress and outstanding issues at https://github.com/orgs/gulpjs/projects.

Documentation

Check out the Getting Started guide and API docs on our website!

Excuse our dust! All other docs will be behind until we get everything updated. Please open an issue if something isn't working.

Sample gulpfile.js

This file will give you a taste of what gulp does.

var gulp = require('gulp');
var less = require('gulp-less');
var babel = require('gulp-babel');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var cleanCSS = require('gulp-clean-css');
var del = require('del');

var paths = {
  styles: {
    src: 'src/styles/**/*.less',
    dest: 'assets/styles/'
  },
  scripts: {
    src: 'src/scripts/**/*.js',
    dest: 'assets/scripts/'
  }
};

/* Not all tasks need to use streams, a gulpfile is just another node program
 * and you can use all packages available on npm, but it must return either a
 * Promise, a Stream or take a callback and call it
 */
function clean() {
  // You can use multiple globbing patterns as you would with `gulp.src`,
  // for example if you are using del 2.0 or above, return its promise
  return del([ 'assets' ]);
}

/*
 * Define our tasks using plain functions
 */
function styles() {
  return gulp.src(paths.styles.src)
    .pipe(less())
    .pipe(cleanCSS())
    // pass in options to the stream
    .pipe(rename({
      basename: 'main',
      suffix: '.min'
    }))
    .pipe(gulp.dest(paths.styles.dest));
}

function scripts() {
  return gulp.src(paths.scripts.src, { sourcemaps: true })
    .pipe(babel())
    .pipe(uglify())
    .pipe(concat('main.min.js'))
    .pipe(gulp.dest(paths.scripts.dest));
}

function watch() {
  gulp.watch(paths.scripts.src, scripts);
  gulp.watch(paths.styles.src, styles);
}

/*
 * Specify if tasks run in series or parallel using `gulp.series` and `gulp.parallel`
 */
var build = gulp.series(clean, gulp.parallel(styles, scripts));

/*
 * You can use CommonJS `exports` module notation to declare tasks
 */
exports.clean = clean;
exports.styles = styles;
exports.scripts = scripts;
exports.watch = watch;
exports.build = build;
/*
 * Define default task that can be called by just running `gulp` from cli
 */
exports.default = build;

Use latest JavaScript version in your gulpfile

Gulp provides a wrapper that will be loaded in your ESM code, so you can name your gulpfile as gulpfile.mjs or with "type": "module" specified in your package.json file.

And here's the same sample from above written in ESNext.

import { src, dest, watch } from 'gulp';
import less from 'gulp-less';
import babel from 'gulp-babel';
import concat from 'gulp-concat';
import uglify from 'gulp-uglify';
import rename from 'gulp-rename';
import cleanCSS from 'gulp-clean-css';
import {deleteAsync} from 'del';

const paths = {
  styles: {
    src: 'src/styles/**/*.less',
    dest: 'assets/styles/'
  },
  scripts: {
    src: 'src/scripts/**/*.js',
    dest: 'assets/scripts/'
  }
};

/*
 * For small tasks you can export arrow functions
 */
export const clean = () => deleteAsync([ 'assets' ]);

/*
 * You can also declare named functions and export them as tasks
 */
export function styles() {
  return src(paths.styles.src)
    .pipe(less())
    .pipe(cleanCSS())
    // pass in options to the stream
    .pipe(rename({
      basename: 'main',
      suffix: '.min'
    }))
    .pipe(dest(paths.styles.dest));
}

export function scripts() {
  return src(paths.scripts.src, { sourcemaps: true })
    .pipe(babel())
    .pipe(uglify())
    .pipe(concat('main.min.js'))
    .pipe(dest(paths.scripts.dest));
}

 /*
  * You could even use `export as` to rename exported tasks
  */
function watchFiles() {
  watch(paths.scripts.src, scripts);
  watch(paths.styles.src, styles);
}
export { watchFiles as watch };

const build = gulp.series(clean, gulp.parallel(styles, scripts));
/*
 * Export a default task
 */
export default build;

Incremental Builds

You can filter out unchanged files between runs of a task using the gulp.src function's since option and gulp.lastRun:

const paths = {
  ...
  images: {
    src: 'src/images/**/*.{jpg,jpeg,png}',
    dest: 'build/img/'
  }
}

function images() {
  return gulp.src(paths.images.src, {since: gulp.lastRun(images)})
    .pipe(imagemin())
    .pipe(gulp.dest(paths.images.dest));
}

function watch() {
  gulp.watch(paths.images.src, images);
}

Task run times are saved in memory and are lost when gulp exits. It will only save time during the watch task when running the images task for a second time.

Want to contribute?

Anyone can help make this project better - check out our Contributing guide!

gulp (gulpjs/gulp)

Searching Database…

32.9K ·GitHub
这是什么?gulp 是一个 JavaScript 工具包,它通过一连串流式转换处理文件,从而自动完成开发流程中重复或耗时的任务。
gulp 只能用于 Node.js 项目,还是其他技术栈也能用?它本质上是一个 Node.js 库,你通过 gulpfile 编写脚本来使用。不过由于主流 IDE 直接集成了它,人们也用它为 PHP、.NET 和 Java 项目运行构建任务。
我该如何在项目中开始使用 gulp?用 npm 全局安装 gulp-cli,并将 gulp 作为开发依赖安装,然后编写 gulpfile.js(或 gulpfile.mjs),使用 gulp.src、gulp.dest 和流来定义你的任务。
gulp 需要哪个版本的 Node.js?gulp 要求 Node.js 10.13.0 或更高版本,这一点在其 engines 字段中有声明。
gulp 会记住两次构建之间哪些文件发生了变化吗?在任务运行时,它可以借助 gulp.src 的 since 选项和 gulp.lastRun 跳过未变化的文件,但运行时间只保存在内存中,gulp 退出后就会重置。因此这一优势主要体现在 watch 任务持续运行期间。
gulp 在 Windows、macOS 和 Linux 上的表现一致吗?是的。gulp 是一个 Node.js 命令行工具和库,所以只要能运行 Node.js 的地方它都能以相同方式运行,包括 Windows、macOS 和 Linux。
gulp 与手写自定义构建脚本有什么不同?gulp 使用 Node 流将文件依次经过各种转换,无需把中间文件写入磁盘;同时可以使用 3000 多个经过筛选的 npm 插件,无需重复造轮子来实现文件处理逻辑。
我能用现代 ES 模块语法编写 gulpfile 吗?可以。gulp 提供了一个包装层,你可以将文件命名为 gulpfile.mjs,或在 package.json 中把 type 设为 module,然后直接使用 ESNext 语法。
gulp 有多少个插件可用?其生态中有 3000 多个经过筛选的插件,发布在 npm 上,用于流式处理文件转换。
What are similar projects?husky, nodemon, cz-cli, task, ordewell, truffle, yeoman, lint-staged
Creator
gulpjs
Size
1.1 MB
Stars
32.9K
Age
13y
Commits · 90 Days