-
-
Notifications
You must be signed in to change notification settings - Fork 1.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
in vitest browser runner plugin with transformIndexHtml to inject importmap script manually does not work. #6394
Comments
We do need to provide a way to use your own HTML, but I don't know how. Open to ideas. Import maps are tricky because they are not supported by Vite, so we can't add our own support otherwise it will contradict the dev environment. Also, I noticed you are using 1.6.0. The browser mode was rewritten in Vitest 2.0, we do not support lower versions anymore. |
There is a "right" and "fast" solution. The "right" is to allow to accept the web page from running web server and inject the code. This way it can be any framework or StoryBook built natively. The "fast" solution is to alter
The hacking attempt into PS. Upgrade vitest to current does not change the approach. The HTML loading code stays the same in "@vitest/browser": "^2.0.5", |
Proposed PR for review: |
@sheremet-va , in PR ^^ there is one of possible solutions. It can be not optimal and perhaps does not follow the current codebase conventions though. Who/how I can ask for review of principle? |
While the PR under review, there is a work around. In project root run:
// injects importmaps for module-url.test.stories.ts
import fs from 'node:fs'
const pathName = 'node_modules/@vitest/browser/dist/client/tester/tester.html';
const jsStr = fs.readFileSync(pathName).toString();
if( !jsStr.includes('importmap') ) // skip if already patched
{
const injectedStr = jsStr.replace('</style>', `</style>
<script type="importmap">
{
"imports": {
"lib-root/": "./demo/lib-dir/",
"embed-lib": "./demo/lib-dir/embed-lib.html"
}
}
</script>
`);
fs.writeFileSync(pathName, injectedStr);
} |
Describe the bug
Need the importmap available for non-js resources like SVG and HTML to be resolved natively by import.meta.resolve( path ).
For some reasin in vitest browser runner the usual plugin with transformIndexHtml to inject importmap script manually does not work.
Advised in vitejs/vite#2483 method of injecting
script
via browser.testerScripts is not applicable as a) import maps requiresscript type="importmap"
, b) it is injected afterstateScript
which causes "import maps injected after module use" error. Following request, openingthe bug on vitest.Root cause:
importmap
for use byimport.meta.resolve( path )
API. Vitest in browser does not support import maps.vite
build and altering it afterwardsSolving on any level from $subj to root causes, better all, would be beneficial for frameworks and reusable libs authors.
Reproduction
System Info
Used Package Manager
yarn
Validations
The text was updated successfully, but these errors were encountered: