Description
This appears to be an ongoing and reasonably common issue. See #44.
The issue is that webpack outputs URLs relative to the context (assumed to be where the HTML is), which works fine for JS but not CSS, as urls in CSS files are resolved relative to the CSS file, while URLs in JS files are resolved relative to the HTML.
Using publicPath
is a blunt instrument that either requires you to use an absolute path, or to use a relative path and to always output your CSS files at the same depth (e.g. set publicPath
to ../
and always output your css files into css/
).
I am preparing a simple patch for mini-css-extract-plugin
to add an additional option which I think I'll call urlsRelativeToSource
. It dynamically adjusts the publicPath
that it sends to downstream loaders to the relative path from the source CSS file to the root context...
So if the source file is ./forum/css/main.css
then publicPath
is set to ../../
when passed down to url-loader
or file-loader
or whatever is next, which means a url in your CSS that was ../img/header.png
, becomes ../../forum/img/header.png
. When loaded from your CSS _assuming it's output to ./forum/css/main.css
, that relative URL (while containing a redundant ..
and forum
) works.
The assumption that is required here is that you'll output your CSS files at the same depth in the directory structure as the source file is. Hence the option is urlsRelativeToSource
. This is because I don't think a loader knows where the output file is going, otherwise we could just make the URLs relative to the output.
Is anyone interested? This fixes the issue for me... will this be useful for others and will it be acceptable as a patch? I don't want to base our workflow on a fork!!