ngBoilerplate and Font Awesome - gruntjs

I have a ngBoilerplate project, I would like to add Font Awesome to it.
I have installed Font Awesome with:
bower install font-awesome --save-dev
I then added the following to the build.config.js file:
css: [
'vendor/font-awesome/css/font-awesome.min.css'
],
assets: [
'vendor/font-awesome/fonts/*'
]
And I added the following to my src/less/variables.less file:
#fa-font-path: "../assets";
Now I would like to add a simple home icon to my site with:
<i class="fa fa-home"></i>
Instead now I get a ␦ (ascii, blank square) where the icon should be.
The Font Awesome CDN works fine but cant get it to work with bower.

So for some reason I get an intermittent error that the font files cant be found.
I amended the build.config.js and removed 'vendor/font-awesome/fonts/*'.
Then I configured Gruntfile.js to copy the fonts to the folder that error was looking for. I did this by adding the following to the copy section:
font_awesome_fonts: {
files:{
expand: true,
flatten: true,
src: 'vendor/font-awesome/fonts/*',
dest: '<%= build_dir %>/vendor/fonts'
}
}
And added the following to the delta.assets section:
'copy:font_awesome_fonts'
This seemed to work but now I get the following warning messages in console, no idea why, at least it works now:
localhost/:1 Failed to decode downloaded font: http://localhost:14080/fonts/fontawesome-webfont.woff2?v=4.3.0
localhost/:1 Failed to decode downloaded font: http://localhost:14080/fonts/fontawesome-webfont.woff?v=4.3.0
localhost/:1 Failed to decode downloaded font: http://localhost:14080/fonts/fontawesome-webfont.ttf?v=4.3.0

Thanks for your question; it got me on the path to this answer.
What worked for me was using the Font Awesome LESS file instead of the CSS. In src/less/main.less I added the following:
#import '../../vendor/font-awesome/less/font-awesome.less';
I made this declaration in my src/less/variables.less:
#fa-font-path: ".";
I also removed any mention of the font awesome from the vendor css section of build.config.js. My font declaration looks a bit different, but should be effectively the same:
vendor_files: [
js: ...,
css: [
],
assets: [
'vendor/font-awesome/fonts/*.otf',
'vendor/font-awesome/fonts/*.eot',
'vendor/font-awesome/fonts/*.svg',
'vendor/font-awesome/fonts/*.ttf',
'vendor/font-awesome/fonts/*.woff'
]
]
I haven't noticed your issue with the fonts not copying, nor have I seen the messages in the console, but the icons show up correctly when hosting the files in a webserver and browsing to my app.

Related

Prevent fonts to be embedded in webpack generated css

