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

问题描述:

我的目录结构如下:

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

现在我的gulpfile.js如下所示:

Now my gulpfile.js is as shown below:

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文件如下所示:

My index.html file is as shown below:

<!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中.那么路径有什么问题吗?

But somehow my js files are not added in index.html via gulp. So is there anything wrong with the path?

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

Try adding var inject = require('gulp-inject'); and var gulp = require('gulp'); to the top of your gulpfile.

然后再查看一下您的来源和gulp.dest:

And then take another look at your sources and gulp.dest:

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

在应用前,您需要继续进行./.

You will need the proceed ./ before app.

使用relative true处理来自目录树中较高位置的文件夹中的注入文件

Use relative true to deal inject files from a folder higher up in the directory tree

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