簡體   English   中英

webpack - 無法解析圖像 src 路徑

[英]webpack - not able to resolve image src path

這是我的項目目錄:

src
 -components
   -Header
     -index.jsx
webpack
 -webpack.base.js
 -webpack.dev.js

在我的App.jsx中,

return (
  <img src="@/assets/images/components/header/logo-header.png" alt="logo" class="header__icon"/>
)

運行npm start后,圖像未顯示。

來自督察在此處輸入圖像描述

在此處輸入圖像描述

package.json

  "scripts": {
    "start": "webpack-dev-server --config ./webpack/webpack.dev.js"
  },

實用程序.js

const path = require('path')

module.exports = {
  resolve: function(dir) {
    return path.join(__dirname, '..', dir)
  }
}

webpack.base.js

const webpack = require("webpack");

const utils = require('./utils')

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

module.exports = {
  entry: "./src/index.jsx",
  resolve: {
    alias: {
      '@': utils.resolve('src'),
    },
    extensions: ["*", ".js", ".jsx"]
  },
  module: {
    rules: [...],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "public/index.html",
      filename: "index.html"
    })
  ],
};

webpack.dev.js

const { merge } = require("webpack-merge");
const base = require("./webpack.base");

const Dotenv = require("dotenv-webpack");

module.exports = merge(base, {
  mode: "development",
  devtool: "inline-source-map",
  output: {
    path: '/',
  },
  devServer: {
    port: 3000,
    static: true
  }
});

我試圖在 webpack 中添加file-loader ,但仍然無法正常工作。

      {
        test: /\.(png|jpe?g|gif)$/i,
        use: [
          {
            loader: 'file-loader',
          },
        ],
      },

您可以使用Asset Management#Loading Images 您可以根據自己的文件目錄結構調整文件路徑。

例如

目錄結構:

⚡  tree -L 4 -I 'node_modules'
.
├── package-lock.json
├── package.json
├── src
│   ├── assets
│   │   └── images
│   │       └── logo.png
│   ├── index.html
│   └── index.jsx
└── webpack.config.js

3 directories, 6 files

src/index.jsx

import React from "react";
import ReactDOM from "react-dom";

function App() {
  return (
    <div>
      <img src={require("@/assets/images/logo.png")} alt="logo" />
    </div>
  );
}

ReactDOM.render(<App />, document.getElementById("app"));

webpack.config.js

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");

module.exports = {
  entry: "./src/index.jsx",
  output: {
    path: path.resolve(__dirname, "./dist"),
  },
  mode: "development",
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "./src"),
    },
    extensions: ["*", ".js", ".jsx"],
  },
  module: {
    rules: [
      {
        test: /\.(jsx|js)$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
          options: {
            presets: ["@babel/preset-env", "@babel/preset-react"],
          },
        },
      },
      {
        test: /\.(png|svg|jpg|jpeg|gif)$/i,
        type: "asset/resource",
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, "./src/index.html"),
    }),
  ],
  devServer: {
    static: {
      directory: path.join(__dirname, "dist"),
    },
    port: 9000,
  },
};

package.json

{
  "name": "70107481",
  "scripts": {
    "build": "webpack",
    "start": "webpack serve"
  },
  "devDependencies": {
    "@babel/core": "^7.16.0",
    "@babel/preset-env": "^7.16.4",
    "@babel/preset-react": "^7.16.0",
    "babel-loader": "^8.2.3",
    "html-webpack-plugin": "^5.5.0",
    "webpack": "^5.64.3",
    "webpack-cli": "^4.9.1",
    "webpack-dev-server": "^4.5.0"
  },
  "dependencies": {
    "react": "^17.0.2",
    "react-dom": "^17.0.2"
  }
}

通過npm start webpack 開發服務器。 結果:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM