0
votes

I've tried several different solutions but not a single one have worked for me.

I'm using.NET Core, latest Aurelia/Aurelia CLI and Font-Awesome installed using npm install font-awesome --save.

Solution 1:

New file: prepare-font-awesome.js in folder \aurelia_project\tasks

import gulp from 'gulp';
import merge from 'merge-stream';
import changedInPlace from 'gulp-changed-in-place';
import project from '../aurelia.json';

export default function prepareFontAwesome() {
  const source = 'node_modules/font-awesome';

  const taskCss = gulp.src(`${source}/css/font-awesome.min.css`)
    .pipe(changedInPlace({ firstPass: true }))
    .pipe(gulp.dest(`${project.platform.output}/css`));

  const taskFonts = gulp.src(`${source}/fonts/*`)
    .pipe(changedInPlace({ firstPass: true }))
    .pipe(gulp.dest(`${project.platform.output}/fonts`));

  return merge(taskCss, taskFonts);
}

Updated build.js\aurelia_project\tasks

import prepareFontAwesome from './prepare-font-awesome'; // Our custom task

export default gulp.series(
  readProjectConfiguration,
  gulp.parallel(
    transpile,
    processMarkup,
    processCSS,
    prepareFontAwesome // Our custom task
  ),
  writeBundles
);

Included font-awesome in html

<link rel="stylesheet" href="scripts/css/font-awesome.min.css">

Error:

GET http://localhost:9000/scripts/css/font-awesome.min.css

Solution 2:

Updated aurelia.json

 {
    "name": "font-awesome",
    "path": "../node_modules/font-awesome/",
    "main": "",
    "resources": [
      "css/font-awesome.min.css"
    ]
  }

Added font files in root/font-awesome/fonts

Included font-awesome in html

<require from="font-awesome/css/font-awesome.min.css"></require>

Error: No error but icons are not shown

Solution 3:

Updated build.js:

import gulp from 'gulp';
import transpile from './transpile';
import processMarkup from './process-markup';
import processCSS from './process-css';
import { build } from 'aurelia-cli';
import project from '../aurelia.json';
import fs from 'fs';
import readline from 'readline';
import os from 'os';

export default gulp.series(
  copyAdditionalResources,
  readProjectConfiguration,
  gulp.parallel(
    transpile,
    processMarkup,
    processCSS
  ),
  writeBundles
);

function copyAdditionalResources(done){
  readGitIgnore();
  done();
}

function readGitIgnore() {
  let lineReader = readline.createInterface({
    input: fs.createReadStream('./.gitignore')
  });
  let gitignore = [];

  lineReader.on('line', (line) => {
    gitignore.push(line);
  });

  lineReader.on('close', (err) => {
    copyFiles(gitignore);
  })
}

function copyFiles(gitignore) {
  let stream,
    bundle = project.build.bundles.find(function (bundle) {
      return bundle.name === "vendor-bundle.js";
    });

  // iterate over all dependencies specified in aurelia.json
  for (let i = 0; i < bundle.dependencies.length; i++) {
    let dependency = bundle.dependencies[i];
    let collectedResources = [];
    if (dependency.path && dependency.resources) {
      // run over resources array of each dependency
      for (let n = 0; n < dependency.resources.length; n++) {
        let resource = dependency.resources[n];
        let ext = resource.substr(resource.lastIndexOf('.') + 1);
        // only copy resources that are not managed by aurelia-cli
        if (ext !== 'js' && ext != 'css' && ext != 'html' && ext !== 'less' && ext != 'scss') {
          collectedResources.push(resource);
          dependency.resources.splice(n, 1);
          n--;
        }
      }
      if (collectedResources.length) {
        if (gitignore.indexOf(dependency.name)< 0) {
          console.log('Adding line to .gitignore:', dependency.name);
          fs.appendFile('./.gitignore', os.EOL + dependency.name, (err) => { if (err) { console.log(err) } });
        }

        for (let m = 0; m < collectedResources.length; m++) {
          let currentResource = collectedResources[m];
          if (currentResource.charAt(0) != '/') {
            currentResource = '/' + currentResource;
          }
          let path = dependency.path.replace("../", "./");
          let sourceFile = path + currentResource;
          let destPath = './' + dependency.name + currentResource.slice(0, currentResource.lastIndexOf('/'));
          console.log('Copying resource', sourceFile, 'to', destPath);
          // copy files
          gulp.src(sourceFile)
            .pipe(gulp.dest(destPath));
        }
      }
    }
  }
}


