我用webpack創建了html樣板文件。我面臨的問題是,如果我在scss中調用圖像像「background:url('/ img/img.png');」它沒有采取。在這裏,我附加了webpack文件和文件夾結構。我曾嘗試使用「url-loader」,這也不起作用。請幫助某人解決此問題,因爲我已經嘗試了很長時間才完成此操作,但仍無法找到解決方案。webpack css背景圖片不顯示?
這是我的文件夾結構,
Project
|
+-- html
| |
| +-- css
| +-- img
| +-- js
| +-- index.html
|
+-- src
| |
| +-- js
| +-- scss
的WebPack文件
'use strict';
let webpack = require('webpack');
let path = require('path');
let nodeModulesPath = path.join(__dirname, 'node_modules');
let ExtractTextPlugin = require("extract-text-webpack-plugin");
let exp = []; // multiple exports array
// Exports configs for each language
let configs = [
{
name: 'min',
entry: './src/index.js',
scssRule: {
test: /\.scss$/,
use: ExtractTextPlugin.extract({
use: [{
loader: "css-loader",
options: { minimize: true },
},
{
loader: "sass-loader",
options: { minimize: true },
},
],
fallback: "style-loader"
}),
}
}
];
// Generate exports module for each config
for (let c of configs) {
var e = {
context: __dirname,
watch: true,
entry: c.entry,
output: {
path: path.resolve(__dirname, 'html/'),
//pathinfo: true,
filename: "js/app.js"
},
module: {
rules: [{
enforce: 'pre',
test: /\.js$/,
exclude: /node_modules/,
loader: "jshint-loader"
},
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
use: [{
loader: "css-loader",
options: { minimize: true },
}],
fallback: "style-loader"
}),
},
c.scssRule,
{
test: /\.(ttf|eot|woff|woff2|svg)$/,
loader: 'file-loader',
options: {
name: '../[path].[ext]',
publicPath: '../',
emitFile: false
},
},
{
test: /\.(jpe?g|png|gif|svg)$/i,
loader: 'file-loader',
options: {
name: '../[path].[ext]',
publicPath: '../',
emitFile: false
},
}
]
},
// Use the plugin to specify the resulting filename (and add needed behavior to the compiler)
plugins: [
new ExtractTextPlugin({
filename: 'css/app.css'
}),
new webpack.optimize.UglifyJsPlugin({
compressor: { warnings: false }
})
]
}
exp.push(e);
}
module.exports = exp;
你在控制檯中得到任何錯誤? – Aslam
@hunzaboy我得到404錯誤。如果我喜歡「background:url('../ img/img.png');」不能解決模塊問題 – Sathya
我認爲你需要添加圖像加載器。更多https://www.ag-grid.com/ag-grid-understanding-webpack/ – Aslam