How to use font-awesome 4 with barebones Rails 6 and Webpacker, without any additional gems, copy-paste fonts or CSS files into your application and do other strange things:
Add the awesome npm package - package.json:
{ "dependencies": { "font-awesome": "4.7.0" } }
Include the css font file in the css manifest - app / stylesheets / application.css:
/* *= require font-awesome/css/font-awesome.min *= require_tree . *= require_self */
Override the font definition in our 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 in the resource 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 will get the included fonts and compiled into the public / fonts directory, and you will get one css file with all the compiled materials (your application and font-awesome).
The fact is that font-awesome minified css contains hard-coded font paths, and to override this, we simply add the font-face directive with the generated font paths. Because of this, font-awesome.min.css must be added first in the manifest file.
Vladimir Rozhkov Jul 09 '19 at 19:33 2019-07-09 19:33
source share