简体   繁体   English

使用汇总解决woff导入

[英]Resolve woff imports with Rollup

I am attempting to bundle a theme package using Rollup.js. 我正在尝试使用Rollup.js捆绑主题包。 The theme includes some global styles, most relevant @font-face. 主题包括一些全局样式,最相关的@ font-face。 I am importing my fonts and with the intent to inject them via styled-components injectGlobal. 我正在导入我的字体,并打算通过样式组件injectGlobal注入它们。

When I try to bundle the package Rollup is chocking on the font files. 当我尝试捆绑软件包时,Rollup阻塞了字体文件。 My assumption was that Webpack and Rollup could be used pretty much interchangeably is this not the case? 我的假设是Webpack和Rollup可以互换使用,不是吗?

What is the correct way to do something like this? 做这样的事情的正确方法是什么?

Error in console : 控制台错误

{ 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 : 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;

After exhaustive Google searches I could not find a way to get Rollup to pull in the font files without crashing. 经过详尽的Google搜索之后,我无法找到一种方法来让Rollup提取字体文件而不会崩溃。

I moved my imports to requires that get called when the export is executed and that solved my problem. 我将导入移动到要求在执行导出时调用并且解决了我的问题。

Update file : 更新文件

const injectGlobalStyles = () => {

  const NeueHassGroteskDisplayBold = require('../fonts/NeueHaasGroteskDisplayBold.woff');
  const NeueHassGroteskDisplay = require('../fonts/NeueHaasGroteskDisplay.woff');
  const NeueHassGroteskText = require('../fonts/NeueHaasGroteskText.woff');
  const NeueHassGroteskTextBold = require('../fonts/NeueHaasGroteskTextBold.woff');

  return `
    * {
      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;

Another way would be to bundle the font files as base64 strings using rollup-plugin-url : 另一种方法是使用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,
    }),
  ],
  // ...
}

and then import them as usual: 然后像往常一样导入它们:

// 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;
  }
`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM