-
-
Notifications
You must be signed in to change notification settings - Fork 388
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
publicPath can now be a function #373
publicPath can now be a function #373
Conversation
Also added schema validation for publicPath.
@evilebottnawi One thing about this; I tried to follow the pattern of If that's the case then we can do this perfectly and emit relative URLs in our output file that will work. Contrary to my previous PR #368 which made paths relative to the source file (which probably worked). Given that the example on the |
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 good, need add tests and we can merge 👍
const loaders = this.loaders.slice(this.loaderIndex + 1); | ||
this.addDependency(this.resourcePath); | ||
const childFilename = '*'; // eslint-disable-line no-path-concat | ||
const publicPath = | ||
typeof query.publicPath === 'string' | ||
? query.publicPath | ||
: typeof query.publicPath === 'function' | ||
? query.publicPath(this.resourcePath, this.rootContext) |
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.
We don't need here url
as in file-loader
because file-loader
for other purpose (answer on above question)
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.
@evilebottnawi in file-loader
it's because file-loader
can choose to output the file itself? Doesn't the plugin do exactly that?
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.
@karlvr @evilebottnawi Just wanted to let you know that this introduces a breaking change in the case when the publicPath is set to an empty string meaning that resources are located in the same directory as the css file, e.g. dist/app.css
, dist/aResource.png
. With the introduced change the publicPath is transformed to a /
and so the resource path becomes /aResource.png
which is clearly not desired.
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.
That change was to follow the same behaviour in https://github.com/webpack-contrib/file-loader/blob/master/src/index.js#L37
I'm really sorry about this regression! @evilebottnawi shall we introduce a special case for an empty string here?
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.
@evilebottnawi test added! |
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.
Good PR! Need resolve logic for /
. We don't change this for function
because you can configure webserver handle path/to
and path/to/
in difference way, it is doesn't standard and not a best practice, so we fix it only for string
due in 99% it is mistake
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.
And let's make two tests with /
and without and we can merge 👍
Make it consistent with new publicpath-trailing-slash test
In order to test output into nested directories, such as I’m about to add for the publicpath-function test.
bec55f4
to
0fa6e4b
Compare
With output of the CSS into a nested folder the computation of the relative publicPath makes sense.
0fa6e4b
to
9ef1de0
Compare
@evilebottnawi please excuse the force pushes! I've been trying to make the The test previously used With my It's such a shame that I don't appear to be able to determine the entry point output in the loader to compute a relative publicPath with impunity! Anyway, I'm going to add one more file to the |
Added a second entry point / output at different nesting demonstrating the application of the publicPath function
Anyway we can add more arguments to |
@evilebottnawi the I have finished, unless there's anything else you'd like to suggest. I hope you don't mind the addition of recursive checking out expected output to the tests. In porting legacy projects into webpack it has been important to maintain an existing output file structure (rather than not needing to care where the generated files end up), hence the multiple entry points and caring about nested folders! |
@evilebottnawi agreed we can add more arguments, but we can't reorder them without breaking stuff. Any other arguments you think we should / could add at this stage that want to not go on the end? |
@ericclemmons yep, it is hard to imagine what developers need, so we will add their by request, for release 1.0.0 we normalize their order, it is not scary. When you have big package with a lot of developers major release are inevitable |
@evilebottnawi thanks for this collaboration, it's been a pleasure. |
It is very strange what CI broken 😞 Locally works fine? |
@evilebottnawi looks like it's an audit issue?
from https://circleci.com/gh/webpack-contrib/mini-css-extract-plugin/1038
|
Also added schema validation for publicPath.
This PR contains a:
Motivation / Use-Case
Addresses #367 by allowing
publicPath
to be a function, therefore allowing thepublicPath
to be adjusted according to the resource.Rationale
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)The example in the
README
demonstrates apublicPath
function that solves the relative path problem above, and is an alternative approach to #368Additional contents
I also added schema validation for
publicPath
, which seemed like a useful addition now that it's a bit more complicated. Previously the loader would simply ignorepublicPath
if it was anything other than astring
.