I have recently need to use fonts from local application which were earlier referenced from http://fonts.googleapis.com/css?family=Open+Sans, earlier fonts were referenced in sass like :
#import url("https://fonts.googleapis.com/css?family=Maven+Pro");
I installed fonts from npm & imported in sass like:
#import url("../node_modules/typeface-maven-pro?family=Maven+Pro");
Now webpack is generating 21MB css which earlier was 320kb. As I supspected upon inspection I found fonts are embedded in css as hexadecimal like :
#font-face {
font-family: 'Open Sans';
font-style: italic;
font-weight: 300;
src: url(data:application/vnd.ms-fontobject;base64,h3cBAKF2AQACAAIABAAAAAILAwYDBQQCAgQBASwBAAAIAExQ7wIA4FsgAEAoAAAAAAAAAJ8BACAAAAAAhvcg1AAAAAAAAAAAAAAAAAAAAAAAAB4ATwBwAGUAbgAgAFMAYQBuAHMAIABMAGkAZwBoAHQAAAAMAEkAdABhAGwAaQBjAAAAGABWAGUAcgBzAGkAbwBuACAAMQAuADEAMAAAACwATwBwAGUAbgAgAFMAYQBuAHMAIABMAGkAZwBoAHQAIABJAHQAYQBsAGkAYwAAAAAAQlNHUAAAAAAAAAAAAAAAAAAAAAADAuoIAVbNAW9TAWcEFM3pjMl4WmfKaWN3hnx6JmO6ojRvSI1U6OiPDyQA0a+M0J9YQcEiqJaAbQHtU1iDq2Bnl7ejJ8rC41GlgOAY9I5OWoJSjiHI9DfkJpbKlOMkLprn1q4rVvWzbCf930p8703JJ0nHo31vHRYa4ebmnW+8zvx0RSUd2QcQKhRRo8nvU8UADxYMUJeXBpGj9aUg2Y3ZEO8tyX0zEQ8MFqgcOnR8pISExrfRL3Kn98z3gkfclmOhXwU+6M5ofsi1WApsIrGHO/rqog6zb5GuKGJqPCcnakQVQwmY1GfEOjxb7S46r4JgLa3OY2lM3fgsJ9aTZ064fU8mjZeQbdUAkJ8PsdL8B/tWQlQHFypDF97cLNa/WrONAi9AOGESW+kaQLHIzIDHRCcAZUFo7BTXwsgaANXHRc3rVvIA81wZ+yMjSbDO85h+KgPt4VMqvTImD1Qz8DysK7dGEA/PSL6oopAt7H1sC29hY3uJcsr3ZmFTWc/DGrYsU4nAbr6C3A9k+iWgah08uphgGlfwhyLWAr5u7gptDVTVJY9HuHLyoy6LLB3a6eC6DKF1rYawTIQf3sw44lKgl9sU2/R9D9iKRjZqcamQkszEufXMUfXMUHEUHEUHEUHBiDjVH1zFH1zFH1zFBM1R9cxR9cxR91Yj6mVH1zHrzbncHebc7g70IqhGTg+BA+BBRVBRXYH2bwjzN4R5m8DvJ7dlQb3bY7Qa7FsjqCeZvCOs3gd4/b7s2saCa3P0IJXbk6ET/YG+fEeLdrPbqbTTZVng6zeB369OTrN4HfTtJHi86tE1B1m8DuJUxBFiOraEdW0IEWDvViIBHUKEcW+I4zeB3e7Rh3+nFpUbWlRteAgHija8BG1+Qd6P3Gm2bwgOoRteEdxwI2c0zeEaXhRpeFBnQN7/7whnQN++WwWyBzLSCtgPX6B3qA71GTEUb8G2KRoP83Pu7Gk7SkyX5RvlPQ8yX5UHTBvyFgybFRnxfNpu0pm593Y0r7TtPbvWzXLCgRFAIVA5VGCxRvrE+AR7YZbxLS26Xg0BA0Ab5jpwje92z3MahbyvMFLRvrRCuILgQLgTZYgBQ);
Please note I have to truncate font in encoded text as question character limit is 30000 characters in stack overflow & there are multiple font faces like this.
What is wrong here?
Issue was with my loader for fonts as used for fonts. I was using url-loader like:
{
test: /\.(jpg|png|gif|eot|woff2|woff|ttf|ico|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'url-loader'
}
Solution is to modify it limit file size and split the bigger files like :
{
test: /\.(woff2|woff|ttf|eot|svg|otf)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loaders: ["url-loader?limit=100&name=fonts/[name]_[hash].[ext]"]
}
Incase anyone deals with this while using Sensible webpack 5 boilerplate . I solved it by changing the type
from
{ test: /\.(woff(2)?|eot|ttf|otf|svg|)$/, type: 'asset/inline' }
to
{ test: /\.(woff(2)?|eot|ttf|otf|svg|)$/, type: 'asset/resource' },
nb: also puts svgs separate. You can modify the test

Custom font from node_modules into angular-cli

I have looked at several stackoverflow posts and github issues, but can't find something that is working for me.
I have a custom font that I'm using for icons that I'm installing via npm. These are the files in my folder a-icons in node_modules:
a-icon.css
a-icon.eot,
a-icon.ttf,
a-icon.woff,
a-icon.svg
In my main stylesheet I've added:
#import '~a-icons/a-icon';
#font-face {
font-family: 'a-icon';
src: url('../../../node_modules/a-icons/a-icon.eot');
src:
url('../../../node_modules/a-icons/a-icon.ttf') format('truetype'),
url('../../../node_modules/a-icons/a-icon.woff') format('woff'),
url('../../../node_modules/a-icons/a-icon.svg') format('svg');
}
#import '~a-icons/a-icon' is specifically giving me an error saying that it can't find the .eot, .ttf, .woff, .svg
This is one of the errors:
ERROR in ./node_modules/css-loader?{"sourceMap":false,"importLoaders":1}!./node_modules/postcss-loader/lib?{"ident":"postcss","sourceMap":false}!./node_modules/sass-loader/lib/loader.js?{"sourceMap":false,"precision":8,"includePaths":[]}!./src/assets/stylesheets/global-styles.scss
Module not found: Error: Can't resolve './a-icon.eot' in '/Users/rmadan/a-one/a-platform/src/assets/stylesheets'
resolve './a-icon.eot' in '/Users/rmadan/a-one/a-platform/src/assets/stylesheets'
using description file: /Users/rmadan/a-one/a-platform/package.json (relative path: ./src/assets/stylesheets)
Field 'browser' doesn't contain a valid alias configuration
after using description file: /Users/rmadan/a-one/a-platform/package.json (relative path: ./src/assets/stylesheets)
using description file: /Users/rmadan/a-one/a-platform/package.json (relative path: ./src/assets/stylesheets/a-icon.eot)
no extension
Field 'browser' doesn't contain a valid alias configuration
/Users/rmadan/a-one/a-platform/src/assets/stylesheets/a-icon.eot doesn't exist
.ts
Field 'browser' doesn't contain a valid alias configuration
/Users/rmadan/a-one/a-platform/src/assets/stylesheets/a-icon.eot.ts doesn't exist
.js
Field 'browser' doesn't contain a valid alias configuration
/Users/rmadan/a-one/a-platform/src/assets/stylesheets/a-icon.eot.js doesn't exist
as directory
/Users/rmadan/a-one/a-platform/src/assets/stylesheets/a-icon.eot doesn't exist
[/Users/rmadan/a-one/a-platform/src/assets/stylesheets/a-icon.eot]
[/Users/rmadan/a-one/a-platform/src/assets/stylesheets/a-icon.eot.ts]
[/Users/rmadan/a-one/a-platform/src/assets/stylesheets/a-icon.eot.js]
[/Users/rmadan/a-one/a-platform/src/assets/stylesheets/a-icon.eot]
# ./node_modules/css-loader?{"sourceMap":false,"importLoaders":1}!./node_modules/postcss-loader/lib?{"ident":"postcss","sourceMap":false}!./node_modules/sass-loader/lib/loader.js?{"sourceMap":false,"precision":8,"includePaths":[]}!./src/assets/stylesheets/global-styles.scss 6:76081-76109
# ./src/assets/stylesheets/global-styles.scss
# multi ./src/assets/stylesheets/global-styles.scss ./node_modules/normalize.css/normalize.css
I'm using scss, but the font is in vanilla css.
I've also tried #import '../../../node_modules/a-icons/a-icon.css'; The icons show up as a square and I think that's because the font-family isn't recognized. So I added #font-face, but that it doesn't do anything.
I also tried changing angular-cli.json to include it in the assets and style:
"assets": [
"assets",
"assets/languages",
"assets/stylesheets",
"favicon.ico",
"environments/environment.values.js",
{
"glob": "a-icon.*",
"input": "../node_modules/a-icons",
"output": "./assets/a-icons"
}
"styles": [
"assets/stylesheets/global-styles.scss",
"../node_modules/normalize.css/normalize.css",
"../node_modules/a-icons/a-icon.css"
]
This doesn't do anything.
First, I advise you to create a single css file "fonts.css" in your node module, it will be simple to include a css file in your application rather than redefine the fonts and refer to N files (woff2, eot...ect), for example:
Your fonts.css:
#font-face {
font-family: 'a-icon';
src: url('a-icon.eot');
src:
url('a-icon.ttf') format('truetype'),
url('a-icon.woff') format('woff'),
url('a-icon.svg') format('svg');
}
Second, include the file "fonts.css" in your styles.css of your angular application as follows:
#import "~a-icons/fonts.css";
I have a similar solution that works fine, my node module contains a theme with fonts.
Why did you refer To a node module to get icons? The font is related to the theme of your application so it should be within your assets folder and then simply add: import the-name-font in your main stylesheet.
If you need to import external css files from node_modules, add their links in angular cli config file To import them in the assets folder and then import them in your main stylesheet from assets folder
Hope my response can help you

