在Webpack中设置文件加载器会在构建期间产生错误

时间:2018-09-19 12:13:12

标签: javascript reactjs webpack babel webpack-file-loader

这是我的webpack.config.babel.js的样子:

import path from 'path'
import MiniCssExtractPlugin from 'mini-css-extract-plugin'

const out = path.resolve(__dirname, './dist')
const client = path.resolve(__dirname, './src/client')

export default {
  entry: {
    main: `${client}/index`,
    plugins: `${client}/plugins`
  },
  output: {
    path: `${out}`,
    filename: 'js/[name].js',
    chunkFilename: 'js/[name].js'
  },
  module: {
    rules: [
      {
        test: /\.jsx?/,
        loader: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.scss$/,
        use: ['style-loader', MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader', 'sass-loader']
      },
      {
        test: /\.(png|svg|jpg|gif)$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              name: 'assets/img/[name].[ext]'
            }
          }
        ]
      }
    ]
  },
  optimization: {
    runtimeChunk: {
      name: 'manifest'
    },
    splitChunks: {
      cacheGroups: {
        vendor: {
          test: /[\\/]node_modules[\\/]/,
          name: 'vendors',
          priority: -20,
          chunks: 'all'
        }
      }
    }
  },
  target: 'web',
  cache: true,
  plugins: [
    new MiniCssExtractPlugin({
      filename: 'css/[name].css'
    })
  ]
}

因此您可以看到我正在使用png,svg,gif和jpg的文件加载器。 然后,我像这样导入图像:

import img404 from '../../assets/img/404-error.svg'

但我不断收到此错误: enter image description here

我不太确定自己在做什么错了:( 仅供参考:这实际上按预期将图像保存在dist/assets/img/THE_ACTUAL_IMAGE_FILE

0 个答案:

没有答案