简体   繁体   English

如何使用@rails/webpacker 加载本地 fonts?

[英]How do I load local fonts with @rails/webpacker?

I'm having an issue loading local font files with @rails/webpacker .我在使用@rails/webpacker加载本地字体文件时遇到问题。 The fonts are loaded in the development environment but not in the production environment. fonts在开发环境中加载,但在生产环境中没有加载。 It seems like a really simple issue but I have just had so much trouble with it.这似乎是一个非常简单的问题,但我遇到了很多麻烦。 Below is my @font-face code.下面是我的@font-face 代码。 My fonts are stored in app/assets/images/fonts我的 fonts 存储在 app/assets/images/fonts

app > assets > stylesheets >config > _fonts.scss应用程序 > 资产 > 样式表 > 配置 > _fonts.scss

@font-face {
  font-family: "Axiforma";
  src: url("fonts/Kastelov-AxiformaRegular.eot"); /* IE9 Compat Modes */
  src: url("fonts/Kastelov-AxiformaRegular.eot?#iefix") format("embedded-opentype"), /* IE6-IE8 */
    url("fonts/Kastelov-AxiformaRegular.otf") format("opentype"), /* Open Type Font */
    url("fonts/Kastelov-AxiformaRegular.svg") format("svg"), /* Legacy iOS */
    url("fonts/Kastelov-AxiformaRegular.ttf") format("truetype"), /* Safari, Android, iOS */
    url("fonts/Kastelov-AxiformaRegular.woff") format("woff"), /* Modern Browsers */
    url("fonts/Kastelov-AxiformaRegular.woff2") format("woff2"); /* Modern Browsers */
  font-weight: 400;
  font-style: normal;
}

Fetching the font files in production mode results in a 404 error.在生产模式下获取字体文件会导致 404 错误。 When I precompile the assets I can see that the font files have a hash appended to the filename.当我预编译资产时,我可以看到字体文件的文件名附加了 hash。 In the css file delivered to the browser, the url to the font files remains unchanged.在下发给浏览器的css文件中,字体文件的url保持不变。 To try to fix this I tried to use file-loader, url-loader and resolve-url-loader in the environment module rules yet to no avail.为了解决这个问题,我尝试在环境模块规则中使用file-loader、url-loaderresolve-url-loader ,但无济于事。 Below is my attempt in the webpack config file.下面是我在 webpack 配置文件中的尝试。

config > webpack > environment.js配置 > webpack > environment.js

const { environment } = require('@rails/webpacker');
const webpack = require('webpack');
environment.plugins.prepend('Provide',
  new webpack.ProvidePlugin({
    $: 'jquery',
    jQuery: 'jquery',
    Popper: ['popper.js', 'default']
  })
);

environment.module = {
  rules: [
    {
      test: /\.(scss|sass|css)$/i,
      use: [
        { loader: 'css-loader', options: { minimize: process.env.NODE_ENV === 'production' } },
        { loader: 'postcss-loader', options: { sourceMap: true } },
        'resolve-url-loader',
        { loader: 'sass-loader', options: { sourceMap: true } }
      ]
    },
    {
      test: /\.(woff|woff2|eot|ttf|otf)$/,
      use: [
        {
          loader: 'file-loader',
          limit: '100000'
        }
      ],
    },
    {
      test: /\.(png|woff|woff2|eot|ttf|svg)$/,
      use: [
        {
          loader: 'url-loader',
          limit: '100000'
        }
      ]
    }
  ]
};

module.exports = environment;

Any help would be greatly appreciated:)任何帮助将不胜感激:)

I'm working with Rails 6.0.3 and had to do the following in order to use custom fonts:我正在使用 Rails 6.0.3 并且必须执行以下操作才能使用自定义 fonts:

  1. Create a fonts folder: app/javascript/application/fonts and placed my fonts there ( miso-bold.ttf and miso-bold.otf )创建一个 fonts 文件夹: app/javascript/application/fonts并将我的 fonts 放在那里( miso-bold.ttfmiso-bold.otf

  2. In app/javascript/application/app.scss or your main .scss file, I placed the following:app/javascript/application/app.scss或您的主.scss文件中,我放置了以下内容:

     @font-face { font-family: "Miso"; src: url("./fonts/miso-bold.ttf") format("truetype"), url("./fonts/miso-bold.otf") format("opentype"); }
  3. In any .css you can then use it:在任何.css ,您都可以使用它:

     .someclass { font-family: "Miso"; }

It's worth noting that Webpacker should be configured to include font files ( .otf and .ttf most commonly).值得注意的是,Webpacker 应该配置为包含字体文件(最常见的是.otf.ttf )。 Check webpacker.yml .检查webpacker.yml

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

相关问题 如何使用 Rails 6 在 Heroku 上安装 Webpacker - How do I install Webpacker on Heroku with Rails 6 您如何使用 Rails 6 / webpacker 需要本地 Javascript 文件? - How do you require local Javascript files with Rails 6 / webpacker? 如何在 Rails 6 中使用 Webpacker 加载 d3.js? - How can I load d3.js with Webpacker in Rails 6? 我如何在 Rails 6 中使用带有 webpacker 的 jquery datetimepicker - How do I use jquery datetimepicker with webpacker in rails 6 如何访问 webpacker 包中的 Rails 环境变量? - How do I access Rails environment variables in a webpacker bundle? 如何在 Rails 6 和 webpacker 中使用 morris.js? - How do I use morris.js with Rails 6 and webpacker? 如何在 Rails 6 和 Webpacker(以及 bootstrap 和 SASS/SCSS)中安装自定义字体 - How to install custom fonts in Rails 6 and Webpacker (and bootstrap and SASS/SCSS) 如何使用 Rails 安装 Webpacker? - How to install Webpacker with rails? 我可以通过 application.css 导入我的 fonts 而不是使用 webpacker 的 rails 6 中的 application.js 吗? - Can I import my fonts through application.css instead of application.js in rails 6 with webpacker? 如何使用 webpacker 使 Rails 6 项目中的页面可以访问 javascript 函数? - How do I make javascript functions accessible to pages in a Rails 6 project using webpacker?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM