【发布时间】:2018-01-06 15:00:28
【问题描述】:
我已经用 webpack 创建了 html 样板。我面临的问题是,如果我在 scss 中调用图像,例如“背景:url('/img/img.png');”它没有。在这里,我附上了 webpack 文件和文件夹结构。我曾经尝试使用“url-loader”,这也不起作用。请帮助某人解决此问题,因为我已经尝试了很长时间才能完成此问题,但仍然无法找到解决方案。
这是我的文件夹结构,
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;
【问题讨论】:
-
您是否在控制台中遇到任何错误?
-
@hunzaboy 我收到 404 错误。如果我喜欢“背景:url('../img/img.png');”获取无法解决模块问题
-
我认为你需要添加图片加载器。更多这里ag-grid.com/ag-grid-understanding-webpack
-
为什么不用绝对路径?
标签: javascript html css webpack