Gulp基于文件注入

时间:2018-04-17 21:55:42

标签: gulp inject gulp-inject

我有这个结构

dependencies:
  flutter:
    sdk: flutter
  camera: ^0.1.2
  path_provider: ^0.4.0
  flutter_localizations:
      sdk: flutter

我想注入a1.js& a2.js文件到a.html。与b相同的情况。我怎么能和gulp一起做?我无法在inject()

上找到要写的内容
    ├── a
    │   ├── a.html
    │   └── a1.js        
    │   └── a2.js
    ├── b
    │   ├── b.html
    │   └── b1.js
    │   └── b2.js

1 个答案:

答案 0 :(得分:1)

有可能在gulp-inject内完全执行此操作,但以下内容非常简单:

const gulp = require('gulp');
const inject = require('gulp-inject');
const glob = require("glob");
const path = require('path');

// get your target html files as an array of strings
const htmlTargets = glob.sync("./**/*.html");

gulp.task('default', function () {

  htmlTargets.forEach(function (htmlTarget) {

    let directory = htmlTarget.split('/')[1];

    const sources = gulp.src(path.join('./', directory, '*.js'))

    return gulp.src(htmlTarget)
      .pipe(inject(sources))
      .pipe(gulp.dest('dist'));

  });
});