0

我在 JS 中有以下模板字符串:

`<div class="card-img center-auto" alt="Image of ${data.title}" class="${data.imgClass}" style="background-image: url(images/${data.imageName}.JPG)"></div>`

背景图像是一种内联样式,文件路径名data.imageName在运行时被加载。

图像在 DEV 中加载,如果我在本地打开 PROD 环境,图像就在那里,但是当我将其推送到 Github 页面时,图像是错误 404。我已经等待,因为我知道有时可能需要一些时间才能上传到 GitHub 页面但仍然没有什么。

图片文件夹在与 index.html 相同的根目录中称为“图片”

如何让 Webpack 加载这些图像?我已经尝试过文件加载器、url-loader 并且我配置错误或者应该使用其他东西?谢谢

Webpack 配置开发:

const path = require("path");

module.exports = {
  mode: "development",
  entry: {
    app: "./src/scripts/script.js"
  },
  devtool: "eval-source-map",
  devServer: {
    contentBase: "./dev"
  },
  output: {
    filename: "[name].bundle.js",
    path: path.resolve(__dirname, "dev")
  },
  module: {
    rules: [
      {
        test: /\.(png|jpg|gif)$/i,
        use: [
          {
            loader: "url-loader",
            options: {
              limit: 8192
            }
          }
        ]
      },
      {
        test: /\.(png|jpe?g|gif|svg)$/,
        use: [
          "file-loader",
          {
            loader: "image-webpack-loader",
            options: {
              mozjpeg: {
                progressive: true,
                quality: 65
              },
              // optipng.enabled: false will disable optipng
              optipng: {
                enabled: false
              },
              pngquant: {
                quality: "65-90",
                speed: 4
              },
              gifsicle: {
                interlaced: false
              }
            }
          }
        ]
      },
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"]
      }
    ]
  }
};

Webpack 产品配置:

const webpack = require("webpack");
const path = require("path");
const UglifyJSPlugin = require("uglifyjs-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");

module.exports = {
  mode: "production",
  entry: {
    app: "./src/scripts/script.js"
  },
  devtool: "source-map",
  plugins: [
    new UglifyJSPlugin({
      cache: true,
      sourceMap: true
    }),
    new OptimizeCSSAssetsPlugin({}),
    new webpack.DefinePlugin({
      "process.env.NODE_ENV": JSON.stringify("production")
    }),
    new MiniCssExtractPlugin({
      // Options similar to the same options in webpackOptions.output
      // both options are optional
      filename: "[name].css",
      chunkFilename: "[id].css"
    })
  ],
  output: {
    filename: "[name].bundle.js",
    path: path.resolve(__dirname, "docs")
  },
  module: {
    rules: [
      {
        test: /\.(html)$/,
        use: {
          loader: "html-loader",
          options: {
            attrs: [":data-src", "img:src"],
            minimize: true
          }
        }
      },
      {
        test: /\.css$/,
        use: [MiniCssExtractPlugin.loader, "css-loader"]
      },
      {
        test: /\.svg$/,
        loader: "svg-inline-loader"
      }
    ]
  }
};
4

1 回答 1

0

由于某种原因,GitHub 会记住 .jpg 的图像,即使我已将其更改为 .JPG 并推动更改,我重新添加并重新发布了 Prod 中的图像并且它有效。

于 2018-12-04T15:05:20.333 回答