gulp-inject 无法在 index.html 中注入 js 文件

gulp-inject is not able to inject js file in index.html

我的目录结构如下:

|- app
   |- bower_components
   |- css
   |- img
   |- js
   |- scss
   |- template
|- node_modules
|- gulpfile.js
|- package.json
|- .bowerrc
|- bower.json

现在我的gulpfile.js如下图:

gulp.task('index', function() {
    var target = gulp.src('./app/index.html'); 
    var sources = gulp.src(['app/js/**/*.js'], { read: false });

    return target.pipe(inject(sources))
        .pipe(gulp.dest('app/js'));
});


gulp.task('default', function(callback) {
    runSequence(['sass', 'browserSync', 'index'], 'watch',
        callback
    );
});

我的index.html文件如下图:

<!DOCTYPE html>
<html>

<head>
    <title>kisanApp</title>
    <!-- inject:css -->
    <!-- endinject -->
</head>

<body ng-app="kisanApp">
    <ng-view></ng-view>
    <!-- inject:js -->
    <!-- endinject -->
    <script src="bower_components/angular/angular.js"></script>
    <script src="bower_components/angular-route/angular-route.js"></script>
</body>

</html>

但是我的 js 文件没有通过 gulp 添加到 index.html 中。那么路径有什么问题吗?

尝试将 var inject = require('gulp-inject');var gulp = require('gulp'); 添加到 gulpfile 的顶部。

然后再看看你的来源和gulp.dest:

var sources = gulp.src(['./app/js/**/*.js'], { read: false });

您需要在申请前进行 ./

编辑:

使用relative true处理目录树中更高层文件夹中的注入文件

var sources = gulp.src(['./app/js/**/*.js'], { read: false }, {relative: true});