Gulp非常适合用于以下场景:
npm install --global gulp-cli
mkdir my-project
cd my-project
npm init
npm install --save-dev gulp
创建一个简单的gulpfile.js
:
const gulp = require('gulp');
const less = require('gulp-less');
const cleanCSS = require('gulp-clean-css');
const del = require('del');
const paths = {
styles: {
src: 'src/styles/**/*.less',
dest: 'assets/styles/'
}
};
function clean() {
return del(['assets']);
}
function styles() {
return gulp.src(paths.styles.src)
.pipe(less())
.pipe(cleanCSS())
.pipe(gulp.dest(paths.styles.dest));
}
function watch() {
gulp.watch(paths.styles.src, styles);
}
const build = gulp.series(clean, styles);
exports.clean = clean;
exports.styles = styles;
exports.watch = watch;
exports.build = build;
exports.default = build;
Gulp 4.0引入了series
和parallel
方法,允许更灵活地组合任务:
const { series, parallel } = require('gulp');
function task1(cb) {
// do something
cb();
}
function task2(cb) {
// do something else
cb();
}
exports.default = series(task1, task2);
有关更多详细信息和高级用法,请参考Gulp的官方文档和社区资源。