使用Webpack Encore和字体face

4
我是一名有用的助手,可以为您进行文本翻译。以下是需要翻译的内容:

我正在开发一个Symfony 4项目,由于没有选择,我正在使用Webpack Encore。 在我的CSS中,我尝试使用一个通过font-face导入的字体。

所以我有:

assets/css/app.css:

...
@font-face 
{
    font-family: "myfont";
    src: url('build/fonts/myfont.eot'); /* IE9 Compat Modes */
    src: url('build/fonts/myfont.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
         url('build/fonts/myfont.woff') format('woff'), /* Modern Browsers */
         url('build/fonts/myfont.ttf')  format('truetype'), /* Safari, Android, iOS */
         url('build/fonts/myfont.svg#myfont') format('svg'); /* Legacy iOS */
}
...

我的字体在assets / fonts /中。

我尝试了不同的路径/方法,并得到了不同的错误信息,当前的错误信息是:

找不到这些相对模块:

  • ./ build / fonts / myfont.eot 在 ./node_modules/css-loader??ref--1-2!./assets/css/ app.css
  • ./ build / fonts / myfont.svg 在 ./node_modules/css-loader??ref--1-2!./assets/css/ app.css
  • ./ build / fonts / myfont.ttf 在 ./node_modules/css-loader??ref--1-2!./assets/css/ app.css
  • ./ build / fonts / myfont.woff 在 ./node_modules/css-loader??ref--1-2!./assets/css/ app.css 错误代码为2的命令失败。

请注意,我希望保持简单,不要下载不必要的东西来完成这样简单的事情。

所以我的问题是:官方/基本方法是什么(在font-face中使用哪个路径,如果需要,使用哪个配置)? 我搜索了几天,但我找到的一切都无法工作或适用于我的情况,这特别令人沮丧,因为它应该非常容易。

我的webpack.config.js是默认的未修改的:

var Encore = require('@symfony/webpack-encore');

Encore
// directory where compiled assets will be stored
.setOutputPath('public/build/')
// public path used by the web server to access the output path
.setPublicPath('/build')
// only needed for CDN's or sub-directory deploy
//.setManifestKeyPrefix('build/')

/*
 * ENTRY CONFIG
 *
 * Add 1 entry for each "page" of your app
 * (including one that's included on every page - e.g. "app")
 *
 * Each entry will result in one JavaScript file (e.g. app.js)
 * and one CSS file (e.g. app.css) if you JavaScript imports CSS.
 */
.addEntry('app', './assets/js/app.js')
.addEntry('index', './assets/js/index.js')
//.addEntry('page2', './assets/js/page2.js')

/*
 * FEATURE CONFIG
 *
 * Enable & configure other features below. For a full
 * list of features, see:
 * https://symfony.com/doc/current/frontend.html#adding-more-features
 */
.cleanupOutputBeforeBuild()
.enableBuildNotifications()
.enableSourceMaps(!Encore.isProduction())
// enables hashed filenames (e.g. app.abc123.css)
.enableVersioning(Encore.isProduction())

// enables Sass/SCSS support
//.enableSassLoader()

// uncomment if you use TypeScript
//.enableTypeScriptLoader()

// uncomment if you're having problems with a jQuery plugin
//.autoProvidejQuery()
;

module.exports = Encore.getWebpackConfig();

谢谢

2个回答

6

我放弃了不使用插件或加载器的方式,我尝试了文件加载器:没有成功,我尝试了复制插件:成功。

webpack.config.js

var Encore = require('@symfony/webpack-encore');

var CopyWebpackPlugin = require('copy-webpack-plugin');

Encore
    ...
    .addPlugin(new CopyWebpackPlugin([
        { from: './assets/fonts', to: 'fonts' }
    ]))
    /*.addLoader({
        test: /\.(eot|svg|ttf|woff)$/,
        use: [{
            loader: 'file-loader',
            options: {
                name: '[path][name].[ext]',
                context: './assets',
            }
        }]
    })*/

    ...
;

module.exports = Encore.getWebpackConfig();

CSS:

@font-face 
{
    font-family: "myfont";
    src: url('/build/fonts/myfont.eot'); /* IE9 Compat Modes */
    src: url('/build/fonts/myfont.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
         url('/build/fonts/myfont.woff') format('woff'), /* Modern Browsers */
         url('/build/fonts/myfont.ttf')  format('truetype'), /* Safari, Android, iOS */
         url('/build/fonts/myfont.svg#myfont') format('svg'); /* Legacy iOS */
}

更新webpack encore到新版本:

在更新webpack encore后,这个答案不再适用。

现在,我可以删除CopyWebpackPlugin,而是在assets文件夹中引用字体(webpack会检测url并将它们复制到构建中,并自动更改路径)

CSS:

@font-face 
{
    font-family: "myfont";
    src: url('../fonts/myfont.eot'); /* IE9 Compat Modes */
    src: url('../fonts/myfont.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
         url('../fonts/myfont.woff') format('woff'), /* Modern Browsers */
         url('../fonts/myfont.ttf')  format('truetype'), /* Safari, Android, iOS */
         url('../fonts/myfont.svg#myfont') format('svg'); /* Legacy iOS */
}

请注意,webpack将SVG版本放在图片文件夹中,而不是字体文件夹中。

1

另一种方法是改变字体文件的保存方式。 您可以在此处阅读更多信息https://github.com/symfony/webpack-encore/issues/282

Encore
    .addLoader({
        test: /\.(woff|woff2|eot|ttf|otf)$/,
        use: [
            {
                loader: 'file-loader',
                options: {
                    outputPath: 'assets/dist/fonts/''
                }
            }
        ]
    })
    .configureFilenames({
        fonts: 'fonts/[name].[ext]'
    })

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接