Laravel SCSS file can't cope with relative paths for #font-face

I'm working on an ".scss" file for a Laravel website. I'm trying to use a locally stored ".otf" font file in a CSS #font-face tag.
The problem is that Laravel / web pack doesn't seem to be able to cope with relative paths for CSS src: properties. However, I need it to because the directory structure is going to change from the current development environment to when it's deployed, so I'm avoiding hard coding the full path.
The SCSS code:
#font-face {
font-family: LucyScript;
src: url('../fonts/LucyScript.otf');
}
Error message (npm run watch):
Update:
I've managed to put in a "workaround" by adding the following to my "app.blade.php" file:
<style type="text/css">
#font-face {
font-family: LucyScript;
src: url('./fonts/LucyScript.otf');
}
</style>
However, this isn't ideal as it's now mixing the CSS and HTML, if anyone knows how to resolve this just from within the ".scss" please enlighten me.
Thank you!

Grails 2.3 changes css font-face url to "resource:/..."

I want to include a custom font in my CSS like this:
#font-face
{
font-family: TheFont;
src: url(fonts/SourceSansProLight.ttf);
}
The CSS is served with Grails 2.3 and the CSS is modified to become this
#font-face
{
font-family: TheFont;
src: url(resource:/css/fonts/fonts/SourceSansProLight.ttf);
}
The resulting font url scheme is unknown and browsers can't open that file. Chrome, for example, reports:
GET resource:/css/fonts/fonts/SourceSansProLight.ttf net::ERR_UNKNOWN_URL_SCHEME
/css/fonts is prepended to the original URL as well.
How can I instruct Grails to leave the font-face URL exactly as it is?
The solution was to disable CSS processing in Config.groovy:
grails.resources.rewrite.css = false
I found the tip how to do that on the Grails mailing list.
A better solution I think is proposed by dmahapatro at: https://stackoverflow.com/a/22849288/2286664
You need to ensure your font files are known to the Resources plugin.
The following worked for me in my Config.groovy, adapt it based on your paths:
grails.resources.adhoc.includes = [
'/images/**', '/css/**', '/js/**', '/img/**', '/fonts/**'
]
You'll needed to run grails clean after making this change.

