-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
Vite fails to resolve html-proxy in vitest browser mode #19213
Comments
Thanks for the repro and investigation👍 Can you also explain which plugins actually have this issue (namely injecting inline module script)? If there's a common plugin employing this pattern, it's good for us to know. Also for your use case, is it possible to workaround it by disabling this plugin on Vitest browser mode (e.g. by |
Thanks for quick reply!
I created a Vite plugin for usage within my company for authoring and building web component libraries. Unfortunately, the source code is not open source.
The plugin is injecting code like the following into index.html[
{
tag: 'script',
attrs: { type: 'module' },
children: 'globalThis.litIssuedWarnings ??= new Set();\n' +
'globalThis.litIssuedWarnings.add(\n' +
'"Lit is in dev mode. Not recommended for production! See https://lit.dev/msg/dev-mode for more information.",\n' +
');'
},
{
tag: 'script',
attrs: { type: 'module' },
// ?layered flag is handled by my plugin
children: 'import styles$0 from "@esri/calcite-components/calcite/calcite.css?layered";\n' +
'document.adoptedStyleSheets = [...document.adoptedStyleSheets, styles$0.styleSheet];\n' +
'import { defineCustomElements as defineCustomElements$0 } from "@esri/calcite-components/loader";\n' +
'defineCustomElements$0({\n' +
' resourcesUrl: "/@fs/Users/mak13180/site/esri/arcgis-web-components-3/node_modules/@esri/calcite-components/dist/calcite/",\n' +
'});'
},
{
tag: 'link',
attrs: {
rel: 'stylesheet',
href: '/@fs/Users/mak13180/site/esri/arcgis-web-components-3/node_modules/@arcgis/core/assets/esri/themes/light/main.css',
id: 'arcgisCoreStylesheet'
}
},
{
tag: 'script',
attrs: { type: 'module' },
children: 'import { defineCustomElements } from "/Users/mak13180/site/esri/arcgis-web-components-3/packages/map-packages/map-components/src/loader.ts";\n' +
'window.devOnly$ownTagNames = new Set();\n' +
'defineCustomElements();'
}
] This code loads css for our dependencies, as well as setups lazy loading of web components. It is needed in regular dev sever, storybook dev server and vitest dev server
Ah, absolutely! Before Vitest browser mode started calling transformIndexHtml, I was using test setup files for this. Can confirm this works around the issue in Vitest ^2.1.5 |
Describe the bug
This is a Vite bug that affects Vitest.
vite:html-inline-proxy
plugin is supposed to resolve requests for module scripts from index.html, but it fails to do so in some environments. The issue is that this plugin expects URL to include?html-proxy
, but in some cases the URL instead includes?v=12535af4&html-proxy
, causing regex to not match, and thus request fails to resolve correctly. SeeExplanation
section below for more details.Reproduction
https://github.com/maxpatiiuk/vite-html-proxy-bug
Steps to reproduce
Clone this repository
git clone https://github.com/maxpatiiuk/vite-html-proxy-bug cd vite-html-proxy-bug
Install dependencies
Run vitest browser mode
See the following error message in the terminal:
Explanation
Starting with Vitest 2.1.5, Vitest browser mode calls transformIndexHtml.
If html contains module scripts,
preTransformRequest
will be called:vite/packages/vite/src/node/server/middlewares/indexHtml.ts
Line 257 in 4f5845a
The request URL may look like
/Users/mak13180/site/esri/arcgis-dashboards/node_modules/@vitest/browser/dist/client/tester/tester.html?html-proxy&index=0.js
Later,
ensureVersionQuery
is called to add version hash to query string:vite/packages/vite/src/node/plugins/resolve.ts
Line 277 in 4f5845a
The updated URL:
/Users/mak13180/site/esri/arcgis-dashboards/node_modules/@vitest/browser/dist/client/tester/tester.html?v=12535af4&html-proxy&index=0.js
Such URL is supposed to be resolved by the
vite:html-inline-proxy
plugin:vite/packages/vite/src/node/plugins/html.ts
Lines 96 to 106 in 4f5845a
Here is the bug: the regex that checks for html-proxy URLs expects the URL to contain
?html-proxy
, where as the above URL contains&html-proxy
:vite/packages/vite/src/node/plugins/html.ts
Lines 52 to 53 in 4f5845a
Because of that,
pluginContainer.load(id)
fails to resolve the file. In such cases, it falls back to reading the file from the file system:vite/packages/vite/src/node/server/transformRequest.ts
Line 276 in 4f5845a
fs.readFile()
ignores the query string, reads thetester.html
file, and serves it - however, the .js file was expected. This causes an error in the Pre-transform during import analysis.Solution
Vite should update the following regexes:
vite/packages/vite/src/node/plugins/html.ts
Lines 52 to 54 in 4f5845a
Should replace
\?
by[?&]
.For reference, this is already correct in the
vite:css-post
plugin:vite/packages/vite/src/node/plugins/css.ts
Line 227 in 4f5845a
Also, the Vite team should evaluate whether this bug affects any other Vite plugins.
System Info
Used Package Manager
npm
Logs
Click to expand!
Too large for github. File log.log
Validations
The text was updated successfully, but these errors were encountered: