无需使用SASS。如果您正在使用CSS,则需要使用css-loader
;如果您正在使用SASS,则需要使用sass-loader
。请注意,如果您使用SASS,则需要两个加载器。
这两个加载器都将打包url()
语句。但是,它们只能在URL为相对URL时才能工作(这可能是当前答案似乎无法工作的原因)。
这意味着您需要下载字体。更麻烦的是,每种字体都有多种格式,如果要支持所有浏览器,则需要所有格式。幸运的是,有一个出色的网站可以帮助我们:google-webfonts-helper。
将所需字体输入该网站,它将为您生成类似以下的CSS规则:
@font-face {
font-family: 'Roboto';
font-style: normal;
font-weight: 400;
src: url('../fonts/roboto-v18-latin-regular.eot');
src: local('Roboto'), local('Roboto-Regular'),
url('../fonts/roboto-v18-latin-regular.eot?#iefix') format('embedded-opentype'),
url('../fonts/roboto-v18-latin-regular.woff2') format('woff2'),
url('../fonts/roboto-v18-latin-regular.woff') format('woff'),
url('../fonts/roboto-v18-latin-regular.ttf') format('truetype'),
url('../fonts/roboto-v18-latin-regular.svg#Roboto') format('svg');
}
这个CSS规则是通过
url()
导入的,而且URL是相对路径。这意味着
css-loader
会将其打包到您的应用程序中。但是,您还必须下载所有这些URL引用的文件。幸运的是,上面提到的
google-webfonts-helper
网站为此提供了一个下载链接。下载这些字体并将它们放在
../fonts
(或者任何您想要的目录。我个人使用
./assets/fonts
)。如果您有自定义目录,
google-webfonts-helper
工具有一个可以使用的输入。
附加内容: Material Icons字体
Google的Material Icons通常以字体形式呈现在网站上。然而,它们需要特殊的CSS才能使它们正常工作。如果您想打包Material Icons字体,则需要以下字体格式:
@font-face {
font-family: 'Material Icons';
font-style: normal;
font-weight: 400;
src: url('../fonts/MaterialIcons-Regular.eot');
src: local('Material Icons'),
local('MaterialIcons-Regular'),
url('../fonts/MaterialIcons-Regular.eot?#iefix') format('embedded-opentype'),
url('../fonts/MaterialIcons-Regular.woff2') format('woff2'),
url('../fonts/MaterialIcons-Regular.woff') format('woff'),
url('../fonts/MaterialIcons-Regular.ttf') format('truetype'),
url('../fonts/MaterialIcons-Regular.svg#Inconsolata') format('svg');
}
你可以从
这里下载字体文件(在解压的zip文件中查找
iconfont
目录)。
此外,您还需要在字体规则后添加以下CSS:
.material-icons {
font-family: 'Material Icons';
font-weight: normal;
font-style: normal;
font-size: 24px;
display: inline-block;
line-height: 1;
text-transform: none;
letter-spacing: normal;
word-wrap: normal;
white-space: nowrap;
direction: ltr;
-webkit-font-smoothing: antialiased;
text-rendering: optimizeLegibility;
-moz-osx-font-smoothing: grayscale;
font-feature-settings: 'liga';
}
注意:使用材料图标字体的说明来自这里,以防这些说明过时。