在 Gulp 任务中更改 bowerFiles() 注入路径

Change bowerFiles() injection path in Gulp task

本文关键字:注入 路径 bowerFiles Gulp 任务      更新时间:2023-09-26

我有以下 Gulp 任务:

gulp.task('bowerinstall', function() {
    gulp.src('app/index.html')
        .pipe($.inject(gulp.src(bowerFiles(), {read: false}), {name: 'bower'}))
        .pipe(gulp.dest(['dist/', 'app/']));
});

此任务查看我的索引.html并将 bower 文件注入其中。这工作正常,但是它将以下路径注入我的索引.html:

<script src="/app/bower_components/modernizr/modernizr.js"></script>
任务

已经确定了要注入到我的应用程序中的正确资源,这很好,但我希望它注入以下内容:

<script src="bower_components/modernizr/modernizr.js"></script>

有没有办法修改我的 gulp 任务来执行此操作?

谢谢!

在以下行中:

    .pipe($.inject(gulp.src(bowerFiles(), {read: false}), {name: 'bower'}))

添加选项relative: true,例如:

    .pipe($.inject(gulp.src(bowerFiles(), {read: false}), {name: 'bower', relative: true}))

文档对此进行了介绍:https://www.npmjs.com/package/gulp-inject#injecting-files-relative-to-target-files