简体   繁体   English

我如何将吞咽任务合并为一个?

[英]How do I consolidate my gulp tasks into one?

My project uses two different code bases - one for desktop and one for mobile. 我的项目使用两种不同的代码库-一种用于桌面,另一种用于移动。 I'd like to use the same gulp "build" task for both. 我想对两者使用相同的“完成”任务。 My gulp "build" task for desktop looks like this: 我的桌面 “吞噬”任务看起来像这样:

gulp.task('build', function() {
  return gulp.src([
    'file1.js',
    'file2.js',
    'file3.js',
    'file4.js',
    'file5.js',
    'file6.js',
    'file7.js',
    'file8.js',
    'file9.js',
    'file10.js',
  ])
  .pipe(concat('bundle.js'))
  .pipe(uglify())
  .pipe(gulp.dest('desktop/dist'));
});

The only differences between these tasks is: 这些任务之间的唯一区别是:

1) the list of files being concatenated into bundle.js 1)被串联到bundle.js中的文件列表

2) the prefixing dist/bundle.js with "desktop/" or "mweb/" based on which platform I'm compiling code for 2)基于我正在为其编译代码的平台,将dist / bundle.js前缀为“ desktop /”或“ mweb /”

Ideally, the task would be generic enough to where I could pass in a different array of files (perhaps using a function argument and/or variables), but am struggling with how to achieve this using gulp. 理想情况下,该任务应该足够通用,以至于我可以传递不同的文件数组(可能使用函数自变量和/或变量),但是却在如何使用gulp实现这一目标方面感到吃力。

What I've tried so far: 到目前为止,我已经尝试过:

My first approach was to create two separate build tasks, "desktop-build" and "mobile-build", but this approach feels bloated and redundant. 我的第一种方法是创建两个单独的构建任务,即“桌面构建”和“移动构建”,但是这种方法感到肿且多余。

I recommend looking at this question Pass Parameter to Gulp Task . 我建议查看此问题,将参数传递给Gulp Task You can define, for example, an environment variable object which contains an environment name, prefix, file list, and any other useful information, then check for a flag like, gulp build --desktop to use the corresponding environment attributes in the build process, with the most common use case as a default environment. 例如,您可以定义一个environment变量对象,该对象包含环境名称,前缀,文件列表和任何其他有用的信息,然后检查诸如gulp build --desktop类的标志以在构建过程中使用相应的环境属性,最常见的用例是默认环境。 Seems the easiest and most flexible option. 似乎是最简单,最灵活的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM