惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
有赞技术团队
有赞技术团队
罗磊的独立博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
U
Unit 42
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Recent Announcements
Recent Announcements
Y
Y Combinator Blog
Vercel News
Vercel News
Martin Fowler
Martin Fowler
V
V2EX
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
LangChain Blog
云风的 BLOG
云风的 BLOG
H
Hackread – Cybersecurity News, Data Breaches, AI and More
aimingoo的专栏
aimingoo的专栏
G
Google Developers Blog
The GitHub Blog
The GitHub Blog
N
Netflix TechBlog - Medium
Google DeepMind News
Google DeepMind News
雷峰网
雷峰网
阮一峰的网络日志
阮一峰的网络日志
F
Fortinet All Blogs

zodream梦想开源/个人编程日记

文件解析笔记-zodream梦想开源/个人编程日记 密码本开发笔记之读写与保存-zodream梦想开源/个人编程日记 SkiaSharp 把 pixel byte[] 转成 SKBitmap-zodream梦想开源/个人编程日记 nas 使用 Docker 安装 gogs-zodream梦想开源/个人编程日记 复制 android 手机中的文件到电脑-zodream梦想开源/个人编程日记 周报:寻找优质的周刊-zodream梦想开源/个人编程日记 开发日志:对Markdown的代码块新增引用来源支持-zodream梦想开源/个人编程日记 周报:怎么写技术类的教程文章-zodream梦想开源/个人编程日记 css display:flex 布局尺寸超出问题-zodream梦想开源/个人编程日记 周报:SEO优化的思考-zodream梦想开源/个人编程日记 Edge 浏览器不适用 Edge Image Viewer 打开图片 -zodream梦想开源/个人编程日记 SEO 学习笔记(一) 内容来源-zodream梦想开源/个人编程日记 PHP 实现双因素身份认证(2FA)-zodream梦想开源/个人编程日记 WPF MVVM 获取List 多选数据-zodream梦想开源/个人编程日记 Burp Suite 抓包-zodream梦想开源/个人编程日记 使用 indexnow 注意事项-zodream梦想开源/个人编程日记 Godot 使用字体图标 例如: Iconfont、FontAwesome-zodream梦想开源/个人编程日记 angular 15 对指定页面进行访问限制-zodream梦想开源/个人编程日记 CSS 使用 column-count 实现瀑布流出现内容分割的解决办法-zodream梦想开源/个人编程日记 input 确认按键事件在手机端不生效-zodream梦想开源/个人编程日记 C# 使用socket 进行通讯-zodream梦想开源/个人编程日记 Maui开发中Windows应用开启管理员权限-zodream梦想开源/个人编程日记 Maui 中自定义控件-zodream梦想开源/个人编程日记 angular 14 使用 ng-template 实现tree 结构显示-zodream梦想开源/个人编程日记 c# 动态安装和卸载dll-zodream梦想开源/个人编程日记 慎用 CompositionTarget.Rendering-zodream梦想开源/个人编程日记 c# 重写 c++ 程序笔记:数据初始化-zodream梦想开源/个人编程日记 源码编译 aseprite-zodream梦想开源/个人编程日记 记录一下字符串分隔split各语言之间的不同-zodream梦想开源/个人编程日记 c# Gzip解码无头内容-zodream梦想开源/个人编程日记
Gulp 实现根据参数处理不同任务-zodream梦想开源/个人编程日记
zodream · 2018-12-19 · via zodream梦想开源/个人编程日记

需求来源

Gulp 一般使用一个默认的任务,如果需要处理不同的文件就多建几个任务,但是这是一般的做法。

比如本站源码就分成多个模块,项目结构都一样,如果按一般的做法就是要建无数个任务,关键是项目是不断开发中的,不可能每次都新建,所以就想怎么自动切换处理不同文件夹。

解决方法

Gulp 调用不同的任务是通过 gulp task 这个调用task任务的,如果不存在task任务就会报错,那有没有可能 通过 gulp task 调用处理 task 这个文件夹呢?

1.首先获取 参数

var name = '';
if (process.argv && process.argv.length > 2) {
    name = process.argv[2]; // 这就获取到了参数
}

1234

2.然后根据参数改变目标文件夹

gulp.src(name + '/*.css')

1

但是 gulp task 是会调用 task 任务的,那么可以

3.临时生成 task 任务

gulp.task(name || 'z', build);

1

name 可能为空,但不能生成一个空命名的任务

4.最终结果

var gulp = require('gulp'),
    minCss = require('gulp-clean-css'),
    name = '';
if (process.argv && process.argv.length > 2) {
    name = process.argv[2]; // 这就获取到了参数
}
function cssTask() {
    return gulp.src('src/' name + "/*.css")
        .pipe(minCss())
        .pipe(gulp.dest('dist/'));
}
exports.cssTask = cssTask;
var build = gulp.series(gulp.parallel(cssTask));
gulp.task(mo, build);
gulp.task('default', build);

123456789101112131415

测试 压缩 test 文件夹下的css

转载请保留原文链接: https://zodream.cn/blog/id/24.html