Webpack JS模板字符串为背景图像生成内联CSS样式:url()

时间:2018-12-03 01:40:25

标签: javascript css webpack template-strings

我在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页,但仍然没有。

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

如何使Webpack加载这些图像?我尝试了文件加载器,URL加载器,或者我配置错误或应该使用其他工具?谢谢

Webpack配置DEV:

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"
      }
    ]
  }
};

1 个答案:

答案 0 :(得分:0)

弄清楚它是出于某种原因,即使我将其更改为.JPG并推高了这些更改,GitHub仍会记住图像的.jpg,我重新添加并重新发布了Prod中的图像,并且可以正常工作。