Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I load font-awesome using SCSS (SASS) in Webpack using relative paths?

I have font-awesome in my node_modules folder so I try to import it in my main .scss file like so:

@import "../../node_modules/font-awesome/scss/font-awesome.scss"; 

But Webpack bundling compilation fails, telling me

Error: Cannot resolve 'file' or 'directory' ../fonts/fontawesome-webfont.eot  

because the font-awesome.scss file refers to a relative path, '../fonts/'.

How can I tell scss \ webpack to @import another file, and use that file's folder as the home folder so that its relative paths work as it expects?

like image 285
Richard Avatar asked Nov 11 '15 11:11

Richard


People also ask

How do I import font awesome to SCSS?

Adding Font Awesome to Your Compile Open your project's scss/variables. scss and edit the $fa-font-path variable to point to where you placed the webfonts folder. $fa-font-path: "../webfonts"; In your main SCSS compile file, import Font Awesome by adding the core styles file, @import "scss/fontawesome.

How do I use Google fonts in SCSS?

The first step is to choose your font. Explore the Google Fonts website and when you decide on a font, click the “Add to Collection” button. Once you have all of your desired fonts for your website, click the “Use” button on the bottom right of the page. Next, pick out your desired font weights and character sets.


2 Answers

Use

$fa-font-path: "~font-awesome/fonts"; @import "~font-awesome/scss/font-awesome"; 

where the $fa-font-path variable is seen in font-awesome/scss/_variables.scss

$fa-font-path: "../fonts" !default; 

The tilde "~" is interpolated by sass-loader using the webpack mecanism.

like image 118
user137794 Avatar answered Oct 02 '22 01:10

user137794


There doesn't appear to be any way to @import files that have their own relative paths in SCSS \ SASS.

So instead I managed to get this to work:

  • Import the scss \ css font-awesome file in my .js or .jsx files, not my stylesheet files:

    import 'font-awesome/scss/font-awesome.scss';    
  • Add this to my webpack.config file:

     module:     {         loaders:         [             {test: /\.js?$/, loader: 'babel-loader?cacheDirectory', exclude: /(node_modules|bower_components)/ },             {test: /\.jsx?$/, loader: 'babel-loader?cacheDirectory', exclude: /(node_modules|bower_components)/ },             {test: /\.scss?$/, loaders: ['style-loader', 'css-loader', 'sass-loader']},                      {test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: 'file-loader?mimetype=image/svg+xml'},             {test: /\.woff(\?v=\d+\.\d+\.\d+)?$/, loader: "file-loader?mimetype=application/font-woff"},             {test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/, loader: "file-loader?mimetype=application/font-woff"},             {test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: "file-loader?mimetype=application/octet-stream"},             {test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: "file-loader"},         ]     } 
like image 30
Richard Avatar answered Oct 02 '22 03:10

Richard