yeoman generator font awesome

I created a mobile application with the yeoman mobile-generator.
Now I want to add font awesome. I tried this, but not really works in the compiling process. A small advice or experience would be great
Okay, it's really a bit unclear. Sorry.
The compiling process works, but console says 404 not founds for the fonts.
It's not really clear to me how is a good way to do this. Creating the CSS from the scss files, implemeted the .min.css in the index.html with "<-- build: ... -->" or something else?!
When I add this in my main .scss file the the fonts would not be found.
$fontAwesomePath: "../bower_components/font-awesome/fonts";
#import '../bower_components/font-awesome/scss/font-awesome.scss';
Grunt copies and renames the font files f.e. to:
5a6b8fb8.FontAwesome
Sounds like grunt rev https://github.com/cbas/grunt-rev is renaming your font file. Just look for something like this in your Gruntfile:
// Renames files for browser caching purposes
rev: {
dist: {
files: {
src: [
'<%= yeoman.dist %>/styles/fonts/*'
...
remove the fonts line and you should be okay.
Ok,
seems I could fixed this with the help of this answer.
Installed font awesome with bower:
bower install --save font-awesome
Then simply this line in the index.html fixed my problem.
<!-- build:css styles/vendor/fontawesome.css -->
<link rel="stylesheet" href="bower_components/font-awesome/css/font-awesome.min.css">
<!-- endbuild -->
Thanks #OddEssay and #bpaul for our help!!
#importing the scss takes care of the CSS side of things, but I think you also need to move the assests to a location the browser can access them, so grunt-contrib-copy would do the job perfectly. So if your webroot is public Something like:
copy: {
main: {
files: [
{expand: true, cwd: '../bower_components/font-awesome/fonts', src: ['*.*'], dest: 'public/fonts'}
]
}

Resources