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

时间:2017-07-31 07:32:43

标签: angularjs gulp gulp-inject

我的目录结构如下:

|- 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中。那路径有什么不对吗?

1 个答案:

答案 0 :(得分:1)

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

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

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

您需要在应用之前继续./

编辑:

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

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

相关问题