Gulp 我如何才能更有效地编写此代码狼吞虎咽的任务

Gulp 我如何才能更有效地编写此代码狼吞虎咽的任务,gulp,gulp-usemin,Gulp,Gulp Usemin,也许比我更有java脚本经验的人可以回答这个问题。到目前为止,我已经从“usemin”块中复制并粘贴了一个,如课程中所示。代码如下: gulp.task('useminTrigger', ['deleteDistFolder'], function() { gulp.start("usemin", "usemin-de"); }); gulp.task('usemin', ['styles', 'scripts'], function () { return gulp.src("./a

也许比我更有java脚本经验的人可以回答这个问题。到目前为止,我已经从“usemin”块中复制并粘贴了一个,如课程中所示。代码如下:

gulp.task('useminTrigger', ['deleteDistFolder'], function() {
  gulp.start("usemin", "usemin-de");
});

gulp.task('usemin', ['styles', 'scripts'], function () {
  return gulp.src("./app/index.html")
    .pipe(usemin({
      css: [function () {return rev()}, function () {return cssnano()}],
      js: [function () {return rev()}, function () {return uglify()}]
    }))
    .pipe(gulp.dest("./dist"));
});

gulp.task('usemin-de', ['styles', 'scripts'], function () {
  return gulp.src("./app/de/index.html")
    .pipe(usemin({
      css: [function () {return rev()}, function () {return cssnano()}],
      js: [function () {return rev()}, function () {return uglify()}]
    }))
    .pipe(gulp.dest("./dist/de"));
});
该脚本可以正常工作,但可能有一种更简单或更优雅的方式来编写它

对于优雅,我的意思是:有没有办法将usemin块与usemin de合并

非常感谢您的帮助。提前谢谢

如果在
gulp.src
中使用a(即,如果使用通配符选择要由
gulp.task
处理的文件),则源文件结构将一直保留到
gulp.dest

要匹配
app/index.html
app/de/index.html
可以使用glob
“./app/{,de}/index.html'
(它还可以使用
'./app/{,de/}index.html'
”./app/{,de}/index.html'

那么你的任务就是

gulp.task('usemin',['styles','scripts',function(){
返回gulp.src('./app/{,de}/index.html')
.管道(使用分钟({
css:[函数(){return rev()},函数(){return cssnano()}],
js:[函数(){return rev()},函数(){return uglify()}]
}))
.管道(大口目的地(“/距离”);
});
这一任务将把
/app/index.html
/app/de/index.html
作为源文件,并将输出文件
/dist/index.html
/dist/de/index.html

请注意,使用glob是必要的-如果您刚刚执行了
gulp.src(['./app/index.html','./app/de/index.html'))
则不会保留相关的文件结构。处理后的
/app/index.html
版本将写入
/dest/index.html
,然后处理后的
/app/de/index.html
版本将写入
/dest/index.html
(覆盖第一个输出文件)


这是一个很好的例子,这是一个。

Javascript或Gulp中没有任何内容阻止您提取函数:

您的任务

gulp.task('useminTrigger', ['deleteDistFolder'], function() {
    gulp.start("usemin", "usemin-de");
});

gulp.task('usemin', ['styles', 'scripts'], function () {

    return createMinificationPipe("./app/index.html", "./dist");
});

gulp.task('usemin-de', ['styles', 'scripts'], function () {
    return createMinificationPipe("./app/de/index.html", "./dist/de");
});
通用功能

function createMinificationPipe(src, dest){
    return gulp.src(src)
        .pipe(usemin({
          css: [function () {return rev()}, function () {return cssnano()}],
          js: [function () {return rev()}, function () {return uglify()}]
        }))
        .pipe(gulp.dest(dest));
}

欢迎来到SO!很难说你的问题是什么。你想在
/app/index.html
(输出到
/dist
)和
/app/de/index.html
(输出到
/dist/de
)上运行相同的任务吗?你好,亨利,谢谢你的回复。我想这是一个新手问题……:)我想我实际上并不理解我所写的所有代码,需要一些帮助来了解如何正确地告诉gulp我想要什么。我已经编辑了我的问题,你会看到更清楚。希望这能有所帮助。再次感谢。感谢你们的努力和这次巨大的分裂,这是我无法做到的。我还没有试过,但是如果我试过,我会更新的。谢谢henry,这是一个非常紧凑的解决方案。我已经读了一些关于这个主题的文章(你的链接)。我只使用regexp来搜索文件,这是一种新的体验,可以用它来为我编码。我试试看。