Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@import doesn't work for fonts #101

Open
JTinkers opened this issue Feb 22, 2020 · 7 comments
Open

@import doesn't work for fonts #101

JTinkers opened this issue Feb 22, 2020 · 7 comments

Comments

@JTinkers
Copy link

JTinkers commented Feb 22, 2020

I have a fresh installation of nuxt with nothing but this package installed.

The config is as such:

css:
[
    '@/assets/scss/defaults.scss'
],

styleResources:
{
    sass: [],
    scss: ['@/assets/scss/imports/*.scss'],
    less: [],
    stylus: []
},

Now, the file that package is meant to import is:
assets/scss/imports/fonts.scss

Which only contains this:
@import url("http://fonts.googleapis.com/css?family=Open+Sans:400,700|Quicksand:400,700");

.. and the error is:

Module build failed (from ./node_modules/sass-loader/dist/cjs.js):                            friendly-errors 16:54:59
SassError: @import directive requires a url or quoted path
        on line 1 of D:\Laragon\www\Project\app\layouts\default.vue
>> @import ../assets/scss/imports/url("http:/fonts.googleapis.com/css?family=Op

Note: I've tried all variations of it, with and without url, with and without quotation marks, ending url with .css and so on.

@selfagency
Copy link

selfagency commented Mar 2, 2020

Having the same problem. Any files referenced inside the styleResources block that contains links to resources in a relative path won't load.

Here's my nuxt.config.js:

  styleResources: {
    scss: ['~/assets/css/variables.scss', '~/assets/css/mixins.scss', '~/assets/css/fonts.scss', '~/assets/css/fontawesome.scss', '~/assets/css/global.scss']
  }

I am in the assets/css folder. I'm trying to access fonts in the assets/fonts folder. Here's a sample import:

@font-face {
  font-family: 'FontAwesome';
  src: url('../fonts/fontawesome/fontawesome-webfont.eot?v=4.7.0');
  src: url('../fonts/fontawesome/fontawesome-webfont.eot?#iefix&v=4.7.0') format('embedded-opentype'),
    url('../fonts/fontawesome/fontawesome-webfont.woff2?v=4.7.0') format('woff2'),
    url('../fonts/fontawesome/fontawesome-webfont.woff?v=4.7.0') format('woff'),
    url('../fonts/fontawesome/fontawesome-webfont.ttf?v=4.7.0') format('truetype'),
    url('../fonts/fontawesome/fontawesome-webfont.svg?v=4.7.0#fontawesomeregular') format('svg');
  font-weight: normal;
  font-style: normal;
}

Using .. for a relative path produces this:

These relative modules were not found:
 
* ../fonts/fontawesome/fontawesome-webfont.eot in ./node_modules/css-loader/dist/cjs.js??ref--7-oneOf-1-1!./node_modules/vue-loader/lib/loaders/stylePostLoader.js!./node_modules/postcss-loader/src??ref--7-oneOf-1-2!./node_modules/sass-loader/dist/cjs.js??ref--7-oneOf-1-3!./node_modules/sass-resources-loader/lib/loader.js??ref--7-oneOf-1-4!./node_modules/vue-loader/lib??vue-loader-options!./components/global/Buttons.global.vue?vue&type=style&index=0&id=cc98eade&lang=scss&scoped=true&, ./node_modules/css-loader/dist/cjs.js??ref--7-oneOf-1-1!./node_modules/vue-loader/lib/loaders/stylePostLoader.js!./node_modules/postcss-loader/src??ref--7-oneOf-1-2!./node_modules/sass-loader/dist/cjs.js??ref--7-oneOf-1-3!./node_modules/sass-resources-loader/lib/loader.js??ref--7-oneOf-1-4!./node_modules/vue-loader/lib??vue-loader-options!./components/home/AboveFold.vue?vue&type=style&index=0&id=a7b13a2e&lang=scss&scoped=true& and 4 others
* ../fonts/fontawesome/fontawesome-webfont.eot?v=4.7.0 in ./node_modules/css-loader/dist/cjs.js??ref--7-oneOf-1-1!./node_modules/vue-loader/lib/loaders/stylePostLoader.js!./node_modules/postcss-loader/src??ref--7-oneOf-1-2!./node_modules/sass-loader/dist/cjs.js??ref--7-oneOf-1-3!./node_modules/sass-resources-loader/lib/loader.js??ref--7-oneOf-1-4!./node_modules/vue-loader/lib??vue-loader-options!./components/global/Logo.global.vue?vue&type=style&index=0&id=3af98035&lang=scss&scoped=true&, ./node_modules/css-loader/dist/cjs.js??ref--7-oneOf-1-1!./node_modules/vue-loader/lib/loaders/stylePostLoader.js!./node_modules/postcss-loader/src??ref--7-oneOf-1-2!./node_modules/sass-loader/dist/cjs.js??ref--7-oneOf-1-3!./node_modules/sass-resources-loader/lib/loader.js??ref--7-oneOf-1-4!./node_modules/vue-loader/lib??vue-loader-options!./components/home/AboveFold.vue?vue&type=style&index=0&id=a7b13a2e&lang=scss&scoped=true& and 4 others
* ../fonts/fontawesome/fontawesome-webfont.svg?v=4.7.0 in ./node_modules/css-loader/dist/cjs.js??ref--7-oneOf-1-1!./node_modules/vue-loader/lib/loaders/stylePostLoader.js!./node_modules/postcss-loader/src??ref--7-oneOf-1-2!./node_modules/sass-loader/dist/cjs.js??ref--7-oneOf-1-3!./node_modules/sass-resources-loader/lib/loader.js??ref--7-oneOf-1-4!./node_modules/vue-loader/lib??vue-loader-options!./components/global/Buttons.global.vue?vue&type=style&index=0&id=cc98eade&lang=scss&scoped=true&, ./node_modules/css-loader/dist/cjs.js??ref--7-oneOf-1-1!./node_modules/vue-loader/lib/loaders/stylePostLoader.js!./node_modules/postcss-loader/src??ref--7-oneOf-1-2!./node_modules/sass-loader/dist/cjs.js??ref--7-oneOf-1-3!./node_modules/sass-resources-loader/lib/loader.js??ref--7-oneOf-1-4!./node_modules/vue-loader/lib??vue-loader-options!./components/home/AboveFold.vue?vue&type=style&index=0&id=a7b13a2e&lang=scss&scoped=true& and 4 others
* ../fonts/fontawesome/fontawesome-webfont.ttf?v=4.7.0 in ./node_modules/css-loader/dist/cjs.js??ref--7-oneOf-1-1!./node_modules/vue-loader/lib/loaders/stylePostLoader.js!./node_modules/postcss-loader/src??ref--7-oneOf-1-2!./node_modules/sass-loader/dist/cjs.js??ref--7-oneOf-1-3!./node_modules/sass-resources-loader/lib/loader.js??ref--7-oneOf-1-4!./node_modules/vue-loader/lib??vue-loader-options!./components/global/Buttons.global.vue?vue&type=style&index=0&id=cc98eade&lang=scss&scoped=true&, ./node_modules/css-loader/dist/cjs.js??ref--7-oneOf-1-1!./node_modules/vue-loader/lib/loaders/stylePostLoader.js!./node_modules/postcss-loader/src??ref--7-oneOf-1-2!./node_modules/sass-loader/dist/cjs.js??ref--7-oneOf-1-3!./node_modules/sass-resources-loader/lib/loader.js??ref--7-oneOf-1-4!./node_modules/vue-loader/lib??vue-loader-options!./components/home/AboveFold.vue?vue&type=style&index=0&id=a7b13a2e&lang=scss&scoped=true& and 4 others
* ../fonts/fontawesome/fontawesome-webfont.woff2?v=4.7.0 in ./node_modules/css-loader/dist/cjs.js??ref--7-oneOf-1-1!./node_modules/vue-loader/lib/loaders/stylePostLoader.js!./node_modules/postcss-loader/src??ref--7-oneOf-1-2!./node_modules/sass-loader/dist/cjs.js??ref--7-oneOf-1-3!./node_modules/sass-resources-loader/lib/loader.js??ref--7-oneOf-1-4!./node_modules/vue-loader/lib??vue-loader-options!./components/global/Buttons.global.vue?vue&type=style&index=0&id=cc98eade&lang=scss&scoped=true&, ./node_modules/css-loader/dist/cjs.js??ref--7-oneOf-1-1!./node_modules/vue-loader/lib/loaders/stylePostLoader.js!./node_modules/postcss-loader/src??ref--7-oneOf-1-2!./node_modules/sass-loader/dist/cjs.js??ref--7-oneOf-1-3!./node_modules/sass-resources-loader/lib/loader.js??ref--7-oneOf-1-4!./node_modules/vue-loader/lib??vue-loader-options!./components/home/AboveFold.vue?vue&type=style&index=0&id=a7b13a2e&lang=scss&scoped=true& and 4 others
* ../fonts/fontawesome/fontawesome-webfont.woff?v=4.7.0 in ./node_modules/css-loader/dist/cjs.js??ref--7-oneOf-1-1!./node_modules/vue-loader/lib/loaders/stylePostLoader.js!./node_modules/postcss-loader/src??ref--7-oneOf-1-2!./node_modules/sass-loader/dist/cjs.js??ref--7-oneOf-1-3!./node_modules/sass-resources-loader/lib/loader.js??ref--7-oneOf-1-4!./node_modules/vue-loader/lib??vue-loader-options!./components/global/Buttons.global.vue?vue&type=style&index=0&id=cc98eade&lang=scss&scoped=true&, ./node_modules/css-loader/dist/cjs.js??ref--7-oneOf-1-1!./node_modules/vue-loader/lib/loaders/stylePostLoader.js!./node_modules/postcss-loader/src??ref--7-oneOf-1-2!./node_modules/sass-loader/dist/cjs.js??ref--7-oneOf-1-3!./node_modules/sass-resources-loader/lib/loader.js??ref--7-oneOf-1-4!./node_modules/vue-loader/lib??vue-loader-options!./components/home/AboveFold.vue?vue&type=style&index=0&id=a7b13a2e&lang=scss&scoped=true& and 4 others

Using either ~/assets or @/assets produces this:

These relative modules were not found:
* ../../../../../../../../assets/fonts/fontawesome/fontawesome-webfont.eot in ./node_modules/css-loader/dist/cjs.js??ref--7-oneOf-1-1!./node_modules/postcss-loader/src??ref--7-oneOf-1-2!./node_modules/sass-loader/dist/cjs.js??ref--7-oneOf-1-3!./node_modules/sass-resources-loader/lib/loader.js??ref--7-oneOf-1-4!./node_modules/vue2-animate/src/sass/vue2-animate.scss
* ../../../../../../../../assets/fonts/fontawesome/fontawesome-webfont.eot?v=4.7.0 in ./node_modules/css-loader/dist/cjs.js??ref--7-oneOf-1-1!./node_modules/postcss-loader/src??ref--7-oneOf-1-2!./node_modules/sass-loader/dist/cjs.js??ref--7-oneOf-1-3!./node_modules/sass-resources-loader/lib/loader.js??ref--7-oneOf-1-4!./node_modules/vue2-animate/src/sass/vue2-animate.scss
* ../../../../../../../../assets/fonts/fontawesome/fontawesome-webfont.svg?v=4.7.0 in ./node_modules/css-loader/dist/cjs.js??ref--7-oneOf-1-1!./node_modules/postcss-loader/src??ref--7-oneOf-1-2!./node_modules/sass-loader/dist/cjs.js??ref--7-oneOf-1-3!./node_modules/sass-resources-loader/lib/loader.js??ref--7-oneOf-1-4!./node_modules/vue2-animate/src/sass/vue2-animate.scss
* ../../../../../../../../assets/fonts/fontawesome/fontawesome-webfont.ttf?v=4.7.0 in ./node_modules/css-loader/dist/cjs.js??ref--7-oneOf-1-1!./node_modules/postcss-loader/src??ref--7-oneOf-1-2!./node_modules/sass-loader/dist/cjs.js??ref--7-oneOf-1-3!./node_modules/sass-resources-loader/lib/loader.js??ref--7-oneOf-1-4!./node_modules/vue2-animate/src/sass/vue2-animate.scss
* ../../../../../../../../assets/fonts/fontawesome/fontawesome-webfont.woff2?v=4.7.0 in ./node_modules/css-loader/dist/cjs.js??ref--7-oneOf-1-1!./node_modules/postcss-loader/src??ref--7-oneOf-1-2!./node_modules/sass-loader/dist/cjs.js??ref--7-oneOf-1-3!./node_modules/sass-resources-loader/lib/loader.js??ref--7-oneOf-1-4!./node_modules/vue2-animate/src/sass/vue2-animate.scss

If, however, I do this instead, it works – but it isn't ideal:

  css: ['~/assets/css/fonts.scss', '~/assets/css/fontawesome.scss'],
  styleResources: {
    scss: ['~/assets/css/variables.scss', '~/assets/css/mixins.scss', '~/assets/css/global.scss']
  }

@JTinkers
Copy link
Author

JTinkers commented Mar 2, 2020

@selfagency One solution (although it increases project complexity, as you have to keep it outside of your .scss file that uses it) is to put all @imports inside .scss imported via nuxt's scss as oposed to the loader.

@selfagency
Copy link

Yes, that's what I wound up doing above, at the end.

@szymon-krysztofiak-dev
Copy link

Use ~assets, for example:

@font-face {
  font-family: 'FontAwesome';
  src: url('~assets/fonts/fontawesome/fontawesome-webfont.eot?v=4.7.0');
  src: url('~assets/fonts/fontawesome/fontawesome-webfont.eot?#iefix&v=4.7.0') format('embedded-opentype'),
    url('~assets/fonts/fontawesome/fontawesome-webfont.woff2?v=4.7.0') format('woff2'),
    url('~assets/fonts/fontawesome/fontawesome-webfont.woff?v=4.7.0') format('woff'),
    url('~assets/fonts/fontawesome/fontawesome-webfont.ttf?v=4.7.0') format('truetype'),
    url('~assets/fonts/fontawesome/fontawesome-webfont.svg?v=4.7.0#fontawesomeregular') format('svg');
  font-weight: normal;
  font-style: normal;
}

That works well for me.

@tufinhon
Copy link

tufinhon commented Feb 4, 2021

Use ~assets, for example:

@font-face {
  font-family: 'FontAwesome';
  src: url('~assets/fonts/fontawesome/fontawesome-webfont.eot?v=4.7.0');
  src: url('~assets/fonts/fontawesome/fontawesome-webfont.eot?#iefix&v=4.7.0') format('embedded-opentype'),
    url('~assets/fonts/fontawesome/fontawesome-webfont.woff2?v=4.7.0') format('woff2'),
    url('~assets/fonts/fontawesome/fontawesome-webfont.woff?v=4.7.0') format('woff'),
    url('~assets/fonts/fontawesome/fontawesome-webfont.ttf?v=4.7.0') format('truetype'),
    url('~assets/fonts/fontawesome/fontawesome-webfont.svg?v=4.7.0#fontawesomeregular') format('svg');
  font-weight: normal;
  font-style: normal;
}

That works well for me.

This is the right answer!, thank you so much. So when referencing fonts to include in a @font-face through nuxt sass loader it should be an absolute path, but ~assets (this should be written in nuxt/assets documentation)

@tufinhon
Copy link

tufinhon commented Mar 4, 2021 via email

@IgorSousaFront
Copy link

IgorSousaFront commented Mar 10, 2021

if you want to use google fonts, try to use the link import.

<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Open+Sans&display=swap" rel="stylesheet">

on Nuxt.js edit the nuxt.config.js

link: [
    { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' },
    { rel: 'preconnect', href: "https://fonts.gstatic.com" },
    { rel: 'stylesheet', href: "https://fonts.googleapis.com/css2?family=Open+Sans:wght@300;400;600&display=swap" }
],

this worked for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants