How to use font-awesome 4 with barebones Rails 6 and Webpacker, without any additional gems, copy-pasting font or css files into your app and doing other weird things:
Add font-awesome npm package —
package.json:
{
"dependencies": {
"font-awesome": "4.7.0"
}
}
Include font-awesome css file into css manifest —
app/stylesheets/application.css:
/*
*= require font-awesome/css/font-awesome.min
*= require_tree .
*= require_self
*/
Override font-face definition in our custom css file —
app/stylesheets/font-awesome.css.erb:
@font-face {
font-family: 'FontAwesome';
src: url('<%= font_path('fontawesome-webfont.eot') %>');
src: url('<%= font_path('fontawesome-webfont.eot') %>?#iefix') format('embedded-opentype'), url('<%= font_path('fontawesome-webfont.woff2') %>') format('woff2'), url('<%= font_path('fontawesome-webfont.woff') %>') format('woff'), url('<%= font_path('fontawesome-webfont.ttf') %>') format('truetype'), url('<%= font_path('fontawesome-webfont.svg') %>#fontawesomeregular') format('svg');
font-weight: normal;
font-style: normal;
}
Include node_modules font-awesome dir + font file types into assets pipeline—
config/initializers/assets.rb
Rails.application.config.assets.paths << Rails.root.join('node_modules/font-awesome/fonts')
Rails.application.config.assets.precompile << /\.(?:svg|eot|woff|ttf)\z/
You'll get fonts included and compiled into public/fonts directory and you'll get single css file with all stuff (your app and font-awesome) compiled into it.
The thing is, font-awesome minified css contains hardcoded paths to fonts and to override this we just adding font-face directive with generated fonts paths. Because of this, the font-awesome.min.css should be added first in the manifest file.
However, while everything will work fine, you'll get 404 errors in console. I wasn't managed to fix this and eventually gave up and switched to font-awesome-sass
gem.