【问题标题】:Output fonts, css files and other assets in a subdirectory of the dist folder在 dist 文件夹的子目录中输出字体、css 文件和其他资源
【发布时间】:2021-04-20 13:51:28
【问题描述】:

我正在关注官方 webpack 网站上的指南。可复现的代码见官网this section

在提供的示例中,所有字体都在 dist 文件夹下输出。我想要的是在字体子目录下输出字体。类似地,对于其他类型的资产,例如 css、图像等。我希望它们根据其扩展名输出到文件夹下。

输出如下所示:

-/dist
--index.html
--bundle.js
--/css
---style1.css
--/fonts
---font1.woff
---font2.ttf

【问题讨论】:

    标签: javascript css webpack fonts


    【解决方案1】:

    您可以使用file-loader 来实现:

      {
        test: /\.(png|svg|jpe?g|gif)$/,
        include: /images/,
        use: [
          {
            loader: 'file-loader',
            options: {
              name: '[name].[ext]',
              outputPath: 'images/',
              publicPath: 'images/'
            }
          }
        ]
      },
      {
        test: /\.(woff(2)?|ttf|otf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
        include: /fonts/,
        use: [
          {
            loader: 'file-loader',
            options: {
              name: '[name].[ext]',
              outputPath: 'fonts/',
              publicPath: 'fonts/'
            }
          }
        ]
      },
    

    【讨论】:

    • 太好了,成功了。 include 属性有什么用?不知何故,没有它它也能工作。
    • 你可以使用includeexclude来过滤路径例如
    猜你喜欢
    • 2018-03-17
    • 1970-01-01
    • 1970-01-01
    • 2013-11-26
    • 2014-12-27
    • 2023-01-11
    • 1970-01-01
    • 2016-11-11
    • 1970-01-01
    相关资源
    最近更新 更多