feat: read sec-fetch-dest header to detect JS in transform#9981
feat: read sec-fetch-dest header to detect JS in transform#9981bluwy merged 4 commits intovitejs:mainfrom
sec-fetch-dest header to detect JS in transform#9981Conversation
|
The problem is that known sources of JS are used in other places, and not only in this condition. I think we should try to resurrect @dominikg's #3828 so @haikyuu can add |
|
Hmm, @dominikg's PR was in response to #2829, which discusses various issues with dependency scanning and bundling for non-Vue/Svelte/Astro SFCs. That looks like a different beast to me, and also requires changes for the html detection regex and the SFC script extraction logic as described in the first comment there.
From a quick search through the Vite source, Overview of
|
|
Hmm, after thinking this through, running the |
If I understand you correctly, you want us to start marking every known JS source with |
No, I just want to append
to this:
to make sure that these to behave the same way: <script type="module" src="./entry.myjsdialect"></script>
<!-- and -->
<script type="module">import "./entry.myjsdialect"</script>Currently, the first variant breaks because the entry file will not be transformed by plugins, but the second variant works. That's inconsistent behavior imo. So the change would be to run the src attr of a |
This is a stretch of the word import, but it makes sense to me as a temporal patch. I think we can do it and work to expose a configuration option or change the scheme in other PRs |
|
Cool, gonna update the PR later! 👍 And yeah, thinking of an entry src as an import first sounded strange to me too. But it kind of makes sense when you consider that it should be equivalent to an actual import statement in an inline module script — and MDN calls it an import, too:
|
|
Re About appending So I think there's three ways here:
While writing this comment, I came up with the third one and I feel it's the best approach for now. |
Agreed, browser support is not quite there yet. But fwiw, the But yeah, |
|
I will test this tomorrow and see if it works. |
That must be an error on your side, can't reproduce. However it does indeed not work – if (importQueryExists(id)) {
if (id.endsWith('.imba')) addImportQuery() // ← never reached! :(
transform()
} |
|
I guess we can add imba to the hardcoded list and work on a proper mechanism to allow plugin developers to declare their extensions |
That's a good point, I totally forgot that... configureServer(server) {
server.middlewares.use((req, res, next) => {
if (req.url.endsWith('.imba')) {
req.url += '?import'
res.setHeader('Content-Type', 'application/javascript')
}
next()
})
}, |
|
@sapphi-red That's great. That actually works, thank you :) https://stackblitz.com/edit/vitejs-vite-gmkyeg?file=vite.config.js I'll now make a new version of the plugin. Should we document this in the plugins section? or do you think it's worth adding as an option to the plugin api? |
|
After some testing, I found a little tweak to @sapphi-red's solution in order to support query params that are added sometimes: |
|
But that seems to break other things. I have a couple of failing tests now that are complaining about this https://github.com/vitejs/vite/blob/main/packages/vite/src/node/server/moduleGraph.ts#L144 |
|
@haikyuu you may want to use a utility like vite/packages/vite/src/node/utils.ts Line 307 in 9f8d79b Note: Maybe we could expose |
|
Safari 16.4 Beta added support for Fetch Metadata Request Headers. So in a year, I guess we can start using |
|
is there any workaround in the meantime? could this be otherwise merged? there's another solution in: #3828 which could be good too... |
|
Again cautiously noting that Vite does already use |
|
We discussed in the last meeting whether we can merge this in Vite 5. It's been only half a year since Safari started supporting Re the fact Vite already uses |
i believe we should do even more. just exposing this util and expecting most framework plugins to call configureServer and bring the same update middleware is still duplicating that code over a lot of plugins. Setting up custom extensions is a common need so it should be as easy as defining an array of extensions that transform to js. |
|
/ecosystem-ci run |
|
📝 Ran ecosystem CI on
✅ analogjs, histoire, ladle, laravel, nuxt, quasar, qwik, rakkas, storybook, unocss, vite-environment-examples, vite-plugin-pwa, vite-plugin-react, vite-plugin-react-swc, vite-plugin-vue, vite-setup-catalogue, vitepress, vuepress |
sec-fetch-dest header to detect JS in transform
Vite 6 uses the Sec-Fetch-Dest header field to detect when a file should be treated as a JavaScript module rather than a raw file. So when a classic JS file is served in dev, it is served as a module. However, we actually want the raw file in this situation, and we were relying on the previous (Vite 5 and lower) behavior that did not detect this as a JavaScript file. The ?raw query ensures that the raw file is served (this is supported in Vite 4 and 5 as well). Change in Vite 6: vitejs/vite#9981 Fixes: #2
…9981) Co-authored-by: bluwy <bjornlu.dev@gmail.com>
Description
fix #9963
Additional context
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123).