如何配置Webpack构建以在公共文件夹中生成资产?

时间:2019-10-02 10:31:40

标签: webpack

当我使用webpack构建项目时,它会将所有文件发布到dist文件夹中。我希望将HTML文件和资产文件发布到公用文件夹中。如何配置webpack来实现这一目标?

webpack.config.js

const HtmlWebPackPlugin = require("html-webpack-plugin");

module.exports = {
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      },
      {
        test: /\.html$/,
        use: [
          {
            loader: "html-loader",
            options: { minimize: true }
          }
        ]
      },
      {
        test: /\.css$/,
        use: [
          'style-loader',
          'css-loader'
        ]
      },
      {
        test: /\.(png|svg|jpg|gif)$/,
        use: [
          'file-loader'
        ]
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/,
        use: [
          'file-loader'
        ]
      }
    ]
  },
  plugins: [
    new HtmlWebPackPlugin({
      template: "./src/index.html",
      filename: "./index.html"
    })
  ]
};

1 个答案:

答案 0 :(得分:0)

outputPath 可用于实现此目的。

文件加载器的配置可以更改为:

`{
  test: /\.(png|svg|jpg|gif)$/,
  loader: 'file-loader',
  options: { outputPath: '../public' },
},`

由于公用文件夹和dist文件夹处于同一级别,因此使用../ public。