注入bower依赖与gulp - 相对路径问题

时间:2015-02-22 15:58:39

标签: gulp gulp-inject

我想用gulp-injectmain-bower-files注入bower依赖项。拾取依赖关系,但生成的路径是绝对的。

我得到/www/lib/angular/angular.js而我想要lib/angular/angular.js

<!-- bower:js -->
<script src="/www/lib/angular/angular.js"></script>
<!-- endinject -->

.bowerrc

{
  "directory": "www/lib"
}

gulpfile.js

var gulp = require('gulp');
var inject = require('gulp-inject');
var mainBowerFiles = require('main-bower-files');
var debug = require('gulp-debug');

gulp.task('index', function () {
    return gulp.src('./www/index.html')
        .pipe(inject(gulp.src(mainBowerFiles(), {read: false}, {relative: true}), {name: 'bower'}))
        .pipe(gulp.dest('./www'));
});

1 个答案:

答案 0 :(得分:7)

错误使用选项,relative选项应位于inject而不是gulp.src

正确的任务:

gulp.task('index', function () {
    return gulp.src('./www/index.html')
        .pipe(inject(gulp.src(mainBowerFiles(), {read: false}), {name: 'bower', relative: true}))
        .pipe(gulp.dest('./www'));
});