Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative path to fonts not working with sass-loader

I have a completely clean project utilizing vue-templates/pwa. Everything is working as intended. The SCSS files are loaded, but the paths for the fonts are failing.

The error message:

assets/fonts/DIN/din_alternate_bold_1-webfont.ttf in ./~/css-loader?{"minimize":false,"sourceMap":false}!./~/vue-loader/lib/style-compiler?{"vue":true,"id":"data-v-0312694b","scoped":true,"hasInlineConfig":false}!./~/sass-loader/lib/loader.js?{"includePaths":["./src/assets/scss"],"data":"@import /"base.scss/";","sourceMap":false}!./~/vue-loader/lib/selector.js?type=styles&index=0!./src/components/TestLib.vue, ./~/css-loader?{"minimize":false,"sourceMap":false}!./~/vue-loader/lib/style-compiler?{"vue":true,"id":"data-v-242c44c2","scoped":false,"hasInlineConfig":false}!./~/sass-loader/lib/loader.js?{"includePaths":["./src/assets/scss"],"data":"@import /"base.scss/";","sourceMap":false}!./~/vue-loader/lib/selector.js?type=styles&index=0!./src/components/TestLib/Tile.vue and 3 others

Some code and hierarchy:

/src
    /assets
        /fonts
            /DIN
                din_alternate_bold_1-webfont.ttf
        /scss
            /base
                _typography.scss
            base.scss      

base.scss

@import 'base/typography'

_typography.scss

$font_path: '~assets/fonts/';
@font-face {
    font-family: "DinAltBold";
    src: url($font_path + "DIN/din_alternate_bold_1-webfont.ttf");
}

build/utils.js

exports.cssLoaders = function (options) {
  options = options || {}

  var cssLoader = {
    loader: 'css-loader',
    options: {
      minimize: process.env.NODE_ENV === 'production',
      sourceMap: options.sourceMap
    }
  }

  // generate loader string to be used with extract text plugin
  function generateLoaders (loader, loaderOptions) {
    var loaders = [cssLoader]
    if (loader) {
      loaders.push({
        loader: loader + '-loader',
        options: Object.assign({}, loaderOptions, {
          sourceMap: options.sourceMap
        })
      })
    }

    // Extract CSS when that option is specified
    // (which is the case during production build)
    if (options.extract) {
      return ExtractTextPlugin.extract({
        use: loaders,
        fallback: 'vue-style-loader'
      })
    } else {
      return ['vue-style-loader'].concat(loaders)
    }
  }

  // https://vue-loader.vuejs.org/en/configurations/extract-css.html
  return {
    css: generateLoaders(),
    postcss: generateLoaders(),
    less: generateLoaders('less'),
    sass: generateLoaders('sass', { indentedSyntax: true }),
    scss: generateLoaders('sass', {
          includePaths: ['./src/assets/scss'],
          data: '@import "base.scss";'
        }),
    stylus: generateLoaders('stylus'),
    styl: generateLoaders('stylus')
  }
}
like image 883
JavaCake Avatar asked Sep 26 '17 21:09

JavaCake


1 Answers

Don't use tilde ~ for the relative path. This points to the node_modules folder. So you have to options:

  1. Use $font_path: '../../fonts/'; as mentioned from @highFlyingSmurfs or
  2. Use $font_path: '~/src/assets/fonts/'; where ~/ points to the project root.

I got this information from the sass-loader GitHub Page which the Vue CLI uses.

like image 89
Sebastian Richter Avatar answered Oct 23 '22 06:10

Sebastian Richter