【发布时间】:2019-07-26 09:51:24
【问题描述】:
我的 dist 文件夹包含我的 index.html、contact.html、index.js、contact.js 以及我的 css 提到的两个图像作为背景图像。
例如
background-image: url("../images/prairie.jpg");
这就是我的 dist 文件夹中的所有内容。未加载所有其他图像(我的 css 未提及)。这些图像仅在 .js 文件中提及(例如 slideshow.js)。这个“slideshow.js”文件是幻灯片,它不显示任何图像,但显示丢失的图像图标。显示了前面提到的背景图像“../images/prairies.jpg”。 css 文件引用的所有照片都加载到 dist 文件夹中。所有仅由 .js 文件而不是 css 文件提及的照片都不会加载到 dist 文件夹中。
这是我的 webpack.config.js
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackTemplate = require('html-webpack-template');
const path = require('path');
const config = {
mode: 'development', // set mode option, 'development' or 'production'
entry: {
index: './src/index.js',
contact:'./src/contact.js'
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: "[name].js",
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
use: 'babel-loader',
exclude: /node_modules/,
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},
{
test: /\.(gif|png|jpe?g|svg)$/i,
use: [
'file-loader',
{
loader: 'image-webpack-loader',
options: {
bypassOnDebug: true, // webpack@1.x
disable: true, // webpack@2.x and newer
},
},
],
}
],
},
resolve: {
extensions: ['.js', '.jsx'],
},
plugins: [
new HtmlWebpackPlugin({
template: './index.html',
inject: true,
chunks: ['index'],
filename: './index.html'
}),
new HtmlWebpackPlugin({
template: './contact.html',
inject: true,
chunks: ['contact'],
filename: './contact.html'
})
]
};
module.exports = config;
【问题讨论】:
-
你是如何在 JS 中引用图片的?
-
我使用的是相对路径和 jsx,例如:
-
是的,这行不通。对于 js,您需要
require图片.. 此信息可能对您有所帮助 -> medium.com/@godban/… -
@Keith 你是个天才。我基本上必须将我的图像路径的所有字符串从“images/map.png”之类的东西更改为 require('images/map.png')。还有一件事,为了避免使用类似“39fjksjalef9.jpg”的名称,您知道如何在 jpg 名称中包含原始图像名称吗?出于 SEO 目的?
-
你可以告诉 webpack 使用什么文件名。也许试试这个 -> decembersoft.com/posts/…
标签: javascript css reactjs webpack