我们有一个大型项目,在那里我们尝试实现ITCSS模式(它工作得很好),并在其中添加由我们的石墨家创建的自定义图标字体。
以下是文件夹结构(并非详尽无遗):
.
├── build
│ └── webpack.config.js
└── src
├── components # Using React
│ ├── test.jsx
│ └── test.scss
└── styles
├── _objects # Components style reusable
│ ├── _all.scss
│ └── table.scss
├── _settings # Global variables
│ ├── _all.scss
│ ├── _custom-icons.scss # Define icon font (see below)
│ ├── _custom-icons-variables.scss # Define icon codes
│ ├── _colors.scss # Define colors
│ ├── _predefined-colors.scss # Define brand colors
│ └── _theme.scss # Define specific theme
├── _tools # Mixins / helpers
│ ├── _all.scss
│ ├── _animation.scss
│ ├── _breakpoints.scss
│ └── _transition.scss
├── _wins # Overrides
│ ├── _all.scss
│ ├── _display.scss
│ ├── _position.scss
│ └── _text.scss
├── assets
│ └── fonts
│ ├── custom-icons.eot
│ ├── custom-icons.svg
│ ├── custom-icons.ttf
│ └── custom-icons.woff
├── _base.scss # HTML Selectors
├── _generic.scss # Reset (normalize)
└── main.scss # Loads everything好的,这是一个相当沉重的文件结构,但它做的工作,我们想要实现什么!
我们在davezuko的redux kit上启动了我们的应用程序。我们调整webpack配置文件以满足我们的需要,但没有更改样式部分,因此您可以参考webpack.config.js来自GitHub。
下面是我们的文件:
_custom-icons-variables.scss
$fontPath: "./assets/fonts" !default;
$aw-happy: "\e9df";
$aw-smile: "\e9e1";
$aw-tongue: "\e9e3";
$aw-sad: "\e9e5";
$aw-wink: "\e9e7";
$aw-grin: "\e9e9";
$aw-cool: "\e9eb";
$aw-angry: "\e9ed";
…_custom-icons.scss
自定义图标字体依赖于语义-UI字体图标样式。
@import "custom-icons-variables";
$fontName: 'custom-icons';
$fallbackSRC: url("#{$fontPath}/#{$fontName}.eot");
$src:
url("#{$fontPath}/#{$fontName}.eot?#iefix") format('embedded-opentype'),
url("#{$fontPath}/#{$fontName}.woff") format('woff'),
url("#{$fontPath}/#{$fontName}.ttf") format('truetype'),
url("#{$fontPath}/#{$fontName}.svg?##{$fontName}") format('svg')
;
@font-face {
font-family: $fontName;
src: $fallbackSRC;
src: $src;
font-weight: normal;
font-style: normal;
}
i.icon.aw {
font-family: $fontName !important;
}
.aw-happy {
&:before {
content: $aw-happy;
}
}
.aw-smile {
&:before {
content: $aw-smile;
}
}
.aw-tongue {
&:before {
content: $aw-tongue;
}
}
.aw-sad {
&:before {
content: $aw-sad;
}
}
.aw-wink {
&:before {
content: $aw-wink;
}
}
.aw-grin {
&:before {
content: $aw-grin;
}
}
.aw-cool {
&:before {
content: $aw-cool;
}
}
.aw-angry {
&:before {
content: $aw-angry;
}
}
…main.scss
这里的所有内容都是全局的,因此我们避免添加散列,并允许我们在组件中使用直接的类名(参见下面的示例)。
:global {
@import "_settings/all";
@import "_settings/custom-icons";
@import "_tools/all";
@import "generic";
@import "base";
@import "_objects/all";
@import "_wins/all";
}test.jsx
在这里,您可以看到我们同时使用全局样式和作用域样式。
import React, { Component } from 'react'
import s from './test.scss'
export default class Test extends Component {
render () {
return (
<div className={s['test']}>
<i className='icon aw aw-happy'></i>
<h1>Test</h1>
</div>
)
}
}test.scss
是的,对于使用我们品牌颜色的每个组件,我们需要重新导入Sass文件…。我不知道这是否是件坏事,但它起作用了:)
@import "src/styles/_settings/all";
.test {
background: $brown;
color: $white_smoke;
}现在设置好了背景,下面是问题所在:
aw-happy图标呈现为正方形(就像字体未加载…时一样)我无法使它发挥作用,在浏览了大量资源之后,我尝试了所有可能的方法:/
我们使用的是与webpack一起构建并捆绑的语义用户界面,因此我尝试将@font-face规则添加到semantic.min.css中,并将字体文件移到语义文件夹中:
semantic.min.css
@font-face {
font-family: 'custom-icons';
src: url(themes/default/assets/fonts/custom-icons.eot);
src: url(themes/default/assets/fonts/custom-icons.eot?#iefix) format('embedded-opentype'),
url(themes/default/assets/fonts/custom-icons.woff) format('woff'),
url(themes/default/assets/fonts/custom-icons.ttf) format('truetype'),
url(themes/default/assets/fonts/custom-icons.svg?#custom-icons) format('svg');
font-weight: normal;
font-style: normal;
}
// Semantic stuff…和…它起作用了!
我想知道我是否最好像我们使用语义的方式那样构建和服务这个自定义字体,但是把所有的过程都从全局样式移开是很烦人的。
有什么办法解决这个问题,通过保留所有的Sass/Webpack的东西?
发布于 2016-06-29 12:31:01
下面是我通过浏览项目中关闭的bug找到的解决方案:
我将@font-face定义移到main.scss,在:global之上
main.scss
$font-path: 'styles/assets/fonts';
$font-name: 'custom-icons';
$fallback-src: url("#{$font-path}/#{$font-name}.eot");
$src:
url("#{$font-path}/#{$font-name}.eot?#iefix") format('embedded-opentype'),
url("#{$font-path}/#{$font-name}.woff") format('woff'),
url("#{$font-path}/#{$font-name}.ttf") format('truetype'),
url("#{$font-path}/#{$font-name}.svg?##{$font-name}") format('svg')
;
@font-face {
font-family: $font-name;
src: $fallback-src;
src: $src;
font-weight: normal;
font-style: normal;
}当我将$font-path定义为../styles/assets/fonts而不是./assets/fonts时,它也起作用了。
这是一个已知的bug,即在GitHub引用,但是davezuko没有在这个时候挖掘它。
https://stackoverflow.com/questions/38051502
复制相似问题