Skip to content

amd: relative base is relative to baseURI not to current module #20860

@dschmidt

Description

@dschmidt

Describe the bug

I think the behavior of relative paths for amd modules is inconsistent with other output formats and basically wrong.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions