【问题标题】:Gulp and injected pathGulp 和注入路径
【发布时间】:2016-06-17 00:35:22
【问题描述】:

在 Visual Studio 中,我有以下客户端项目结构

-wwwroot
   -app
      -js
      -views
   -css
   -images
   -lib
   index.html

使用 gulp-inject 我想在 index.html 中注入我的 javascript 的路径:

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

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

gulpfile.js 位于 wwwroot 目录之外。 这里的问题是,在我的 index.html 中,注入的形式是:

<script src="/wwwroot/app/js/RemoteCallServices.js"></script>

为了工作,我需要拥有

<script src="app/js/RemoteCallServices.js"></script>

我怎样才能做到这一点?

【问题讨论】:

    标签: javascript node.js npm gulp gulp-inject


    【解决方案1】:

    您可以使用 gulp-inject ignorePath 选项

    gulp.task('injecttest', function () {
        var target = gulp.src('wwwroot/index.html');
        var sources = gulp.src(['wwwroot/app/js/**/*.js'], { read: false });
    
        return target.pipe(inject(sources, { ignorePath: '/wwwrooot/')).pipe(gulp.dest('wwwroot/'));;
    });
    

    【讨论】:

    • 这对我有用,但如果src="app/js/etc..." 我得到src="/app/js/etc..."。知道如何删除额外的领先 /
    • 从 ignorePath 属性中删除额外的前导 /。使用上面的例子,{ ignorePath: 'wwwrooot/' }
    • 更新:在this thread 中我看到我可以使用addRootSlash: false 以及ignorePath: '/wwwrooot/'。为我修好了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多