I'm building a simple web page for a test I have to do using a node.js server. I can easily include an image via src like this:
<img src="assets/young-woman.jpg">
However, when I try to do this:
style="background-image: url(assets/young-woman.jpg);"
...it doesn't work.
I have tried following the various solutions to this problem such as: https://github.com/webpack-contrib/css-loader/issues/256 and this Webpack - background images not loading but none of the solutions work for me.
It also works fine if I do it via the CSS like so:
.element {
background-image: url(../assets/young-woman.jpg);
}
Clearly there must be something wrong with my webpack.config file?
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const webpack = require('webpack');
const path = require('path');
module.exports = {
entry: {
app: './src/index.js',
},
devServer: {
contentBase: './dist'
},
devtool: "source-map",
module: {
rules: [
{
test: /\.scss$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [{
loader: "css-loader", options: {
sourceMap: true
}
}, {
loader: "sass-loader", options: {
sourceMap: true
}
}],
publicPath: '../',
})
},
{
test: /\.(png|jp(e*)g|gif)$/,
use: [{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'img/'
}
}]
},
{
test: /\.(html)$/,
use: {
loader: 'html-loader',
options: {
attrs: [':src']
}
}
},
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['env']
}
}
}
]
},
plugins: [
new ExtractTextPlugin({
filename: "css/[name].[contenthash].css",
}),
new CleanWebpackPlugin(['dist']),
new HtmlWebpackPlugin({
template: 'src/index.html',
inject: 'body',
filename: 'index.html'
}),
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery',
Popper: ['popper.js', 'default'],
Util: "exports-loader?Util!bootstrap/js/dist/util",
Dropdown: "exports-loader?Dropdown!bootstrap/js/dist/dropdown",
})
],
output: {
filename: '[name].js',
path: path.resolve(__dirname, 'dist'),
publicPath: ''
}
}
When I try to load the resources in a browser I just get Cannot GET /assets/young-woman.jpg
I've tried installing express, but that also doesn't work for me and I get an error about fs. I can't seem to get my head around this. Is it a problem with my set up perhaps?
Thanks in advance