function readProjectConfiguration() {
  return build.src(project);
}

function writeBundles() {
  return build.dest();
}

Updated aurelia.json

{
    "name": "font-awesome",
    "main":"",
    "path": "../node_modules/font-awesome",
    "resources": [
        "css/font-awesome.css",
        "/fonts/fontawesome-webfont.woff2",
        "/fonts/FontAwesome.otf",
        "/fonts/fontawesome-webfont.eot",
        "/fonts/fontawesome-webfont.svg",
        "/fonts/fontawesome-webfont.ttf"
    ]
}

Included font-awesome in html

<require from="font-awesome/css/font-awesome.css"></require>

Error:

get: http://localhost:9000/font-awesome/fonts/fontawesome-webfont.woff2?v=4.7.0 (same with woff and ttf)

It's really strange because the files are copied and the url is correct..

Folder structure:

enter image description here

Tested a couple of different sources

What am I missing?

I would prefer a less implementation so I can import Font-Awesome in my master less file.

2
To confirm, you have it so that your css file is being pulled successfully? The CSS should actually be bundled in your vendor-bundle most likely (it's wherever you have it defined). Can you confirm that the font-awesome css library is inside of your bundle? It will be defined as a module, probably something like font-awesome/css/font-awesome.css.Andrew
Hi, yes font-awesome is defined in vendor-bundle.Reft
The next thing to confirm is, when you require the font awesome bundle (like you described, and make sure that the "from" attribute is the same name that's defined in the vendor-bundle), do the font-awesome styles get added to the <head>?Andrew
Your missing piece is probably the actual font files themselves (github.com/FortAwesome/Font-Awesome/tree/master/fonts). While you're bringing in the CSS, you're not bringing in the fonts. Currently, you can't quite bundle fonts up with your vendor bundles, so you have to create a gulp task that copies your font files over to your output directory (in a location that font-awesome.css is expecting). Check out this line to know where to put your font files: github.com/FortAwesome/Font-Awesome/blob/master/css/…Andrew
@Andrew Yes the font-awesome styles get added to the head tag, i can see that the styles are added to the icons, but the font doesn't load.. I've added another solution, (3). My files are copied to the right folder, (root/font-awesome/fonts) but I still get an error saying: localhost:9000/font-awesome/fonts/… (same with woff & ttf)Reft

2 Answers

3
votes

Based off of the discussion, since you are hosting your project inside the wwwroot folder, you must base your "gets" for files from there.
So, if you move your font files into wwwroot/fonts/font-name.woff (or somewhere thereabouts), you should be golden.

0
votes

If you are on a webpack based project generated with latest (2019ish) aurelia cli , then adding fontawesome or bootstrap is pretty simple.

step 1: install fontawesome

check the official docs here. Just for completeness, here is the npm or yarn way for free version

//with npm
npm install --save-dev @fortawesome/fontawesome-free

// yarn
yarn add --dev @fortawesome/fontawesome-free

step 2: import the font

in your main.js or main.ts or app.js or app.ts , all of them will work equally well, which is better? ask google.

import '@fortawesome/fontawesome-free/css/all.min.css';

And an even simpler method would be to add the CDN version into the head of your index.esj or index.html file

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.8.1/css/all.css" />

all of the above work equally well, personally for public apps, I prefer CDN solution due to browser cache.