-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Description
Describe the bug
I think the behavior of relative paths for amd
modules is inconsistent with other output formats and basically wrong.
vite/packages/vite/src/node/build.ts
Lines 1312 to 1317 in 788a183
amd: (relativePath) => { | |
if (relativePath[0] !== '.') relativePath = './' + relativePath | |
return getResolveUrl( | |
`require.toUrl('${escapeId(relativePath)}'), document.baseURI`, | |
) | |
}, |
This is the function that computes the relative path for assets when using amd
output format.
The es
and system
output formats compute the relative urls based on import.meta.url
and module.meta.url
which makes a lot of sense to me.
Unlike that amd
uses document.baseURI
which breaks in our use case:
We use amd
modules to build plugins which are loaded into our application with requirejs at runtime.
At build time neither the server url nor the exact path on that server is known. In particular the base path of the application is not equal to the base path of the plugin, which would work with es
and system
modules but doesn't with amd
.
At least when using requirejs there is module.uri
available, afaict require.toUrl(module.id)
should give us the same (potentially lacking the .js
extension but that wouldn't matter as we're using it only as a base anyway).
So using module.uri
or require.toUrl(module.id)
works in our project.
Will send a PR. I'm happy to discuss alternatives or downsides of my approach
Reproduction
https://github.com/opencloud-eu/web-extensions/tree/main/packages/web-app-unzip
Steps to reproduce
I'm sorry, I haven't provided a completely minimal reproduction repo, but I've identified the relevant code already, which is easy to reason about, while it would be very hard to setup a minimal reproduction repo - because not only a single vite build but also requirejs setup would be required.
In the repo I've linked you can also see the workaround we came up with:
https://github.com/opencloud-eu/web-extensions/blob/6fe6171d7e64a170d5abf9766d0acc6f8b99430b/packages/web-app-unzip/src/composables/useUnzipAction.ts#L58
It's a bit brittle, so I would love to see this properly fixed - but it's not a deal breaker for anything for us. It would just make app development for opencloud much more convenient.
System Info
Completely unrelated
Used Package Manager
pnpm
Logs
No response
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to vuejs/core instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.