css-loader: Unexpected output of url includes `file://` protocol
- Operating System: MacOS 11.4
- Node Version: 16.3.0
- NPM Version: 7.15.1
- webpack Version: 5.41.1
- css-loader Version: 6.2.0
Using default options of css-loader
transform background-image: url(../img/foo/bar.jpg)
supposed to be background-image: url(/img/foo/bar.jpg)
but go background-image: url(file:///img/foo/bar.jpg)
,
please see the code below
the original css code
.Banner {
height: 240px;
background-image: **url(../assets/img/banner.jpg)**;
background-position: center;
background-size: cover;
background-repeat: no-repeat;
display: flex;
align-items: center;
justify-content: center;
position: relative;
margin-bottom: 35px;
}
Expected Behavior
.Home_Banner__3Z7xi {
height: 240px;
background-image: **url(/33e75dae657057c9451f.jpg)**;
background-position: center;
background-size: cover;
background-repeat: no-repeat;
display: -webkit-flex;
display: flex;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: center;
justify-content: center;
position: relative;
margin-bottom: 35px;
}
Actual Behavior
.Home_Banner__3Z7xi {
height: 240px;
background-image: **url(file:///33e75dae657057c9451f.jpg)**;
background-position: center;
background-size: cover;
background-repeat: no-repeat;
display: -webkit-flex;
display: flex;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: center;
justify-content: center;
position: relative;
margin-bottom: 35px;
}
Code
// webpack.config.js
{
test: /\.css$/,
loader:require.resolve('css-loader')
}
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 26 (12 by maintainers)
Commits related to this issue
- repro with webpack 5.22.0 — committed to kenrick95/css-loader-repro-file-protocol by kenrick95 3 years ago
@kenrick95 thanks, this solved the problem for me. I was using css-loader v6.7.1, mini-css-extract-plugin v2.6.0, and webpack v5.21.0. I was getting my
url('./relative-file.ttf')
imports tranformed into this crazy string:(where
...
is the absolute path to my project directory).Upgrading webpack (now i’m on 5.71.0) fixed it!
FYI for others encountering this bug: I found that this bug exist if we use css-loader (v6) + mini-css-extract-plugin (v2) with webpack <= 5.22.0. Found that bug will not exist anymore if webpack is upgraded to >= 5.23.0
Repro: https://github.com/kenrick95/css-loader-repro-file-protocol
Yes, feel free to send, we just need to doc it, because it is very exotic
My technical stack is up to date but highly customized so i’m not asking you (or someone) to change something on mini-css-extract-plugin or anywhere else. I think i am the only one to use mini-css-extract-plugin to just “extract css” 😉 and i’m rather have to adapt a well maintained plugin like mini-css-extract-plugin on each update than developing a new one. Thx for the advice and for your great job on webpack !