首页 > 解决方案 > 在 webpack 构建期间未将 Favicon 添加到构建文件夹

问题描述

我正在尝试将图像放入我的 dist 文件夹,但它没有显示出来。

我已经包含了我认为可以使它工作的 copy-webpack-plugin。我在构建过程中确实收到一条警告消息,上面写着:'警告无法在'/Users/developer/Desktop/RecruitsPro/client/public'中找到'/Users/developer/Desktop/RecruitsPro/client/public'

webpack.config.js

var webpack = require('webpack');
var path = require('path');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var CopyWebpackPlugin = require('copy-webpack-plugin');
const helpers = require('./helpers');

const VENDOR_LIBS = [
  'axios',
  'ordinal',
  'react',
  'react-activity',
  'react-avatar',
  'react-dom',
  'react-easy-chart',
  'react-is',
  'react-modal',
  'react-redux',
  'react-router-dom',
  'react-stripe-checkout',
  'react-window-size',
  'redux',
  'redux-form',
  'redux-thunk'
];

module.exports = {
  entry: {
    bundle: './client/app/index.js',
    vendor: VENDOR_LIBS
  },
  output: {
    path: path.join(__dirname, 'dist'),
    filename: '[name].[chunkhash].js'
  },
  module: {
    rules: [
      {
        use: 'babel-loader',
        test: /\.js$/,
        exclude: /node_modules/
      },
      {
        use: ['style-loader', 'css-loader'],
        test: /\.css$/
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './client/public/index.html'
    }),
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
    }),
    new CopyWebpackPlugin([{
      from: helpers.root('client/public')
    }])
  ]
};

helpers.js

const path = require('path');

// Helper functions
function root(args) {
  args = Array.prototype.slice.call(arguments, 0);
  return path.join.apply(path, [__dirname].concat('../', ...args));
}

exports.root = root;

我期待这张照片会出现。

标签: reactjswebpackwebpack-4

解决方案


对于任何好奇的人,我必须将此代码添加到我的 module.rules 数组中。

  {
    test: /\.png$/,
    loader: 'file-loader'
  }

推荐阅读