所以这是我将要使用Webpack的第一个项目,到目前为止,我已经通过对文档中的代码进行故障排除和复制来扩展了这一范围 在我项目的英雄部分,图像是通过JavaScript动态添加的。 这是代码
// Import multiple images
function importAll(r) {
let images = {};
r.keys().map(item => { images[item.replace('./', '')] = r(item) })
return images;
}
const images = importAll(require.context("./img/hero", true, /\.(png|jpe?g|svg)$/));
它放置在index.html中,因此在hero-js文件中是必需的,
const images = require('./index');
并使用以下代码实现:
// Set dynamic background
function setBg() {
console.log(' I am Working')
let today = new Date()
// let today = new Date(),
hour = today.getHours();
// Randomize bg
// morning
let morningArray = ['sunset-view-of-mountains-733100.jpg', 'purple-petal-flower-surrounded-by-green-plants-during-66288.jpg', 'time-lapse-photography-of-waterfalls-during-sunset-210186.jpg', 'two-cargo-ships-sailing-near-city-2144905.jpg'];
let morningBg = morningArray[Math.floor(Math.random() * morningArray.length)];
let morningPath = '../src/img/hero/';
let morning = morningPath + morningBg
console.log(morning)
// afternoon
let afternoonArray = ['empty-dining-tables-and-chairs-1579739.jpg', 'brown-and-green-mountain-view-photo-842711.jpg', 'photo-of-keyboard-near-mouse-3184460.jpg', 'america-arid-bushes-california-221148.jpg'];
let afternoonBg = afternoonArray[Math.floor(Math.random() * afternoonArray.length)];
let afternoonPath = '../src/img/hero/';
let afternoon = afternoonPath + afternoonBg;
// evening
let eveningArray = ['twisted-building-during-nighttime-1470405 s.jpg', 'beautiful-beauty-blue-bright-414612.jpg', 'landscape-photo-of-mountain-with-polar-lights-1434608.jpg', 'photo-of-toronto-cityscape-at-night-2478248.jpg'];
let eveningBg = eveningArray[Math.floor(Math.random() * eveningArray.length)];
let eveningPath = './src/img/hero/';
let evening = eveningPath + eveningBg;
// Show bg
//morning
if (hour < 12) {
hero.style.background = `url(${morning}) no-repeat center center / cover`;
greetingJs.textContent = 'Hi Good Morning,'
// afternoon
} else if (hour < 18) {
hero.style.background = `url(${afternoon}) no-repeat center center / cover`;
greetingJs.textContent = 'Hi Good Afternoon,'
}
// evening and night
else {
hero.style.background = `url(${evening}) no-repeat center center / cover`;
greetingJs.textContent = 'Hi Good Evening,'
}
}
在Netlify上,这些图像不会出现,只有后备的bg颜色可以节省一天的时间。
无休止的故障排除后,我意识到chrome开发工具的网络标签中发生了一些有趣的事情:
该图片似乎是用错误的格式请求的,并且指向与nav.js无关的图片(其代码在index.js和dynamicHero.js中)。 另外:
该错误似乎是由nav.js文件中的一行引起的,否则该文件可以正常工作。 另外,我注意到有时甚至不要求图像,因此未检测到错误。 我也对netlify构建设置进行了一些更改(然后它开始要求它) 最初,基本目录为空 而发布目录只是dist。
//require plugins
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
// const MiniCssExtractPlugin = require('mini-css-extract-plugin');
// const { CleanWebpackPlugin } = require('clean-webpack-plugin');
// modules
module.exports = {
mode: 'development',
entry: {
app: './src/index.js',
},
devServer: {
},
module: {
rules: [
// babel
{
test: /\.js$/,
exclude: /node_modules/,
use: 'babel-loader'
},
{
test: /\.html$/,
use: [
{
// load with html loader and then minimize
loader: 'html-loader',
options: {
attrs: ['img:src', 'link:href'],
minimize: true,
interpolate: true,
}
}
]
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},
{
test: /\.(png|svg|jpg|gif)$/,
use: [
{
loader: 'file-loader',
options: {
esModule: false,
},
}
],
},
]
},
plugins: [
/* new CleanWebpackPlugin({
cleanAfterEveryBuildPatterns: ['dist']
}), */
new HtmlWebpackPlugin({
template: './src/index.html',
filename: './index.html',
}),
/* new MiniCssExtractPlugin({
filename: '[name].css',
chunkFilename: '[id].css',
ignoreOrder: false,
}) */
],
optimization: {
splitChunks: {
chunks: 'all',
},
},
output: {
filename: '[name].bundle.js',
path: path.join(__dirname, 'dist'),
publicPath: '/'
},
}
这是我在netlify上最新的部署预览的链接:netlify
还有一个指向我的存储库的链接,以防您想复制它:github
我在做什么错了?