اذهب إلى المحتوى
  • 0

تعذر تحميل صورة الخلفية من ملف Css في Webpack

محمد لارافيل

السؤال

أحاول وضع صورة كخلفية في div لكنها لا تتعرف عليها لأن حزمة الويب تغير المسار إلى http://localhost:8080/gaia.png بدلاً من http://localhost:8080/assets/images/gaia.png يتم عرض الصورة بشكل صحيح داخل img ولكن لا يتم تحميلها إذا كنت أرغب في استخدامها من ملف Css. أنا متأكد من أن المشكلة تكمن في مسار الملف ولكني لا أعرف كيفية إصلاحها.
JYvWD.thumb.png.99df70a132e5d6c8b8447ded1d6e62ac.png
bfyMP.png.402a7f29184bb5c6e8d61060b42ddbc1.png
index.js
 

import './assets/style/style.css'
import './assets/images/gaia.png'
import './assets/fonts/CascadiaCode.ttf'

const fun = () => {
  console.log('hey')
}
fun();

webpack.config.js

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

module.exports = {
  mode: 'development',
  devtool: 'inline-source-map',
  entry: './src/index.js',
  output: {
    filename: 'main.js',
    path: path.resolve(__dirname, 'dist'),
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, 'src', 'index.html')
    })
  ],
  module: {
    rules: [
      {
        test: /\.css$/i,
        use: ['style-loader', 'css-loader'],
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: ["babel-loader"]
      },
      {
        test: /\.(png|svg|jpg|jpeg|gif)$/i,
        type: 'asset/resource',
        generator: {
          filename: '[name][ext][query]',
          outputPath: 'assets/images/',
        },
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/i,
        type: 'asset/resource',
        generator: {
          filename: '[name][ext][query]',
          outputPath: 'assets/fonts/',
        },
      },
    ],
  },
};

style.css

.container {
  width: 300px;
  height: 300px;
  background-image: url("../images/gaia.png");
  background-size: auto;
}

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

أهلا بك يا محمد.

المشكلة تكمن في ضبط إعدادات الـ file-loader أو url-loader.

يتم استخدام أداة تحميل الملفات أو url-loader للتعامل مع ملفات الأصول مثل الصور والخطوط وملفات الوسائط الأخرى. عندما يصادف webpack ملف أصل ، فإنه يمرره إلى أداة تحميل الملفات أو url-loader ، مما ينشئ ملفًا جديدًا باسم فريد ويضعه في دليل الإخراج. إذا قمت بتحديد خيار publicPath غير صحيح ، فقد لا يتم تحميل الصور بشكل صحيح. تحتاج إلى التأكد من أن خيار publicPath يشير إلى الدليل الصحيح الذي يتم تخزين الصور فيه. على سبيل المثال ، إذا تم تخزين صورك في دليل "assets/images" ، يجب أن تبدو إعدادات webpack على النحو التالي:

module.exports = {
  // ... other config options
  output: {
    // ...
    publicPath: "/assets/images/",
  },
  module: {
    rules: [
      {
        test: /\.(png|jpg|gif|svg)$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              name: '[name].[ext]',
              outputPath: 'assets/images/'
            }
          }
        ]
      }
    ]
  }
};

أي أنه يجب إضافة خيار publicPath ويتم ضبطه للإشارة إلى مسار الصور في ملف المشروع الخاص بك، وتأكد من استخدام absolute path '/' وليس relative path '/.'

output: {
    // ...
    publicPath: "/assets/images/",
  }

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...