-
-
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
Can't alias
module in browser mode when dependency is aliased
#6646
Comments
I don't understand what you are trying to achieve, to be honest. Do you want to always resolve to |
I would recommend creating an <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script type="module" src="./components/DateFnsV3.tsx"></script>
<script type="module" src="./components/DateFns.tsx"></script>
</body>
</html> {
"scripts": {
"dev": "vite dev"
}
} |
I want files that contain I see this as the similar issue from my original topic. There one of the issues was fixed, which is was using |
This is debatable, Vitest in Node.js doesn't actually follow the same resolving mechanism and tries to resolve the same thing twice if it doesn't work. The error happening in the browser is the correct behaviour. As I said, try using this config with a normal Vite project and you will see the same error. |
This also doesn't explain what I asked. Do you want |
I want |
It is not debatable that it works. 🙃 And I understand that running it in the browser gives me errors. If I rename
which means the config is working, but the files are not present 🤔 |
It is debatable whether it works correctly.
It means you misconfigured the In conclusion, the config works correctly. The error is expected, the fact that it works in Node.js fake |
🤣 true
This input helped. I managed to get this to work with the monstrosity below. It is not pretty though. But it works, if anyone needs something similar in the future. {
find: 'date-fns',
replacement: 'date-fns-v3',
customResolver(source, importer) {
if (importer?.includes('DateFnsV3')) {
return path.resolve(__dirname, 'node_modules', `${source}.mjs`)
}
return path.resolve(__dirname, 'node_modules', 'date-fns', 'index.js')
},
} |
This is a more reasonable solution {
find: 'date-fns',
replacement: 'date-fns-v3',
async customResolver(source, importer, options) {
if (importer?.includes('DateFnsV3')) {
return import.meta.resolve(source).replace('file:', '')
}
return import.meta.resolve('date-fns').replace('file:', '')
},
} |
Describe the bug
Same as #6483 which resolved this issue for
mock
But for
vitest.config.ts
resolve.alias
We currently have the following in our package
This is due to the different signatures between these two versions. In the final code, this is a peer dependency, so inside our code we only always use import from 'date-fns'.
But when testing, we want to test the actual library, which requires some aliasing.
Reproduction
https://github.com/JCQuintas/vitest-browser-alias-mock
Important Files
Ensure you uncomment the alias in
vitest.config.ts
Running
System Info
Used Package Manager
pnpm
Validations
The text was updated successfully, but these errors were encountered: