gulp: 常用代码片段 - cheatsheet

常用代码片段
更新于: 2025-05-20 22:42:42

常用功能

名称备注代码
clean清理 dist
// "del": "^8.0.0"

import { deleteAsync } from "del";
import gulp from "gulp";

gulp.task("clean", () => {
  return deleteAsync(["dist"]);
});

gulp.task("default", gulp.series("clean"));
shell执行 shell 命令
import gulp from "gulp";
import readPkg from "@jswork/read-pkg-json";
import { exec } from "child_process";

gulp.task("compress", () => {
  const pkg = readPkg();
  const distName = `${pkg.name}-${pkg.version}.tar.gz`;
  return exec(`cd dist && tar -czf ${distName} *`);
});

gulp.task("default", gulp.series("compress"));
shell pluginshell 插件
const gulp = require('gulp')
const shell = require('gulp-shell')

gulp.task('example', () => {
  return gulp
    .src('*.js', { read: false })
    .pipe(shell(['echo <%= file.path %>']))
})