我在项目的根目录中有两个文件夹 - build和src。我想将图像从src文件夹移动到构建文件夹。但是我收到错误“无法解析'文件'”,尽管我已经包含了url-loader插件。
header.jsx
import {React, ReactDOM} from '../../build/react';
export default class Header extends React.Component {
render() {
return (
<div className="header">
<img className="logo" src={require("../src/content/images/program-brand-logo.jpg")} />
</div>
);
}
};
错误日志:
ERROR in ./src/components/header.jsx
Module not found: Error: Cannot resolve 'file' or 'directory' ../src/content/images/program-brand-logo.jpg in /Users/username/Desktop/Projects/Demo/Scorecard/SPA/React (Working Copy)/src/components
@ ./src/components/header.jsx 45:68-123
Webpack.config.js
var webpack = require('webpack');
var path = require('path');
require("babel-polyfill");
var BUILD_DIR = path.resolve(__dirname, 'build');
var APP_DIR = path.resolve(__dirname, 'src');
module.exports = {
entry: [
// Set up an ES6-ish environment
'babel-polyfill',
'bootstrap-loader',
'webpack/hot/dev-server',
APP_DIR + '/import.js',
],
output: {
path: BUILD_DIR,
filename: 'bundle.js'
},
module: {
loaders: [{
test: /\.jsx?$/,
loader: 'babel',
exclude: /node_modules/,
query: {
plugins: ['transform-runtime'],
presets: ['es2015', 'stage-0', 'react']
}
}, {
test: /\.css$/,
loader: "style-loader!css-loader"
}, {
test: /\.scss$/,
loaders: ["style", "css", "sass"]
}, {
test: /\.(png|woff|woff2|eot|ttf|svg|jpg|gif)$/,
loader: 'url-loader?limit=100000'
}]
},
plugins: [
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery"
}),
new webpack.HotModuleReplacementPlugin()
]
};
答案 0 :(得分:4)
您不必使用require来加载图像,您可以将其导入为导入的其他依赖项。
import img from './../src/content/images/program-brand-logo.jpg'
.....
<img className="logo" src={img} />
或者我应该指定你的路径如下'./../ src / c ontent / images / program-brand-logo.jpg'
答案 1 :(得分:0)
确保此路径../src/content/images/program-brand-logo.jpg
相对于header.jsx
文件
答案 2 :(得分:0)
只需删除
?limit=100000 from
test: /\.(png|woff|woff2|eot|ttf|svg|jpg|gif)$/,
loader: 'url-loader?limit=100000'
并构建Webpack 您的最终测试加载程序应如下所示:
test: /\.(png|woff|woff2|eot|ttf|svg|jpg|gif)$/,
loader: 'url-loader'