07_03_加载字体(Webpack Book)

Loading Fonts(加载字体)

Loading fonts is similar to loading images. It does come with unique challenges, though. How to know what font formats to support? There can be up to four font formats to worry about if you want to provide first class support to each browser.<br/>
加载字体与加载图片相似。可是它也伴随着独特的挑战。如何知道什么字体是被支持的?若是你想对每种浏览器都提供一流的支持,那至少须要考虑四种字体格式。javascript

The problem can be solved by deciding a set of browsers and platforms that should receive first class service. The rest can use system fonts.<br/>
决定对哪些浏览器和平台须要提供一流的服务就可以解决这个问题。其余浏览器和平台就使用系统字体。css

You can approach the problem in several ways through webpack. You can still use url-loader and file-loader as with images. Font test patterns tend to be more complicated, though, and you have to worry about font file related lookups.<br/>
你能够经过Webpack用不一样的方式解决这个问题。像处理图片同样,继续使用 url-loaderfile-loader, 可是字体的 test 会变得更复杂一些,而且须要考虑字体文件相关的查询。java

T> canifont helps you to figure out which font formats you should support. It accepts a .browserslistrc definition and then checks font support of each browser based on the definition.<br/>
T> canifont 赞助你解决应该支持哪一种字体格式。 它接收一个 .browserslistrc 定义,而后基于这个定义检查每一个浏览器对字体的支持。webpack

Choosing One Format(选择一个格式)

If you exclude Opera Mini, all browsers support the .woff format. Its newer version, .woff2, is widely supported by modern browsers and can be a good alternative.

若是你排除了Opera Mini, 全部的浏览器在支持 .woff 格式. 它的新版本 .woff2 被现代浏览器普遍支持,也是一个不错的选择。git

{pagebreak}github

Going with one format, you can use a similar setup as for images and rely on both file-loader and url-loader while using the limit option:

选定一种格式,你可使用和图片相似的配置并在使用limit选项时依赖于 file-loaderurl-loaderweb

{
  test: /\.woff$/,
  use: {
    loader: "url-loader",
    options: {
      limit: 50000,
    },
  },
},

A more elaborate approach to achieve a similar result that includes .woff2 and others would be to end up with the code as below:<br/>
要想用精妙地方法来得到相似结果来包含 .woff2 和其余配置,下面的代码给出了解决方法:npm

{
  // Match woff2 in addition to patterns like .woff?v=1.1.1.
  test: /\.(woff|woff2)(\?v=\d+\.\d+\.\d+)?$/,
  use: {
    loader: "url-loader",
    options: {
      // Limit at 50k. Above that it emits separate files
      limit: 50000,

      // url-loader sets mimetype if it's passed.
      // Without this it derives it from the file extension
      mimetype: "application/font-woff",

      // Output below fonts directory
      name: "./fonts/[name].[ext]",
    }
  },
},

{pagebreak}api

Supporting Multiple Formats(支持多种类型)

In case you want to make sure the site looks good on a maximum amount of browsers, you can use file-loader and forget about inlining. Again, it's a trade-off as you get extra requests, but perhaps it's the right move. Here you could end up with a loader configuration:<br/>
为了确保在最大数量的浏览器中让站点更好地显示,可使用 file-loader 而不考虑使用内联嵌入。重申一次,这会致使增长额外的请求,但或许这是正确之举。下面以一个加载器的配置代码结束:浏览器

{
  test: /\.(ttf|eot|woff|woff2)$/,
  use: {
    loader: "file-loader",
    options: {
      name: "fonts/[name].[ext]",
    },
  },
},

The way you write your CSS definition matters. To make sure you are getting the benefit from the newer formats, they should become first in the definition. This way the browser picks them up.<br/>
编写CSS定义的方式很重要。为确保能重新的格式中获得好处,它们应该放在定义的最开始。这样浏览器就能够加载他们了。

@font-face {
  font-family: "myfontfamily";
  src: url("./fonts/myfontfile.woff2") format("woff2"),
    url("./fonts/myfontfile.woff") format("woff"),
    url("./fonts/myfontfile.eot") format("embedded-opentype"),
    url("./fonts/myfontfile.ttf") format("truetype");
    /* Add other formats as you see fit */
}

T> MDN discusses the font-family rule in detail.
详情参见:MDN discusses the font-family rule

{pagebreak}

Manipulating file-loader Output Path and publicPath(控制 file-loader 的输出路径和 publicPath)

As discussed above and in webpack issue tracker, file-loader allows shaping the output. This way you can output your fonts below fonts/, images below images/, and so on over using the root.

如上面所讨论的以及webpack issue tracker, file-loader 容许设置输出。经过这种方式,您能够在“字体/”下面输出字体,在“图像/”下面输出图像,等等。

Furthermore, it's possible to manipulate publicPath and override the default per loader definition. The following example illustrates these techniques together:
更进一步,它能够操纵 publicPath 并覆盖每一个加载器的默认值。下面的例子一块儿说明了这些技术:

{
  // Match woff2 and patterns like .woff?v=1.1.1.
  test: /\.woff2?(\?v=\d+\.\d+\.\d+)?$/,
  use: {
    loader: "url-loader",
    options: {
      limit: 50000,
      mimetype: "application/font-woff",
      name: "./fonts/[name].[ext]", // Output below ./fonts
      publicPath: "../", // Take the directory into account
    },
  },
},

Generating Font Files Based on SVGs(基于SVG文件生成字体文件)

If you prefer to use SVG based fonts, they can be bundled as a single font file by using webfonts-loader.<br/>
若是你喜欢使用基于SVG的字体,它们能够用 webfonts-loader 打包成一个字体文件.

W> Take care with SVG images if you have SVG specific image setup in place already. If you want to process font SVGs differently, set their definitions carefully. The Loader Definitions chapter covers alternatives.

若是已经设置了SVG图片,对这些SVG图片须要倍加当心。若是想用不一样的方式处理字体SVG文件,须要当心设置他们的定义。 Loader Definitions 一节包含了替代方案。

Using Google Fonts(使用谷歌字体)

google-fonts-webpack-plugin can download Google Fonts to webpack build directory or connect to them using a CDN.

google-fonts-webpack-plugin 可以下载Google字体到Webpack的构建目录或者将他们链接到一个CDN。

Using Icon Fonts(使用图标字体)

iconfont-webpack-plugin was designed to simplify loading icon based fonts. It inlines SVG references within CSS files.
iconfont-webpack-plugin 被设计用来简化加载基于图片的字体的。它在CSS文件中内联SVG引用。

Conclusion(总结)

Loading fonts is similar to loading other assets. You have to consider the browsers you want to support and choose the loading strategy based on that.

加载字体与加载其余资源相似。你不得不考虑想要支持的浏览器并基于此选择加载策略。

To recap(概要):

  • When loading fonts, the same techniques as for images apply. You can choose to inline small fonts while bigger ones are served as separate assets.
  • 加载字体与加载图片使用相同的技术。你能够选择内联小的字体文件,并将大的字体文件生成独立资源。
  • If you decide to provide first class support to only modern browsers, you can select only a font format or two and let the older browsers to use system level fonts.
  • 若是决定只对现代浏览器进行一流的支持,你能够只选择一到两种字体格式,并让传统浏览器使用系统级字体。

In the next chapter, you'll learn to load JavaScript using Babel and webpack. Webpack loads JavaScript by default, but there's more to the topic as you have to consider what browsers you want to support.

相关文章
相关标签/搜索