【问题标题】:gulp.js prepend and append string to src and href attributes after using gulp.useref使用 gulp.useref 后,gulp.js 将字符串添加到 src 和 href 属性
【发布时间】:2015-08-20 18:25:28
【问题描述】:

我正在模板引擎中运行一些 html 文件,所有资产都这样声明:

<link rel="stylesheet" href="$href('libs/bootstrap.min.css')"> <script src="$href('libs/jquery.js')"></script>

当我运行 $.useref 时,它确实会将文件移动到正确的文件夹中,但会将 .html 文件保留为通常应该具有的绝对路径和相对路径。有没有办法我可以编辑 $.useref “模板” 的脚本和链接标签指向正确的位置,并在 src 和 href 属性中预先/附加 $href('') ?有没有其他 gulp 插件可以做到这一点?

【问题讨论】:

    标签: javascript gulp gulp-useref


    【解决方案1】:

    终于用 gulp-replace 做到了:

    gulp.task('templates', function () {
        return gulp.src('dist/**/*.html')
        .pipe(replace(/src="([^"]*)"/g, 'src="$$href(\'$1\')"'))
        .pipe(replace(/href="([^"]*)"/g, 'href="$$href(\'$1\')"'))
        .pipe(gulp.dest('dist'));
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-08
      • 2013-05-09
      • 1970-01-01
      • 1970-01-01
      • 2013-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多