9

I'm using the vue-cli webpack template and I'm trying to load in local fonts in my project. I'm having trouble getting the path to my fonts correct. How should my path look like?

I found some information about what I might be doing wrong but I couldn't figure it out: https://github.com/webpack-contrib/sass-loader#problems-with-url

File structure:

enter image description here

In my _fonts.scss:

    // Webfont: LatoLatin-Regular
@font-face {
  font-family: 'LatoLatinWeb';
  src: url('../assets/fonts/LatoLatin-Regular.eot'); /* IE9 Compat Modes */
  src: url('../assets/fonts/LatoLatin-Regular.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
        url('../assets/fonts/LatoLatin-Regular.woff2') format('woff2'), /* Modern Browsers */
        url('../assets/fonts/LatoLatin-Regular.woff') format('woff'), /* Modern Browsers */
        url('../assets/fonts/LatoLatin-Regular.ttf') format('truetype');
  font-style: normal;
  font-weight: normal;
  text-rendering: optimizeLegibility;
}

Webpack.base.config.sj:

  {
    test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
    loader: 'url-loader',
    options: {
      limit: 10000,
      name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
    }
  }

Utils.js:

  return {
    css: generateLoaders(),
    postcss: generateLoaders(),
    less: generateLoaders('less'),
    sass: generateLoaders('sass', { indentedSyntax: true }),
    scss: generateLoaders('sass').concat(
      {
        loader: 'sass-resources-loader',
        options: {
          resources: path.resolve(__dirname, '../src/styles/_variables.scss')
        }
      }
    ).concat(
      {
        loader: 'sass-resources-loader',
        options: {
          resources: path.resolve(__dirname, '../src/styles/mixins/_mixins.scss')
        }
      }      
    ).concat(
      {
        loader: 'sass-resources-loader',
        options: {
          resources: path.resolve(__dirname, '../src/styles/_fonts.scss')
        }
      }      
    ),
    stylus: generateLoaders('stylus'),
    styl: generateLoaders('stylus')
  }

How do I load my local fonts in with vue-cli webpack template?

2 Answers 2

13

I import my fonts this way:

$font_path: '~@/assets/fonts/';

// *********** //
// SOURCE SANS //
// ITALIC
@font-face{
  font-family    : 'Source Sans Pro';
  src            : url($font_path +'SourceSansPro-Italic.eot'); /* IE9 Compat; Modes */
  src            : url($font_path +'SourceSansPro-Italic.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */ url($font_path +'SourceSansPro-Italic.woff2') format('woff2'), /* Modern Browsers */ url($font_path +'SourceSansPro-Italic.woff') format('woff'); /* Modern Browsers */
  font-style     : italic, oblique;
  font-weight    : 400;
  text-rendering : optimizeLegibility;
}
// REGULAR
@font-face{
  font-family    : 'Source Sans Pro';
  src            : url($font_path +'SourceSansPro-Regular.eot'); /* IE9 Compat; Modes */
  src            : url($font_path +'SourceSansPro-Regular.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */ url($font_path +'SourceSansPro-Regular.woff2') format('woff2'), /* Modern Browsers */ url($font_path +'SourceSansPro-Regular.woff') format('woff'); /* Modern Browsers */
  font-style     : normal;
  font-weight    : 400;
  text-rendering : optimizeLegibility;
}
// SEMI BOLD
@font-face{
  font-family    : 'Source Sans Pro';
  src            : url($font_path +'SourceSansPro-Semibold.eot'); /* IE9 Compat; Modes */
  src            : url($font_path +'SourceSansPro-Semibold.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */ url($font_path +'SourceSansPro-Semibold.woff2') format('woff2'), /* Modern Browsers */ url($font_path +'SourceSansPro-Semibold.woff') format('woff'); /* Modern Browsers */
  font-style     : normal;
  font-weight    : 600;
  text-rendering : optimizeLegibility;
}
// Use "font-family: $source_sans_pro" in your code.
$source_sans_pro : 'Source Sans Pro', sans-serif;
// [END] SOURCE SANS //
// ***************** //

All my fonts are located in src/assets/fonts/

My webpack.base.conf file contains this loader:

 {
    test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
    loader: 'url-loader',
    options: {
      limit: 10000,
      name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
    }
  },
Sign up to request clarification or add additional context in comments.

2 Comments

Worked for me after getting stuck and forgetting that relative paths don't work. Thank you!
Great. not having svg fonts will help imcluding svg images :)
1

In the vuejs project I am working on,

This one did not worked:

@font-face {
   font-family: 'name-of-choice';
   src: url("~@/assets/<location-to-your-font-file>/font-file-name.ttf") format('font-type');
}

This worked:

@font-face {
   font-family: 'name-of-choice';
   src: url(~@/assets/<location-to-your-font-file>/font-file-name.ttf) format('font-type');
}

And I've observed that if we use the solution without double quote for one single time and then add double quotes, it again works! If anyone know what's happening here, please do answer.

Solution: Try not enclosing URL String in quotes.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.