【问题标题】:After gulp task browser can't find fonts吞咽任务浏览器后找不到字体
【发布时间】:2016-07-28 13:33:40
【问题描述】:

在我运行任务从 bower_component 复制字体后,浏览器似乎与正确的路径混淆了。

这是字体的 gulp 任务:

// Fonts
gulp.task('fonts', function() {
  return gulp.src(bowerDir + '/open-sans-fontface/fonts/**/*.{eot,svg,ttf,woff,woff2}', function (err) {})
      .pipe(gulp.dest(dest + 'fonts'));
});

这是样式的 gulp 任务:

// Styles
gulp.task('styles', function() {
  return plugins.sass(src + 'styles/main.scss', {
          style: 'expanded',
          loadPath: [
            // './resources/sass',
            bowerDir + '/bootstrap-sass/assets/stylesheets',
            bowerDir + '/font-awesome/scss',
            bowerDir + '/open-sans-fontface'
          ]
        })
      .pipe(plugins.autoprefixer('last 2 version'))
      .pipe(gulp.dest(dest + 'styles'))
      .pipe(plugins.rename({ suffix: '.min' }))
      .pipe(plugins.cssnano())
      .pipe(gulp.dest(dest + 'styles'))
      .pipe(reload({stream: true}));
      // .pipe(plugins.notify({ message: 'Styles task complete' }));
});

如何调整 gulp 文件,让创建的 css 文件寻找正确的路径?

【问题讨论】:

    标签: css fonts gulp task


    【解决方案1】:

    您可以使用gulp-replace。示例:

    gulp.task('styles', function() {
      return gulp.src('src/styles/main.scss')
        .pipe(plugins.sass())
        .pipe(plugins.replace('original-path/fonts/', 'new-path/fonts/'))
        .pipe(gulp.dest('dist'));
    });
    

    如果例如bootstrap 在original-path/fonts/ 下有它的字体,在运行styles 任务后,该路径现在将替换为new-path/fonts/

    【讨论】:

    • 感谢 MadScone,但遗憾的是没有任何改变。我已将代码添加到我的样式任务中: .pipe(plugins.replace(dest + 'style/fonts', dest + 'fonts/open-sans')) 但浏览器仍然在错误的路径中查找跨度>
    • 您对replace() 的第一个参数看起来不正确。我认为您的字体的原始路径不会是dest + 'style/fonts'。我不知道dest 是什么,但假设它类似于“构建”......这意味着replace() 正在用build/fonts/open-sans 替换build/style/fonts。但是 open-sans 不会指向build/style/fonts。我想你可能想要plugins.replace('./fonts', './fonts/open-sans') 之类的东西。第一个参数是您要更改的原始 URL。
    • 感谢您的帮助 :) 我现在已经解决了这个问题。我查看了 main.css 并使用了替换插件,直到我让它工作!
    猜你喜欢
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    • 2016-01-21
    • 2014-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多