基于文件的Gulp注入

基于文件的Gulp注入,gulp,inject,gulp-inject,Gulp,Inject,Gulp Inject,我有这个结构 ├── a │   ├── a.html │   └── a1.js │   └── a2.js ├── b │   ├── b.html │   └── b1.js │   └── b2.js 我想将a1.js和a2.js文件注入a.html。b的情况也一样。我怎么能大口喝呢?我找不到要写什么 可以通过gulp inject完全做到这一点,但以下方法非常简单: const gulp = requir

我有这个结构

    ├── a
    │   ├── a.html
    │   └── a1.js        
    │   └── a2.js
    ├── b
    │   ├── b.html
    │   └── b1.js
    │   └── b2.js
我想将a1.js和a2.js文件注入a.html。b的情况也一样。我怎么能大口喝呢?我找不到要写什么


可以通过
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'));

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

  });
});