Ceyhun's Dev Blog

How to Load Local Fonts with Vue CLI 4 in Vue 3

Using local fonts in our Vue projects can be a little tricky. Especially with the webpack compatibility issues with sass-loader.

In this post we’re going to learn how to use our local fonts in our Vue 3 application using Vue CLI 4.

Before we move on, make sure to install Node.js on your system. Vue CLI 4 requires version 8.9 or above.

Installing Vue CLI 4

Run the command below to install Vue CLI 4 globally.

npm install -g @vue/cli

Creating our Vue App

When Vue CLI 4 installation is finished, create a project called my-app. ( or whatever you prefer. )

vue create my-app

selecting-setup-step-1

selecting-setup-step-2

selecting-setup-step-3

selecting-setup-step-4

After your setup is complete you can go to your project folder, and type npm run serve to preview the Vue app.

http://localhost:8080/

selecting-setup-step-5

Folder Structure of the Project

After our application is alive and running, we can now create subfolders ( under the assets folder ) for font and styling files.

folder-structure

Importing Local Fonts Into scss File

To load our fonts we are using @font-face at-rule .

fonts.scss

@font-face {
  font-family: 'Karla-Regular';
  src: local('Karla-Regular'), url('~@/assets/fonts/Karla-Regular.ttf') format("truetype");
}

@font-face {
  font-family: 'Karla-Bold';
  src: local('Karla-Bold'), url('~@/assets/fonts/Karla-Bold.ttf') format("truetype");
}

To give a brief explanation:

Installing sass and sass-loader

As default, Vue CLI 4 uses webpack 4 as a webpack version, which has compatibility issues with the latest sass-loader version. To work around that, we are going to install specific version of the sass-loader. You can also check the docs for this issue.

npm install -D sass-loader@^10 sass

Creating vue.config.js File

With the help of vue.config.js file we can use our variables, mixins, fonts etc. all across our components. That way, we don’t have to manually import necessary features into every single component.

Now, let’s create one in the root folder of our application.

vue-js-config-file

Configuring vue.config.js File

vue.config.js

module.exports = {
  css: {
    loaderOptions: {
      scss: {
        additionalData: `@import "~@/assets/scss/fonts.scss";`
      }
    }
  }
}

Here, we set the internal configuration for the webpack loader, and loaded our fonts.scss file globally. That way, we can access it from each component in the application.

Using Local Fonts in Vue Component

In App.vue component our current styling is like this.

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}

</style>

By adding lang="scss" attribute to style tag, we can now use our fonts as below.

We can also create separate scss file to use our fonts. Both is going to work.

<style lang="scss">
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}

h1 {
  font-family: 'Karla-Bold';
  font-size: 40px;
  color: #9403fc;
}

h3 {
  font-family: 'Karla-Regular';
  font-size: 30px;
  color: #07c9e3;
}
</style>

Here’s the End Result

After applying our styles, we now can see that our fonts are applied to h1 and h3 elements. ( Make sure to restart your local server to see the changes. )

vue-cli-end-result

Conclusion

By configuring css.loaderOptions option in vue.config.js file we can use our shared styles throughout our application. It saves us from repeated work.

#vuejs #vue-3 #vue-cli-4 #google-fonts #local-fonts