-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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(indexHtml): support transform assets url to safe path #12334
base: main
Are you sure you want to change the base?
Conversation
|
You're fixing a ton of bugs! 👏🏼 |
Sorry for missing the PR's description, I've updated it. Not sure whether this PR is a good solution. I will add the test cases later |
I'm not sure if this is the right fix. I think we need to change it here vite/packages/vite/src/node/server/middlewares/indexHtml.ts Lines 232 to 247 in dd7a2f1
Something like I think the initial idea is to have the HTML be as plain as possible (without much processing). The implementation detail when building HTML files enabled #12260, but isn't something we publicly support, so I guess we could say that this is intended behaviour. |
Yeah. I've thought about that approach too, but it's a bit trickier for now. So I was wondering whether we can support resolving URLs during runtime in these special cases. I don't think converting URLs in html via indexHtml in the first place conflicts with resolving them at runtime, both approaches rely on the capabilities of the On the other hand, reserving the html's URLs in dev mode has a better DX. |
The issue I think is that this PR carves out a part of the resolve logic for a case in HTML, so it doesn't cover aliases etc, and causes extra resolving for unrelated parts of the lifecycle. So it's probably better to do it once that supports all correctly. |
I think it's possible to add alias resolving to resolve plugin, even though I haven't added it to this PR. I agree with you
I had an idea that we could convert the assets URLs to js code like |
I'm also not sure if we should support this. My first reaction is that we should do it because it is valid to do the same with a script tag and js. But I agree with @bluwy that we shouldn't bend the current resolve logic to support this. We discussed today about perf with the team, and we should try to remove and simplify the resolve plugin moving forward instead of adding more checks. |
Yeah. I agree with @bluwy too. As I dig deeper into this, I think it's possible to reuse the capability of |
92e7256
to
b7d0ec4
Compare
@patak-dev @bluwy sorry to ping you again, I just implemented the idea mentioned before, it works for the regression tests and new cases of alias pattern link, there may be still some edge cases that need to be handled, but I guess the solution will work fine. If you still have questions about this solution, that's fine to close the pr directly. |
This looks like a neat trick! But I'm not sure if it's a good thing to keep this hack within Vite, and I can't say I've not done this myself before though 😄 I think ideally we should try to extract Thanks for testing this out though. We could probably reserve this hack as a last resort if needed. |
Description
fix #12260
vite server will try to resolve files from the root directory while receiving assets requests which start with '/',but some requests maybe expect to visit the assets from the,node_modules
this PR is trying to add a fallback to resolve assets from the.node_modules
while requested files don't exist in the root directoryWe could mock a fake entry file
_vite_entry_.js
to import assets URLs, then covert asset URLs to import statement and transform them to the safe URLs viaimport-analysis
plugin.Additional context
Also, I think we could enhance to resolve asset requests which match the alias pattern for better DX.
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).