• <bdo id='RnFw4'></bdo><ul id='RnFw4'></ul>
    1. <legend id='RnFw4'><style id='RnFw4'><dir id='RnFw4'><q id='RnFw4'></q></dir></style></legend>

      <i id='RnFw4'><tr id='RnFw4'><dt id='RnFw4'><q id='RnFw4'><span id='RnFw4'><b id='RnFw4'><form id='RnFw4'><ins id='RnFw4'></ins><ul id='RnFw4'></ul><sub id='RnFw4'></sub></form><legend id='RnFw4'></legend><bdo id='RnFw4'><pre id='RnFw4'><center id='RnFw4'></center></pre></bdo></b><th id='RnFw4'></th></span></q></dt></tr></i><div id='RnFw4'><tfoot id='RnFw4'></tfoot><dl id='RnFw4'><fieldset id='RnFw4'></fieldset></dl></div>

        <tfoot id='RnFw4'></tfoot>
      1. <small id='RnFw4'></small><noframes id='RnFw4'>

      2. 使用循环创建任务 [gulp]

        时间:2023-05-28
          <bdo id='7t8u1'></bdo><ul id='7t8u1'></ul>

          <legend id='7t8u1'><style id='7t8u1'><dir id='7t8u1'><q id='7t8u1'></q></dir></style></legend>
          <tfoot id='7t8u1'></tfoot>
          1. <small id='7t8u1'></small><noframes id='7t8u1'>

                  <tbody id='7t8u1'></tbody>
                  <i id='7t8u1'><tr id='7t8u1'><dt id='7t8u1'><q id='7t8u1'><span id='7t8u1'><b id='7t8u1'><form id='7t8u1'><ins id='7t8u1'></ins><ul id='7t8u1'></ul><sub id='7t8u1'></sub></form><legend id='7t8u1'></legend><bdo id='7t8u1'><pre id='7t8u1'><center id='7t8u1'></center></pre></bdo></b><th id='7t8u1'></th></span></q></dt></tr></i><div id='7t8u1'><tfoot id='7t8u1'></tfoot><dl id='7t8u1'><fieldset id='7t8u1'></fieldset></dl></div>

                  本文介绍了使用循环创建任务 [gulp]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在尝试基于 jsFiles 对象动态创建任务(缩小和连接).键将给出目标文件名,数组包含 src 文件.当我运行 gulp 时,我看到所有任务名称都在运行,但它只写入最后一个键,在这种情况下是 group2.js.这里有什么问题?

                  I'm trying to dynamically create tasks (minify and concat) based on jsFiles object. The key will give the destination file name and the array contains the src files. When I run gulp I see all the tasks names being ran but it only writes the last key which is group2.js in this case. What's wrong here?

                  // imports here
                  
                  var jsFiles = 
                  {
                      group1:[file1.js,file2.js],
                      group2:[file2.js,file3.js]
                  };
                  
                  for (var key in jsFiles)
                  {
                      gulp.task(key, function() {
                          return gulp.src(jsFiles[key])
                          .pipe(jshint())
                          .pipe(uglify())
                          .pipe(concat(key + '.js'))  // <- HERE
                          .pipe(gulp.dest('public/js'));
                      });
                  }
                  
                  var defaultTasks = [];
                  for (var key in jsFiles)
                  {
                      defaultTasks.push(key);
                  }
                  gulp.task('default', defaultTasks);
                  

                  推荐答案

                  另一种选择是使用函数式数组循环函数结合Object.keys,像这样:

                  Another option is to use functional array looping functions combined with Object.keys, like so:

                  var defaultTasks = Object.keys(jsFiles);
                  
                  defaultTasks.forEach(function(taskName) {
                     gulp.task(taskName, function() {
                         return gulp.src(jsFiles[taskName])
                            .pipe(jshint())
                            .pipe(uglify())
                            .pipe(concat(key + '.js'))
                            .pipe(gulp.dest('public/js'));
                     });
                  });
                  

                  我觉得这样更简洁一些,因为循环和函数在同一个地方,所以更容易维护.

                  I feel like this is a little cleaner, because you have the loop and the function in the same place, so it's easier to maintain.

                  这篇关于使用循环创建任务 [gulp]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 Gulp.js 和 globbing 模式就地修改文件(相同的目 下一篇:Uglify SyntaxError: Unexpected token: punc ())

                  相关文章

                  最新文章

                • <i id='QsKZc'><tr id='QsKZc'><dt id='QsKZc'><q id='QsKZc'><span id='QsKZc'><b id='QsKZc'><form id='QsKZc'><ins id='QsKZc'></ins><ul id='QsKZc'></ul><sub id='QsKZc'></sub></form><legend id='QsKZc'></legend><bdo id='QsKZc'><pre id='QsKZc'><center id='QsKZc'></center></pre></bdo></b><th id='QsKZc'></th></span></q></dt></tr></i><div id='QsKZc'><tfoot id='QsKZc'></tfoot><dl id='QsKZc'><fieldset id='QsKZc'></fieldset></dl></div>
                • <tfoot id='QsKZc'></tfoot>
                • <small id='QsKZc'></small><noframes id='QsKZc'>

                  • <bdo id='QsKZc'></bdo><ul id='QsKZc'></ul>

                  1. <legend id='QsKZc'><style id='QsKZc'><dir id='QsKZc'><q id='QsKZc'></q></dir></style></legend>