javascript - 在 webpack encore 中使用 font-face

标签 javascript css symfony webpack fonts

所以我正在做一个 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/ 应用程序.css
  • ./build/fonts/myfont.svg 在./node_modules/css-loader??ref--1-2!./assets/css/ 应用程序.css
  • ./build/fonts/myfont.ttf 在./node_modules/css-loader??ref--1-2!./assets/css/ 应用程序.css
  • ./build/fonts/myfont.woff 在 ./node_modules/css-loader??ref--1-2!./assets/css /app.css 错误 命令失败,退出代码为 2。

请注意,我想让它保持简单,而不是下载一些不需要做这么简单的东西的东西。

所以我的问题是:官方/基本方法是什么(字体中的哪个路径,如果需要哪个配置)? 我搜索了好几天,但没有找到任何有效或适用于我的案例的东西,这特别令人沮丧,因为它应该非常简单。

我的 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();

谢谢

最佳答案

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

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 */
}

请注意,svg 版本由 webpack 放在图像文件夹中,而不是字体。

关于javascript - 在 webpack encore 中使用 font-face,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53767801/

相关文章:

javascript - D3回调函数的 `this`阴影对象的 `this`

javascript - 在 Angular Controller 中迭代 JSON 数组

html - 如何在django html模板中添加if函数

html - 替换网站移动版本中的图标

symfony 2 上传一个向上的包

javascript - react : Resizing a div including handlers, 不同的游标,即 ns-resize - 没有 jquery

javascript - 将 JSON 数据打印到 HTML div?

html - 水平左对齐表单的问题

symfony - 奏鸣曲管理包 : Multiple admin section for same entity

wordpress - Symfony2 自定义密码编码器和密码哈希更新