我导入了"glyphicons-halflings": "1.9.0",其中不包含实际的字体。因此,字体相对于我的应用程序存储在app/assets/fonts中。

我正在处理这样的错误:

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


对我来说,这似乎是node_modules/glyphicons-halflings/scss/glyphicons-halflings/_glyphicons-halflings.scss中定义的url没有被正确覆盖

_glyphicons-halflings.scss定义

@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');
}


我自己的app/scss/_font.scss定义

@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');
}


两者之间的唯一区别是网址。我定义了_font.scss(或更确切地说是application.scss)的相对路径,但是webpack无法解析glyphicons-halflings模块中定义的url。

这两个文件都导入到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";


我已经像这样设置了webpack:
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'
        }
    ]
},


供参考,我的文件夹结构如下:

+-- 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
glyphicons-halflings 1.9.0定义了一个@font-face,我很难覆盖它(因为网址错误)。所有SASS,Webpack加载程序管道均为'style', 'css?sourceMap', 'resolve-url', 'sass?sourceMap'

最佳答案

我已经停止使用glyphicons-halflings软件包。
相反,我将内容复制到自己的代码旁边并更改了路径。

我的猜测是webpack sass-loader分别评估每个require(…)-而glyphicions-halflings包未通过评估。

关于css - Webpack:在SASS中覆盖@ font-face的网址(Glyphicons),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40380277/

10-09 03:13