TypeScript 写 gulp 任务

一、环境与项目配置

全局安装 gulp-cli 以支持命令行操作
shell 复制代码
npm i -g gulp-cli
初始化 npm 包管理文件
shell 复制代码
npm init -y

(-y可跳过交互直接生成默认配置)

项目中安装 gulp 核心库
css 复制代码
npm i gulp -D
安装 typescript 、 ts-node 和 @types/gulp
  • 让 gulp 可自动识别并编译 ts 文件
  • gulp 的代码提示
css 复制代码
npm i typescript ts-node @types/gulp -D
TypeScript 配置

创建 tsconfig.json 文件,设置模块规范为 CommonJS(Gulp 默认使用 CommonJS 模块)

json 复制代码
{
  "compilerOptions": {
    "module": "CommonJS", 
    "esModuleInterop": true, 
    "target": "ES2018", 
    "outDir": "./dist", 
    "rootDir": ".", 
    "strict": true 
    } 
}

二、编写测试 gulp 任务

项目根目录新建 gulpfile.ts

编写默认任务和一个拷贝指定类型文件的任务

ts 复制代码
// #region -------- 默认任务
function defaultTask(cb: () => void): void {
  console.log("Default task executed");
  cb();
}

export default defaultTask;

// #region -------- 拷贝任务
import { src, dest } from 'gulp';
function copy() {
  return src('assets/config/*.json')
    .pipe(dest('temp/'));
}

export { copy };

三、编写测试 gulp 任务

执行默认任务

在命令行中测试一下。执行 gulp

csharp 复制代码
$ gulp
[11:12:32] Loaded external module: ts-node/register
[11:12:33] Using gulpfile ./gulpfile.ts
[11:12:33] Starting 'default'...
Default task executed
[11:12:33] Finished 'default' after 1.3 ms
执行指定任务

执行 gulp copy 以执行 copy 任务

csharp 复制代码
$ gulp copy
[11:17:37] Loaded external module: ts-node/register
[11:17:37] Using gulpfile ./gulpfile.ts
[11:17:37] Starting 'copy'...
[11:17:37] Finished 'copy' after 15 ms
查看任务列表

执行 gulp --tasks

sql 复制代码
$ gulp --tasks
[11:21:21] Loaded external module: ts-node/register
Tasks for ./gulpfile.ts
├── copy
└── default
相关推荐
We་ct5 小时前
LeetCode 5. 最长回文子串:DP + 中心扩展
前端·javascript·算法·leetcode·typescript
Wect16 小时前
LeetCode 97. 交错字符串:动态规划详解
前端·算法·typescript
漫游的渔夫21 小时前
前端开发者做多步 Agent:别让 AI 边想边乱跑,用 Plan-Act-Observe 稳住 4 步任务
前端·人工智能·typescript
Elastic 中国社区官方博客1 天前
用于 JavaScript 和 TypeScript 的 ES|QL 查询构建器:流式、类型安全的查询构建
大数据·javascript·数据库·elasticsearch·搜索引擎·typescript·全文检索
小爬的老粉丝1 天前
把 Office 预览搬进浏览器:一次仍在继续的纯前端长跑
前端·typescript·docx·ppt·doc·pptx·office预览
Wect2 天前
LeetCode 5. 最长回文子串:DP + 中心扩展
前端·算法·typescript
漫游的渔夫2 天前
前端开发者做 Agent:别写成一次请求,用 5 步受控循环防止 AI 乱跑
前端·人工智能·typescript
垦利不2 天前
TS基础篇
开发语言·前端·typescript
涵涵(互关)2 天前
GoView各项目文件中的相关语法3
前端·vue.js·typescript
紫微AI2 天前
前端文本测量成了卡死一切创新的最后瓶颈,pretext实现突破了
前端·人工智能·typescript