0

也许比我有更多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 -blockusemin-de合并在一起?

帮助将不胜感激。提前致谢!

4

2 回答 2

0

JavascriptGulp中没有任何内容阻止您提取函数:

你的任务

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));
}
于 2017-03-08T16:31:25.383 回答
0

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

为了匹配app/index.htmlapp/de/index.html您可以使用 glob './app/{,de}/index.html'(也可以使用'./app/{,de/}index.html'or './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 入门,这是一个测试人员学习工具

于 2017-03-08T16:09:05.347 回答