没有必要使用 SASS。您将需要使用css-loader(如果您使用CSS)或sass-loader(如果您使用SASS)。请注意,如果您使用的是 SASS,您将需要两个加载器。
两个加载器都会打包url() 语句。但是,它们只有在 URL 是相对 URL 时才有效(这可能是当前答案似乎不起作用的原因)。
这意味着您需要下载字体。更复杂的是,每种字体都有多种格式,如果您想支持所有浏览器,则需要所有格式。幸运的是,有一个很棒的网站可以帮助我们:google-webfonts-helper。
在该网站中输入您想要的字体,它会为您生成如下所示的 CSS 规则:
/* roboto-regular - latin */
@font-face {
font-family: 'Roboto';
font-style: normal;
font-weight: 400;
src: url('../fonts/roboto-v18-latin-regular.eot'); /* IE9 Compat Modes */
src: local('Roboto'), local('Roboto-Regular'),
url('../fonts/roboto-v18-latin-regular.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
url('../fonts/roboto-v18-latin-regular.woff2') format('woff2'), /* Super Modern Browsers */
url('../fonts/roboto-v18-latin-regular.woff') format('woff'), /* Modern Browsers */
url('../fonts/roboto-v18-latin-regular.ttf') format('truetype'), /* Safari, Android, iOS */
url('../fonts/roboto-v18-latin-regular.svg#Roboto') format('svg'); /* Legacy iOS */
}
此 CSS 规则是通过 url() 导入的,并且 URL 是相对的。这意味着css-loader 会将其打包到您的应用程序中。但是,您还必须下载这些 URL 引用的所有文件。幸运的是,上面提到的google-webfonts-helper 网站为此提供了一个下载链接。下载这些字体并将它们放在../fonts(或您想要的任何目录。我个人使用./assets/fonts。google-webfonts-helper 工具有一个输入,如果您有自定义目录,您可以使用)
奖励:Material Icons 字体
Google 的材料图标通常以字体的形式出现在网站上。但是,它们需要特殊的 CSS 才能使它们工作。如果要打包素材图标字体则需要如下字体:
@font-face {
font-family: 'Material Icons';
font-style: normal;
font-weight: 400;
src: url('../fonts/MaterialIcons-Regular.eot'); /* For IE6-8 */
src: local('Material Icons'),
local('MaterialIcons-Regular'),
url('../fonts/MaterialIcons-Regular.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
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'); /* Legacy iOS */
}
您可以从here 下载字体文件(在解压后的zip 的iconfont 目录中查找。
此外您还需要在字体规则之后添加以下 CSS:
.material-icons {
font-family: 'Material Icons';
font-weight: normal;
font-style: normal;
font-size: 24px; /* Preferred icon size */
display: inline-block;
line-height: 1;
text-transform: none;
letter-spacing: normal;
word-wrap: normal;
white-space: nowrap;
direction: ltr;
/* Support for all WebKit browsers. */
-webkit-font-smoothing: antialiased;
/* Support for Safari and Chrome. */
text-rendering: optimizeLegibility;
/* Support for Firefox. */
-moz-osx-font-smoothing: grayscale;
/* Support for IE. */
font-feature-settings: 'liga';
}
注意:使用材料图标字体的说明来自here,以防这些说明过时。