-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
fix: Allow custom URL prefix in CSS #4337
Conversation
merge from vitejs/vite
I moved this to p2, cause it adds a new option |
@patak-js Do you think we need a test for the new option? |
Tests are needed but could be delayed until there is approval for this feature. The last time this was discussed, it looks like Evan said that this is not something that Vite will support #1539 (comment) It would be good to see if this can't be done by a plugin in user land instead of adding more complexity in Core. |
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.
From my side, I give it an approval, cause it is just a simple if condition
Looks not very complex to me 🤔
But yes, we should take this to the next team meeting
We need to find another way. Info will come later.
The current implementation is a no-go, cause it would break to much. But we discussed that we would like to change behavior with a new option e.g. here vite/packages/vite/src/node/plugins/asset.ts Line 178 in 41a00df
This may only be on serve
Alternatively could be an option to force including hostname in asset paths |
Option 1 seems good for me, because my current project is Migrating from Webpack to Vite. If option 2 means modifying asset paths all reference, the workload is a little heavy, case when the HTML which has inline style sheet service is based on the back end, the asset is based on the local service (Vite) and has a different hostname. For option 1, I should move the new config option from By the way, does the config vite/packages/vite/src/node/config.ts Lines 558 to 563 in 43c957d
vite/packages/vite/src/node/plugins/clientInjections.ts Lines 35 to 41 in 43c957d
|
I think Evan was not aware that this is not easily doable in a lot of contexts Personally, I just need to have the full base (protocol + domain + port) in the asset URL, as suggested by this solution. @Shinigami92's proposal of a boolean option that would do that sounds good to me and would solve all of the back-end issues we have with Laravel, Craft, Symfony, Adonis, etc. We just need that in development, not while building. |
Modifying |
I echo @innocenzi 's sentiments, it would solve the problems I'm having, too, and eliminate the need for custom plugin work-arounds ala: https://nystudio107.com/blog/using-vite-js-next-generation-frontend-tooling-with-craft-cms#vite-processed-assets ref: #2394 |
Thanks for your input @innocenzi @khalwat, I think a new The idea is to modify this line vite/packages/vite/src/node/plugins/asset.ts Line 178 in 41a00df
with return ( config.assetsFullPathInDev ? config.server.origin : '' ) + config.base + rtn.replace(/^\//, '') Where vite/packages/vite/src/node/server/index.ts Line 613 in 41a00df
vite/packages/vite/src/node/server/index.ts Line 570 in 41a00df
We may be able to get everything we need and set the origin or server base URL directly after setting the config port using httpServer.address() .
|
As for the boolean option I'd say something like Either way, I'm looking forward to this issue being solved, thanks for working on it! :) |
FWIW, I believe webpack uses the name To distinguish them, the nomenclature I've been using is:
https://github.com/nystudio107/craft-vite/blob/v1/src/config.php#L39 But I think given that Vite has already defined these in Given that the URLs these affect are things stored in the
...because it would affect asset URLs that would be normally coming from the
...to pair with the existing You may not even need a boolean flag btw... you could just have it default to the normal behavior if this setting is empty, and use the URL specified here if it is not. |
or,Can we change our thinking and add a plugin hook called |
I also discussed with @Akryum and the boolean option would not be enough for him. As it's served on But what @khalwat points out shows that the requirements for this feature are not clear. If the real requirement is that everything from public is going to have the origin URL attached (or another local dev server URL), then it isn't the same as doing it for all asset types. @innocenzi your input would also be helpful here. One problem with the proposed approach is that some URL are not touched by Vite. For example if you have a react component with some images pointing to public. There is some handling of this kind of paths in Vue templates when using the SFC compiler but we are going to be missing some public paths. So, the proposed change is not the same as the plugin you were using based on #2394 (comment). For reference {
enforce: 'pre',
apply: 'serve',
transform: (code, id) => {
code = code.replace(/(from '|import\(')(\/src|~?@)\/(.*)\.(svg|png|mp3|mp4)/g, '$1https://webpack.livestorm.local/src/$3.$4?import=')
code = code.replace(/(?<!local)(\/src|~?@)\/(.*)\.(svg|png|mp3|mp4)/g, 'https://webpack.livestorm.local/src/$2.$3')
return {
code,
map: null,
}
},
} Because with this plugin you are modifying all the URLs, not only the ones that Vite rewrites. The same applies to creating a new hook like @2359634711 proposed. I don't know how are we going to account for these URLs from inside Vite... looks like regex/replace is the only way to catch all (including some false positives) I think that the cleanest way to do this would be to instruct the browser to do this redirect. Have you thought about using a service worker for this following the same pattern used in https://mswjs.io? |
That sounds basically like my use case. First URL is the development server URL, second is the local site one. Assets URLs need to have In other words, to clear it up, instead of I'm not sure why the boolean option would not be enough, but as long as the proposed solution allows to have a full URL with an origin, it's good enough for me. Although the boolean option was appealing because it's simple, it is indeed nice to have more control, so allowing to pass a string seems fine.
Do you mean that some URLs are transformed at build-time, but not because of Vite? I'm not sure to understand that point. Also the term "public path" is confusing to me, I'm not sure what it refers to... What I don't really understand is the fact that the production build outputs the right URLs, so they are all passed through Vite at some point, right? I don't know the internals very well but I really thought your previous suggestion would solve the issue. |
So far I've only needed to add the origin to things handled by Vite: imported files (we also import all the images in JS files), files referenced in CSS/PostCSS/Stylus... |
Coming from the overall problem with a symfony project i like to add on this detail: To elaborate further and picking up @Akryum 's last comment: To add an Example: On a dev server starting up with: a image (from a non-public directory ) i reference in CSS is available at: But vite puts it in the CSS without :3000 (the whole Host-Part) |
To sum up the issue:
This implementation solves the issue by allowing us to define an origin ( The build part was working as expected already, this implementation only changes the development one, so this is what we needed. |
export default { | ||
server: { | ||
publicPath: 'http://127.0.0.1:8080/' | ||
} | ||
} |
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 try to use defineConfig
everywhere in the docs, so folks know that this is a thing and prefer to use it
export default { | |
server: { | |
publicPath: 'http://127.0.0.1:8080/' | |
} | |
} | |
export default defineConfig({ | |
server: { | |
publicPath: 'http://127.0.0.1:8080/' | |
} | |
}) |
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.
The option should be called server.origin
instead of server.publicPath
, other than that, we are good to merge
Description
this PR is aimed to fixed #3070/ #3646/ #1539..
There seems no way to custom the prefix of url (that like PNG/font/SVG etc) in CSS files. But also this feature maybe is very important in some cases(example the files server host is not same as the dev server's, especially when the application can not run at the local dev server).
So, this PR added the option named
publicPath
in CSS config to allow users to custom the prefix of url.Additional context
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).