javascript - 使用 Rollup 解决 woff 导入问题

标签 javascript css node.js styled-components rollup

我正在尝试使用 Rollup.js 捆绑一个主题包。该主题包括一些全局样式,最相关的@font-face。我正在导入我的字体,并打算通过 styled-components injectGlobal 注入(inject)它们。

当我尝试 bundle 时,Rollup 塞满了字体文件。我的假设是 Webpack 和 Rollup 可以互换使用,不是吗?

做这样的事情的正确方法是什么?

控制台错误:

{ SyntaxError: /Users/****/sites/vz-react/packages/Theme/fonts/NeueHaasGroteskDisplayBold.woff: Unexpected character '' (1:4)
> 1 | wOFF:�� 6���OS/2lZ`�  VDMX�w�mt�cmap@\��(cvt �`�
    |     ^
  2 |
       �fpgm��
  3 | �c��gasp
              � glyf
                    �I����rheadV�66�!�vhheaV�!$��hmtxW;*+kernZ$;��ĭloca'��p�\maxp+�  �[name+�   �y*/post4� ��prep5�7ڀɄx�c`f�b������������
  4 | ������9X�@����a����x��3800�fbf�/�p�y9#��������N3(!R��x��eT���g�f`���uƌ�3f������������`���H(ݠ���w���=�w�O���?\��dd�G�Nf2�,d�od%�t�ž��l2;��
...

globalStyles.js:

import NeueHassGroteskDisplayBold from '../fonts/NeueHaasGroteskDisplayBold.woff';
import NeueHassGroteskDisplay from '../fonts/NeueHaasGroteskDisplay.woff';
import NeueHassGroteskText from '../fonts/NeueHaasGroteskText.woff';
import NeueHassGroteskTextBold from '../fonts/NeueHaasGroteskTextBold.woff';

const injectGlobalStyles = () => `
  * {
    box-sizing: border-box;
  }

  *:focus {
    outline: #000 dotted 1px;
    outline-offset: 1px;
  }

  body {
    padding: 0;
    margin: 0;
  }

  @font-face {
    font-family: 'NHGDisplay';
    src: url(${NeueHassGroteskDisplayBold}) format("woff");
    font-weight: bold;
    font-style: normal;
  }

  @font-face {
    font-family: 'NHGDisplay';
    src: url(${NeueHassGroteskDisplay}) format("woff");
    font-weight: normal;
    font-style: normal;
  }

  @font-face {
    font-family: 'NHGText';
    src: url(${NeueHassGroteskText}) format("woff");
    font-weight: normal;
    font-style: normal;
  }

  @font-face {
    font-family: 'NHGText';
    src: url(${NeueHassGroteskTextBold}) format("woff");
    font-weight: bold;
    font-style: normal;
  }
`;

export default injectGlobalStyles;

最佳答案

另一种方法是使用 rollup-plugin-url 将字体文件捆绑为 base64 字符串。 :

// rollup.config.js

import url from 'rollup-plugin-url'

export default {
  // ...
  plugins: [
    // ...
    url({
      // by default, rollup-plugin-url will not handle font files
      include: ['**/*.woff', '**/*.woff2'],
      // setting infinite limit will ensure that the files 
      // are always bundled with the code, not copied to /dist
      limit: Infinity,
    }),
  ],
  // ...
}

然后照常导入它们:

// some-file.js

import { createGlobalStyle } from 'styled-components'
import MyFontWoff from '../fonts/my-font.woff'

const GlobalStyle = createGlobalStyle`
  @font-face {
    font-family: 'MyFont';
    src: url(${MyFontWoff}) format('woff');
    font-weight: normal;
    font-style: normal;
  }
`

关于javascript - 使用 Rollup 解决 woff 导入问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51970359/

相关文章:

javascript - 如何在循环中不断查找数组中的最大值并不断增加最大值确定的次数?

jquery - 当标签具有 "required"属性时,如何使用 css 在特定标签的父标签上设置字体粗细?

javascript - 如何解决 TypeError : d3. 时间未定义?

javascript - Node Express 4 在多次 API 调用后发送响应

javascript - jquery tab ajax问题

c# - 客户端不显眼的验证

javascript - 如何将此mysql select return转换为一个数组

html - 使用 flex 截断文本溢出

javascript - 使四个 div 的网格展开以匹配最高的高度

node.js - 在 package.json 中隐藏私有(private)存储库的 token