css - Webpack:在 SASS(Glyphicons)中覆盖@font-face 的 url

标签 css sass webpack glyphicons relative-url

我导入了 "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');
}

这两者之间的唯一区别是 url。我定义了 _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...

长话短说glyphicons-halflings 1.9.0 定义了一个 @font-face ,我无法覆盖它(因为 url 是错误的)。所有 SASS、webpack 加载器管道都是 'style', 'css?sourceMap', 'resolve-url', 'sass?sourceMap'

最佳答案

我已经停止使用 glyphicons-halflings 包。 我改为复制我自己的代码旁边的内容并更改路径。

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

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

相关文章:

html - 将 CSS 形状的边框宽度设置为始终形成三 Angular 形

javascript - create-react-app webpack 配置和文件在哪里?

asp.net-core - “asp-src-include”不解析文件

javascript - Karma + Webpack(babel-loader)+ ES6 "Unexpected token import"

javascript - 将随机数插入到背景颜色的内联样式中

css - 可以将 Wordpress 固定页脚更改为响应式页脚吗?

html - 无法将链接的宽度设置为相同

html - Sass 和第一个元素

css - 如何使用 CSS-in-JS 方法设置 body 标签的样式?

css - Eclipse插件自动编译Sass文件