在 React 上存储本地资产非常新。
我已经使用 file-loader 和 image-loader 配置了 webpack,并将图像存储在本地,但由于某种原因,它似乎没有显示:
import React, { Component } from 'react';
import WorkItem from './work-item';
import WorkItemsArray from './work-items-array';
class Work extends Component {
render() {
return (
<div id='work'>
<h1>Work</h1>
<img src={require('../images/weatherImg.png')}/>
<div id='portfolio'>
{WorkItemsArray.map(({ url, img, title, description, github }) => {
return (
<WorkItem
key={title}
url={url}
img={img}
title={title}
description={description}
github={github}
/>
);
})}
</div>
</div>
);
}
}
export default Work;
webpack.config.js
var webpack = require('webpack');
module.exports = {
entry: [
'./src/index.js'
],
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader'
},
{
test: [/\.css$/, /\.scss$/],
exclude: /node_modules/,
loaders: ['style-loader', 'css-loader', 'sass-loader']
},
{
test: /.*\.(gif|png|jpe?g|svg)$/i,
use: [
{
loader: 'file-loader',
options: {
name: 'images/[name]_[hash:7].[ext]'
}
}
]
},
{
test: /\.(gif|png|jpe?g|svg)$/i,
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,
},
// the webp option will enable WEBP
webp: {
quality: 75
}
}
}
]
}
]
},
resolve: {
extensions: ['*', '.js', '.jsx']
},
output: {
path: __dirname + '/dist',
publicPath: '/',
filename: 'bundle.js'
},
devServer: {
contentBase: './dist',
historyApiFallback: true
},
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
})
]
};
FWIW,我根本没有收到任何错误,只是图像似乎没有加载。
非常感谢任何帮助!