1

如果我想在 diff 中注入 Js 文件。位置,例如<head> herehere </body>。我需要像这里描述的那样为注射设置名称: https ://github.com/klei/gulp-inject#method-2-use-gulp-injects-name-option

  <!-- head:js -->
  <!-- only importantFile.js will be injected here -->
  <!-- endinject -->

我怎样才能修改这个选择器,我不必为每个文件命名。例如,获取所有包含 *_important.js 的文件

.pipe(inject(gulp.src('./src/importantFile.js', {read: false}), {name: 'head'}))

有没有更好的方法。例如,喜欢添加 s.th。在 javascript 文件中或将其命名为 orderModule.above.js、googleAnalytics.below.js

4

2 回答 2

0
gulp.task('dev', function () {
  var target = gulp.src('./index.html');
  return target
    .pipe(inject(gulp.src('_MAIN_CSS_FILES_', {read: false})))
    .pipe(inject(gulp.src('_BOWER_CSS_FILES_', {read: false}), {name: 'bower'}))
    .pipe(inject(gulp.src('_MAIN_JS_FILES_', {read: false})))
    .pipe(inject(gulp.src('_BOWER_JS_FILES_', {read: false}), {name: 'bower'}))
    .pipe(gulp.dest("./dest/"));
});

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Index</title>

    <!-- inject:css -->
    <!-- built css files will go here... -->
    <!-- endinject -->

    <!-- bower:css -->
    <!-- bower installed css files will go here... -->
    <!-- endinject -->

</head>
<body>


<!-- bower:js -->
<!-- bower installed js files will go here... -->
<!-- endinject -->

<!-- inject:js -->
<!-- built js files will go here... -->
<!-- endinject -->

</body>
</html>
于 2017-03-04T15:53:45.190 回答
0

gulp 文档中:

gulp.src(globs[, options])- 发出匹配提供的 glob 或 glob 数组的文件。

因此,您有很大的自由来定义您希望如何区分“重要”文件和其他文件。

.pipe(inject(gulp.src('*_important.js')))
.pipe(inject(gulp.src('*.head.js')))

gulp.src等等然后在第二次调用中提供这个模式的否定

.pipe(inject(gulp.src(['*.js', '!*_important.js'])))
.pipe(inject(gulp.src(['*.js', '!*.head.js'])))
于 2015-09-20T14:05:10.267 回答