csssasswebpackglyphiconsrelative-url

Webpack: overwriting url for @font-face in SASS (Glyphicons)


I've imported "glyphicons-halflings": "1.9.0", which does not contain the actual fonts. Therefore the fonts are stored relative to my application in app/assets/fonts.

I am dealing with errors such as this:

ERROR in ./~/css-loader?sourceMap!./~/resolve-url-loader!./~/sass-loader?sourceMap!./app/scss/application.scss
Module not found: Error: Cannot resolve 'file' or 'directory' ../fonts/glyphicons-halflings-regular.eot in C:\work\myapp\src\MyApp.Web\app\scss
@ ./~/css-loader?sourceMap!./~/resolve-url-loader!./~/sass-loader?sourceMap!./app/scss/application.scss 6:168498-168550 6:168573-168625

To me, this seems as if the url defined in node_modules/glyphicons-halflings/scss/glyphicons-halflings/_glyphicons-halflings.scss is not correctly being overwritten

_glyphicons-halflings.scss defines

@font-face {
  font-family: 'Glyphicons Halflings';
  src: url('../fonts/glyphicons-halflings-regular.eot');
  src: url('../fonts/glyphicons-halflings-regular.eot?#iefix') format('embedded-opentype'),
       url('../fonts/glyphicons-halflings-regular.woff2') format('woff2'),
       url('../fonts/glyphicons-halflings-regular.woff') format('woff'),
       url('../fonts/glyphicons-halflings-regular.ttf') format('truetype'),
       url('../fonts/glyphicons-halflings-regular.svg#glyphicons_halflingsregular') format('svg');
}

My own app/scss/_font.scss defines

@font-face {
    font-family: 'Glyphicons Halflings';
    src: url('../assets/fonts/glyphicons/glyphicons-halflings-regular.eot');
    src: url('../assets/fonts/glyphicons/glyphicons-halflings-regular.eot?#iefix') format('embedded-opentype'),
         url('../assets/fonts/glyphicons/glyphicons-halflings-regular.woff2') format('woff2'),
         url('../assets/fonts/glyphicons/glyphicons-halflings-regular.woff') format('woff'),
         url('../assets/fonts/glyphicons/glyphicons-halflings-regular.ttf') format('truetype'),
         url('../assets/fonts/glyphicons/glyphicons-halflings-regular.svg#glyphicons_halflingsregular') format('svg');
}

The only difference between these two are the urls. I define a relative path to the _font.scss (or rather, application.scss) but webpack cannot resolve the urls defined in the glyphicons-halflings module.

Both these files are imported in the app/scss/application.scss:

@import "variables";
@import "mixins";
@import "~bootstrap/scss/bootstrap";
@import "~font-awesome/scss/font-awesome";
@import "~glyphicons-halflings/scss/glyphicons-halflings";
@import "bootstrap-override";
@import "libs-override";

@import "font";

I've set up webpack like this: webpack.common.js

module: {
    loaders: [
        {
            test: /\.ts$/,
            include: helpers.root('app'),
            loaders: ['awesome-typescript-loader', 'angular2-template-loader']
        },
        {
            test: /\.html$/, // mostly angular templates
            include: helpers.root('app'),
            loader: 'html'
        },
        {
            test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)(\?|$)/,
            include: helpers.root('app'),
            loader: 'file?name=[path][name].[hash].[ext]'             
        },
        {
            test: /\.scss$/,
            include: helpers.root('app'),
            loaders: ['style', 'css?sourceMap', 'resolve-url', 'sass?sourceMap']
        },
        {
            test: /\.css$/,
            include: helpers.root('app'),
            loader: 'to-string!css!resolve-url'
        }
    ]
},

For reference, my folder structure is like this:

+-- app
|    +-- assets
|        +-- fonts
|            +-- font-awesome
|                +-- FontAwesome.otf
|                +-- ...
|            +-- glyphicons
|                +-- ...
|            +-- ...
|        +-- icon
|            +-- ...
|        +-- ...
|   +-- sass
|       +-- _font.css
|       +-- application.scss
|       +-- ...
|   +-- ...
+-- appdist (later for server-side, if ever)
+-- node_modules
+-- ...
+-- wwwroot
    +-- dist
        +-- ...webpack bundled content goes here...

TL;DR package glyphicons-halflings 1.9.0 defines a @font-face which I'm having trouble to overwrite (because the urls are wrong). All SASS, webpack loader pipeline is 'style', 'css?sourceMap', 'resolve-url', 'sass?sourceMap'


Solution

  • I've stopped using the glyphicons-halflings package. I instead copied the contents next to my own code and changed the paths.

    My guess is that webpack sass-loader evaluates every require(…) separately - and the glyphicions-halflings package does not pass evaluation.