【发布时间】:2016-03-25 00:05:46
【问题描述】:
当我尝试使用 webpack 构建 SASS 文件时,出现以下错误:
找不到模块:错误:无法解析模块“文件加载器”
请注意,此问题仅在我尝试使用相对路径加载背景图像时发生。
这个工作正常:
background:url(http://localhost:8080/images/magnifier.png);
这会导致问题:
background:url(../images/magnifier.png);
这是我的项目结构
- 图片
- 样式
- webpack.config.js
这是我的 webpack 文件:
var path = require('path');
module.exports = {
entry: {
build: [
'./scripts/app.jsx',
'webpack-dev-server/client?http://localhost:8080',
'webpack/hot/only-dev-server'
]
},
output: {
path: path.join(__dirname, 'public'),
publicPath: 'http://localhost:8080/',
filename: 'public/[name].js'
},
module: {
loaders: [
{test: /\.jsx?$/, loaders: ['react-hot', 'babel?stage=0'], exclude: /node_modules/},
{test: /\.scss$/, loaders: ['style', 'css', 'sass']},
{test: /\.(png|jpg)$/, loader: 'file-loader'},
{test: /\.(ttf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/, loader: 'file-loader'}
]
},
resolve: {
extensions: ['', '.js', '.jsx', '.scss', '.eot', '.ttf', '.svg', '.woff'],
modulesDirectories: ['node_modules', 'scripts', 'images', 'fonts']
}
};
【问题讨论】:
-
css-loader 将每个
url(...)转换为require(...),因此../images/magnifier.png被/\.(png|jpg)$/测试选中。你有安装文件加载器吗? -
@silvenon,感谢您的回复 file-loader 坏了,所以我重新安装了它,一切都会好起来的。谢谢