【问题标题】:Webpack collecting wrong src pathWebpack 收集错误的 src 路径
【发布时间】:2021-10-27 18:47:34
【问题描述】:

我认为整个问题在于配置webpack,构建时成功将图像收集在文件夹中,但是在导入时......

在某处我找到了一个指示公共路径的解决方案,但不知何故它并没有一起成长

webpack.config.js

const path = require('path');
// const webpack = require('webpack');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
// const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');

module.exports = {
  entry: { main: './src/lib/index.js' },
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'index.js',
    libraryTarget: "umd",
    library: "@compassplus/ui-mobicash"
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      },
      {
        test: /\.css$/,
        use:  [  
            MiniCssExtractPlugin.loader, 
            {
                loader: 'css-loader',
                options: {
                  url: true,
                  importLoaders: 1,
                  modules: true,
                  localIdentName: '[name]__[local]__[hash:base64:5]'
                }
            },
        ],
        include: /\.module\.css$/,
      },
      {
        test: /\.css$/,
        use: [
            MiniCssExtractPlugin.loader, 
            'css-loader'
        ],
        exclude: /\.module\.css$/,
      },
      {
        test: /\.(png|jp(e*)g|svg|gif)$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              name: '[hash:12].[ext]',
              outputPath: 'images/',
              esModule: false,
            },
          },
        ],
      },
    ]
  },
  plugins: [ 
    new CleanWebpackPlugin(),
    new MiniCssExtractPlugin({
        filename: 'index.css',
    }),
    // new HtmlWebpackPlugin({
    //     template: './public/index.html',
    // }),
    // new webpack.ProvidePlugin({
    //     "React": "react",
    //   }),
  ],
  externals: {
    react: 'react',
  },
  resolve: {
    extensions: ['.js', '.jsx'],
  },
}

Путь указанный в src / src 中的路径:

src="images/809853c38dec.svg"

在 React 组件中,我通过 import 连接它并将其作为对象传递

import imgLight from './img/theme-light.svg';
<img src={img} alt='Картинка' className={style.img}></img>

【问题讨论】:

    标签: reactjs webpack


    【解决方案1】:

    使用 url-loader 解决了问题:

    {
        loader: "url-loader",
        options: {
            limit: 8192,
            name: "static/media/[name].[hash:8].[ext]"
         },
     }
    

    【讨论】:

    • 能否请您让您的答案更具有解释性?问题的原因是什么,您在哪里插入了 sn-p,为什么它可以解决问题?
    • 哦,当然。我正在创建一个 UI 组件库,并且面临重用组件中使用 url() 或 src 的组件的问题。
    • 默认情况下,React 将所有图像放在 /static/media 路径中,在我的配置中,我使用文件加载器,它将所有组件放在路径中 (/images/[name].[ext ]),以及相应的 url(/images/[name].[ext])
    • 并且在 React 中它应该是 (/static/media/[name].[hash].[ext])。这就是整个错误出现的地方。
    • 解决这个问题的方法是将下载器文件替换为下载器url。我个人无法详细说明 url-loader。它像文件加载器一样工作,但它返回一个 DataURL,这有助于我避免 /static/media 出现问题。
    猜你喜欢
    • 2018-09-10
    • 1970-01-01
    • 2016-09-02
    • 1970-01-01
    • 2022-01-03
    • 2016-01-19
    • 2020-08-17
    • 1970-01-01
    相关资源
    最近更新 更多