Fix subpath script import from subpath html #114
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I recently discovered that if an html file located in a sub-path of the root imported a script in the sub-path using relative path, vite server would complain that the script was not found because he was searching at the root.
i.e. if we got
/subpath/index.html
while the a script was at/subpath/script.js
vite would search
/script.js
.I add test case to give a complete example in the basic env.
I manage to understand that the problem comes from how the vite middleware is done and how the
transformIndexHtml
of vite is used. From the code of vite (https://github.com/vitejs/vite/blob/6c4bf266a0bcae8512f6daf99dff57a73ae7bcf6/packages/vite/src/node/server/middlewares/indexHtml.ts#L435) I discovered that we can give more context on what is the real path of the template so that the path given inside the template take the right reference.For the tests, I am not really satisfied of my solution. The problem is that the server manage to serve what it must serve, their is just an error log from vite that tells their is something wrong. So I spy on the
console.error
calls. I do not now if their is something better to do.