I am not finding any docs for what to do next, I installed into my project via npm,
$ npm install --save #fortawesome/fontawesome-free-webfonts
But now what? Can anyone point me to how to actually import or use it now?
Thank you.
First install it using NPM :
npm install #fortawesome/fontawesome-free --save-dev
Now you have two ways, one way is to embed the absolute URL within your HEAD section or if you working with something like SASS(SCSS), You can import it in your custom.scss file like this :
1- first set an absolute path for webfonts (It has to be set before the fontawesome.scss) :
$fa-font-path: "node_modules/#fortawesome/fontawesome-free/WebFonts" !default;
2- Then import fontawesome.scss :
#import "node_modules/#fortawesome/fontawesome-free/scss/fontawesome";
3- Finally import regular, solid or light(pro version) of icon types :
#import "node_modules/#fortawesome/fontawesome-free/scss/regular";
After all you can assign font-family to these classes to work :
.fa,.fas,.far,.fal,.fab {
font-family: "Font Awesome 5 Free";
}
by Gulp
in a SCSS file
#import "../../../node_modules/#fortawesome/fontawesome-free/scss/fontawesome";
#import "../../../node_modules/#fortawesome/fontawesome-free/scss/brands";
#import "../../../node_modules/#fortawesome/fontawesome-free/scss/regular";
#import "../../../node_modules/#fortawesome/fontawesome-free/scss/solid";
#import "../../../node_modules/#fortawesome/fontawesome-free/scss/v4-shims";
in gulpfile.js
gulp.task('icons', function() {
return gulp.src('node_modules/#fortawesome/fontawesome-free/webfonts/*')
.pipe(gulp.dest(dist+'/assets/webfonts/'));
});
Something like this
// ------------ FONT-AWESOME --------------------//
$fa-font-path: $fonts_path + 'font-awesome';
#import
'../../node_modules/#fortawesome/fontawesome-free-webfonts/scss/fontawesome';
#import
'../../node_modules/#fortawesome/fontawesome-free-webfonts/scss/fa-brands';
#import
'../../node_modules/#fortawesome/fontawesome-free-webfonts/scss/fa-regular';
#import
'../../node_modules/#fortawesome/fontawesome-free-webfonts/scss/fa-solid';
To use an npm module on your front-end you'll need to be doing some sort of transpiling with webpack, gulp, or grunt most likely.
Then at the entry point of your application before you write html/jsx/some other html-esque code you'll need to require or import the css file from the font-awesome npm module.
// es6/babel import
import 'font-awesome/css/font-awesome.min.css';
// browserify require statement if not using babel/es6
require('font-awesome/css/font-awesome.min.css');
For example heres a react app I made with webpack/babel and on the entry point file I import the css file from font-awesome: https://github.com/AkyunaAkish/akyuna_akish/blob/master/client/index.js
FYI:
I used
npm install --save font-awesome
Instead of the command you used. You may need to adjust the import/require statement file path to point to the main css file in your font-awesome node_module package if we have slightly different packages.
Related
I've just tried to add FontAwesome to one of my Laravel 8 projects (using TailwindCSS). I've installed it using
npm install #fortawesome/fontawesome-free
Then added it to ./resources/css/app.css (with my TailwindCSS imports)
#tailwind base;
#tailwind components;
#tailwind utilities;
#import '~#fortawesome/fontawesome-free/css/fontawesome.css';
#import '~#fortawesome/fontawesome-free/css/regular.css';
#import '~#fortawesome/fontawesome-free/css/solid.css';
#import '~#fortawesome/fontawesome-free/css/brands.css';
Then applied the build with
npm run production
However it is giving me a 404 Error regarding the font files. They are correctly sitting inside the ./public/fonts directory but the compiled css file is looking for them in the web servers root directory.
If I manually change the compiled css file to replace
src: url(/fonts/vendor/#fortawesome/fontawesome-free/webfa-regular-400.eot?08f9891a6f44d9546678133ab121fc54);
with
src: url(../fonts/vendor/#fortawesome/fontawesome-free/webfa-regular-400.eot?08f9891a6f44d9546678133ab121fc54);
Then it works fine, how can I specify the FontAwesome dir so that I don't have to do this manual edit?
UPDATE
After some digging I found the file ./node_modules/laravel-mix/src/config.js which has the following code
fileLoaderDirs: {
images: 'images',
fonts: 'fonts'
},
This seems to be where FontAwesome is getting it's dir from but is there any way to override it?
You don't have to go so far to get it working, at least I didn't have to.
This was my config laravel mix config file after installing tailwindcss, since tail wind uses postcss. This are steps you take after you install tailwindcss and font-awesome.
mix.js('resources/js/app.js', 'public/js').postCss('resources/css/app.css', 'public/css', [
require('postcss-import'),
require('tailwindcss'),
require('autoprefixer'),
]);
After adding installing font-awesome, I just added this line
mix.js('resources/js/app.js', 'public/js')
.postCss('resources/css/app.css', 'public/css', [
require('postcss-import'),
require('tailwindcss'),
require('autoprefixer')
])
.sass('resources/sass/app.scss', 'public/css') // transpiling app.scss
.copy( // copying fonts directory
'node_modules/#fortawesome/fontawesome-free/webfonts',
'public/webfonts'
).sourceMaps();
then I had a sass file in resoures/sass/app.scss with the font-awesome import, since font-awesome is added using sass.
// Fontawesome
#import "~#fortawesome/fontawesome-free/scss/fontawesome";
#import "~#fortawesome/fontawesome-free/scss/regular";
#import "~#fortawesome/fontawesome-free/scss/solid";
#import "~#fortawesome/fontawesome-free/scss/brands"
My app.css file inside resources/css/app.css looks like this
#import 'tailwindcss/base';
#import 'tailwindcss/components';
#import 'tailwindcss/utilities';
after that just run the npm command to compile
npm run dev
I wasn't able to get FontAwesome to work with PostCSS but I was able to get it to work with SASS.
I have seen that there are a few different ways to setup SASS with Laravel 8 but I am just posting the method that worked for me in case it can help someone else.
First change css into scss
mv ./resources/css ./resources/sass
mv ./resources/sass/app.css ./resources/sass/app.scss
Then add TailwindCSS and FontAwesome imports to app.scss
#tailwind base;
#tailwind components;
#tailwind utilities;
$fa-font-path: "../webfonts"; // this might be the missing piece that doesn't work without SASS
#import "~#fortawesome/fontawesome-free/scss/fontawesome";
#import "~#fortawesome/fontawesome-free/scss/solid";
#import "~#fortawesome/fontawesome-free/scss/brands";
#import "~#fortawesome/fontawesome-free/scss/regular";
Change webpack.mix.js to the following
const mix = require('laravel-mix');
const tailwindcss = require('tailwindcss');
mix
.js('resources/js/app.js', 'public/js')
.copy('node_modules/#fortawesome/fontawesome-free/webfonts', 'public/webfonts')
.sass('resources/scss/app.scss', 'public/css')
.options ({
processCssUrls: false,
postCss: [ tailwindcss('./tailwind.config.js') ]
});
Compile with npm run production
I am using Laravel Mix in a project without Laravel and I was having this problem with Font Awesome. After several hours of searching, I was able to solve the issue (I think):
webpack.mix.js
let mix = require("laravel-mix");
mix.options({
fileLoaderDirs: {
images: 'dist/images',
fonts: 'dist/fonts'
}
});
mix
.postCss("assets/css/app.css", "dist")
.js("assets/js/app.js", "dist")
.sourceMaps();
I am writing a Server Side Rendered app with Svelte/Sapper and I am having trouble using Font Awesome fonts.
I am using the following to load the font:
<script>
import Icon from "svelte-awesome";
import { faTimes } from "#fortawesome/free-solid-svg-icons/faTimes";
</script>
<Icon data={faTimes} />
The error I am seeing is:
" is not a valid SSR component. You may need to review your build config to ensure that dependencies are compiled, rather than imported as pre-compiled modules"
Is there a solution to this?
I landed on this page looking for ways to import just the FontAwesome CSS into Svelte without any additional components or dependencies. Turns out that with Svelte this is just as simple as with plain HTML: Get the FontAwesome CSS (e.g. via package manager, CDN, or download) and add it to the HTML <head> section.
I like to install it via npm to maintain its version together with all other depencies:
npm install --save #fortawesome/fontawesome-free
After installing, just add the stylesheet (from the #fortawesome directory inside node_modules) to the <head> section of app.html.
<head>
...
<link href="./../node_modules/#fortawesome/fontawesome-free/css/all.min.css" rel="stylesheet">
...
</head>
Use FontAwesome icons in your Svelte components, e.g. <i class="fa-regular fa-lightbulb">
For svelete-aweome, the import should look as follows in Sapper:
import Icon from 'svelte-awesome/components/Icon';
I am using Create-React-App and I want to add background image for my header section and I am doing this in that way:
background-image: url('~/Screenshot_11.png');
After this I'm getting this error:
./src/styles/main.scss
(./node_modules/css-loader/dist/cjs.js??ref--6-oneOf-5-1!./node_modules/postcss-loader/src??postcss!./node_modules/sass-loader/lib/loader.js??ref--6-oneOf-5-3!./src/styles/main.scss)
Module not found: You attempted to import
../../../../../../../Screenshot_11.png which falls outside of the
project src/ directory. Relative imports outside of src/ are not
supported.
I've set up homepage in package.json
"homepage": "http://localhost:3000",
In my older projects that works but today I cannot import this correctly.
They have changed that but I don't know why. Working path:
background-image: url('/Screenshot_11.png');
EDIT 2021
For people who think that it doesn't work:
https://codesandbox.io/s/agitated-turing-gsnr3
you can import that image as
import Background from './Screenshot_11.png'
and use
background-image: `url(${Background})`
This still does not work for me with images in the public folder.
UPDATED 19 March 2021
Regarding using of <ROOT/public/images> in .css files.
It appears to be a breaking change (will be considered as a bug?) in create-react-app (react-scripts) package v4.x.
And more precisely in package 'css-loader' v4.x.
3.x branch works OK with that.
Here is the corresponding issue on the github repo:
https://github.com/facebook/create-react-app/issues/9870
(and there are few more actually).
No fixes (yet). (will be?..)
But a few workarounds mentioned there.
Which one to use... it depends on your project, I suppose.
Some of workarounds:
downgrade to react-scripts 3.4.x
don't use url in CSS files :) you still can use in .JSX (inline styles). Or put in .html. They are obviously not processed by css-loader.
reconfigure webpack to add url:false to css-loader options (either eject CRA or use this: https://github.com/gsoft-inc/craco or this: https://github.com/timarney/react-app-rewired
(you can find sample configurations at the github issue page)
use this new feature of css-loader https://github.com/webpack-contrib/css-loader/pull/1264
(released in 5.1.0, current last version is 5.1.3; to use that version you can add the following to the package.json: "resolutions": { "css-loader": "5.1.3" } (at root level) )
I am using the following Gulp task to copy required fonts from the Font Awesome Bower package, this works fine and outputs files like so:
App > Build > Fonts > Font Files
gulp.task('copyfonts', function() {
gulp.src('./bower_components/components-font-awesome/fonts/**/*.{ttf,woff,eof,svg}')
.pipe(gulp.dest('./fonts'));
});
I am trying to refactor this task so that it moves the fonts from any bower package (Bootstrap, for example) using the ** wildcard.
gulp.task('copyfonts', function() {
gulp.src('./bower_components/**/fonts/**/*.{ttf,woff,eof,svg}')
.pipe(gulp.dest('./fonts'));
});
App > Build > Fonts >
Bootstrap > Fonts > Font Files
Font-Awesome > Fonts > Font Files
TL;DR
Using my second method is copying the files as shown above, copying over the package folder and moving that with a 'fonts' folder as a child. Could anyone help show me where I have gone wrong...
I'm a little late to the party here, but for anyone that stumbles upon this in the future: I believe you have a typo in your font glob: eof should be eot.
gulp.src('./bower_components/**/fonts/**/*.{ttf,woff,eof,svg}')
should be
gulp.src('./bower_components/**/fonts/**/*.{ttf,woff,eot,svg}')
Not the best idea to always rely that a package will keep fonts in specific folders, those things can change.
Possibly you're missing another /**/ before fonts (one folder level). Or why all those checks, you know what extensions fonts will have, perhaps add wildcard like **/*.{ttf,woff,eof,svg}
Have you ever tried managing it with gulp-main-bower-files? https://github.com/ck86/main-bower-files
you can test this task
i worked by it
gulp.task('copyfonts', () =>
gulp.src('./app/assets/fonts/*')
.pipe(gulp.dest('./dist/assets/fonts'))
);
I ran into this same issue. After running my gulp builds, my fonts never successfully copied to my dest/fonts folder. The issue was inside my gulp.js file on these lines:
function fonts() {
return src('app/fonts/**/*.{eot,svg,ttf,woff,woff2}')
.pipe($.if(!isProd, dest('.tmp/fonts'), dest('dist/fonts')));
};
After editing the last line, my fonts successfully copied over!
function fonts() {
return src('app/fonts/**/*.{eot,svg,ttf,woff,woff2}')
.pipe(dest('dist/fonts'));
};
This is my application.css.sass :
#import "global.css.sass"
#import "bootstrap.css"
When I look at the source at my localhost application.css?body=1 I see this:
#import url(bootstrap.css);
#content-wrap {
width: 90%;
margin: 0 auto; }
So the global.css.sass contains this :
#content-wrap
width: 90%
margin: 0 auto
So that was imported appropriately, but not bootstrap.css file. And it works on the localhost because its on the file path locally. However in production my assets are on aws:
application-a0546f0315a891e8129e1f8e49eb7e45.css
When looking at source the content is the same as on my localhost, however there is not bootstrap on the path so that file is missing, and I wonder why isn't it all bundled in all one big application file, here are my assets related config.
Development :
config.assets.compress = false
config.assets.digest = false
Production :
config.serve_static_assets = false
# Compress JavaScripts and CSS
config.assets.compress = true
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false
# Generate digests for assets URLs
config.assets.digest = true
What am I doing wrong here? Any help is appreciated
Update :
This is sample bit from my chrome console :
Request URL:http://my-bucket.s3.amazonaws.com/assets/bootstrap.css
Request Method:GET
Status Code:403 Forbidden
Have a look in the SCSS Documentation, that explains the #import function from SCSS.
#import takes a filename to import. By default, it looks for a Sass
file to import directly, but there are a few circumstances under which
it will compile to a CSS #import rule:
If the files extension is .css.
If the filename begins with http://.
If the filename is a url().
If the #import has any media queries
It's important to see the difference between the usge of #import rule from css and the #import given by SCSS.
When you write this in your scss file:
#import "somefile.css"
The file somefile.css will not be merged within your scss compiled file. SASS will just compile this to the css #import rule, causing a http request to look for somefile.css on your server.
You can either want this, and upload somefile.css to the server under the correct path, or you just rename somefile.css to somefile.scss and change the import to #import "somefile.scss". Like this SASS will merge somefile.scss to the compiled file.