-
-
Notifications
You must be signed in to change notification settings - Fork 389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add publicPathRelativeToSource option #368
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks very weird, please create minimum reproducible test repo, i can't understand you structure and what is problem. Don't forget you can use __webpack_public_path__
if you want to change publicPath (https://webpack.js.org/configuration/output/#outputpublicpath) inside code.
@evilebottnawi I shall put together an example! I can't use Example to come... |
Here is a zip of a very basic project that demonstrates the issue. The project is setup to use my fork with the code in this PR, so you can turn on/off
Then look at Then look at This PR would made that relative URL Edit |
Please search issue in file loader about this and read why it was removed and can break your code in many cases, it is really unsafe, I try to search other approach to solve your problem, but maybe it is won't fix, other bundlers also don't do this, even you do fork it is solution only for you case in your project and can lead to problem in future |
@evilebottnawi thank you, I looked at file-loader history and I found what I believe was the bug you are referring to: webpack-contrib/file-loader#221 However I don't think this bug is relevant in this case. File-loader is indeed the wrong place for this approach, as it outputs many different file types while this module only outputs one. My PR creates URLs relative to the CSS file, which is how CSS URLs work. Whereas file-loader results go on to be used in JavaScript, HTML and CSS which have conflicting relative path handling. In my opinion, every solution that ends up setting a relative Are you able to point me to or suggest hiw this approach fails (aside from the failure case I identified regarding output being at a different depth than source)? That would really help me understand the problem that I’m not currently seeing clearly! (For reference, this is the main source of info I found in file-loader webpack-contrib/file-loader#221 and also relevant webpack-contrib/file-loader#32 webpack-contrib/file-loader#257 webpack-contrib/file-loader#260) The webpack-contrib/file-loader#221 seems to suggest that if file-loader outputs a resource once its output may be reused by other loaders and therefore be invalid. Is that true? Wouldn’t that be a problem simply by specifying |
I think support |
@evilebottnawi it will definitely solve my issue, and I will make a PR for that as it will be nice to have this solvable. I do think that given the interest in relative |
@karlvr feel free to update/create new PR 👍 |
Can we close this PR? |
All good. I've closed it. I'm going to continue to tinker and see if I can get access to the output file path nicely and do the relative paths comprehensively; for myself and I'll try to share again :-) |
This PR contains a:
Motivation / Use-Case
Addresses #367 by adding a
publicPathRelativeToSource
option to dynamically adjust thepublicPath
passed down to import loaders so that paths in the output CSS will be relative to the source CSS file rather than the context.The browser treats URLs in CSS files relative to the CSS file, so having URLs be relative to the CSS file is natural in CSS. A common solution is to set
publicPath
to an absolute path, but that isn't always possible. Another common solution is to setpublicPath
to../
, which assumes that your CSS file is output into./css/
(or some other folder, but just one folder) and fails if you output a CSS file more than one folder deep.(URLs in the CSS that comes into
mini-css-extract-plugin
are made relative to the context bycss-loader
, which produces JavaScript where URLs should be relative to the context)Note that this solution only works if the output CSS is at the same depth relative to the context as the source file.
Breaking Changes
N/A
Additional Info