我怎么能更有效地编码? - 一项吞咽任务

时间:2017-03-05 11:36:11

标签: gulp gulp-usemin

也许比我自己有更多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 -block与 usemin-德

非常感谢帮助。提前谢谢!

2 个答案:

答案 0 :(得分:0)

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

要仅匹配app/index.htmlapp/de/index.html,您可以使用glob './app/{,de}/index.html'(它也可以使用'./app/{,de/}index.html''./app/{.,de}/index.html')。

然后你的任务就是

gulp.task('usemin', ['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"));
});

这一项任务将./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(覆盖第一个输出文件)。

这是一个很好的glob primer,这里是tester learning tool

答案 1 :(得分:0)

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));
}