I try to use webpack sass-loader to load a .scss
file which contains background images. Part of the .scss
is like
.clouds_two {
background: url(require("../../../images/cloud_two.png")); //no error, just not display
//background: url("../../../images/cloud_two.png"); // 404 error
position: absolute;
left: 0;
top: 0;
height: 100%;
width: 300%;
-webkit-animation: cloud_two 75s linear infinite;
-moz-animation: cloud_two 75s linear infinite;
-o-animation: cloud_two 75s linear infinite;
-webkit-transform: translate3d(0, 0, 0);
-moz-transform: translate3d(0, 0, 0);
-o-transform: translate3d(0, 0, 0)
}
Corresponding loaders in my webpack config file are set as
{
test: /\.scss$/,
exclude: /node_modules/,
loaders: ['raw-loader', 'sass-loader'] // sass-loader not scss-loader
},
{
test: /\.(png|jpg|gif)$/,
loader: 'url-loader?limit=8192'
}, // inline base64 URLs for <=8k images, direct URLs for the rest
My .scss
file is in src/app/components/login/
and the image is in src/images/
. The webpack output folder is dist
which is of same level as src
folder.
The problem is when I use background: url(require("../../../images/cloud_two.png"));
, there's no error but the background image not showing.
If I use background: url("../../../images/cloud_two.png");
, there's a 404 error complaining http://localhost:8080/images/cloud_two.png
.
The sass-loader's readme has a Problems with url(...)
section, but it can't fix my case.