5

I'm using the NET.Core 2.0 Angular template, which work with webpack, angular-cli, angular component, typescript.

I did:

command line - Install package and loader

npm install --save font-awesome
npm install url-loader --save-dev

webpack.config.js - add loader rule

    module: {
        rules: [
             ...
            { test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "url-loader" },
            { test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "url-loader" }
        ]
    },    

my.component.css - Import to my component

@import '~font-awesome/css/font-awesome.css';

my.component.html - Put the icon

<i class="fa fa-check fa-6"></i>

Now I got no error message but still can't see the icon.

Did I do anything wrong?

2
  • This isn't anything to do with Angular or ASP.Net Core, it's just plain CSS. You can't use ~ in the import, try: @import '/font-awesome/css/font-awesome.css'; Commented Sep 5, 2017 at 10:33
  • I keep getting this error Error: Cannot find module "-!../../../../node_modules/css-loader/index.js!./font-awesome/css/font-awesome.css" at webpackMissingModule but with ~ is OK. Commented Sep 6, 2017 at 6:00

3 Answers 3

11

This is for .NET Core 2, after you create a SPA Project using dotnet new angular:

  1. Go to the root of the project and install the package: npm install font-awesome --save. You should now see it in package.json dependencies.

  2. After that go to webpack.config.vendor.js and add font awesome to the array under non tree shakable modules:

    const nonTreeShakableModules = [
        'bootstrap',
        'bootstrap/dist/css/bootstrap.css',
        'es6-promise',
        'es6-shim',
        'event-source-polyfill',
        'font-awesome/css/font-awesome.css',
        'jquery',
    ];
    
  3. Now we need to tell the webpack that we added this new package. So if you haven't done so before install this in the root of project with npm install --save-dev npm-install-webpack-plugin.

  4. Finally, run this command in the root of project: webpack --config webpack.config.vendor.js

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you so much! I was ripping my hair out. I did not have to do step 3, just step 4.
FYI - my above comment is for webpack version 2.5.1.
I had to install webpack globally npm install -g webpack for #4 to work for me. I also had to remember to rebuild my app and F5 to see the changes in the browser.
None of these worked for me. I started with a fresh angular template. I've added the .css entry to the nonTreeShakableModules array. I then ran step 3, which gave me an error. So I tried step 4 (with and without global webpack installation), and I get "missing script: webpack".
I don't have webpack.config.vendor.js after dotnet new angular. Can I simply make one?
3

Thank you guys for your help~

Eventually I fixed the issue by updating the webpack.config.js to:

module: {
  loaders: [
    {
        test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
        loader: "url-loader"
    },
    {
        test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
        loader: "url-loader"
    }
  ]
}

rather than put it in the rules: [...]

It's weird that rules won't work...but anyway~ :P

Comments

2

Maybe I am the only one who installed font-awesome like npm install fontawesome --save? Well, it should have been like npm install font-awesome --save. Dammit!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.