-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
fix(build): make relative imports in amd/cjs relative to the current module #20861
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
base: main
Are you sure you want to change the base?
Conversation
…module instead of the baseURI
951ec02
to
835e172
Compare
Would you provide concrete examples of |
Sure, no problem. I've used this code: const customRelativeUrlMechanisms = {
...relativeUrlMechanisms,
// override amd to use module.uri instead of document.baseURI
amd: (relativePath) => {
if (relativePath[0] !== '.') relativePath = './' + relativePath
return getResolveUrl(
`require.toUrl('${escapeId(relativePath)}'), (() => {
console.log('relativePath', '${relativePath}')
console.log('require.toUrl(relativePath)', require.toUrl('${relativePath}'))
console.log('module.id', module.id)
console.log('module.uri', module.uri)
console.log('document.baseURI', document.baseURI)
return new URL(module.uri, document.baseURI).href
})()`,
)
},
'worker-iife': (relativePath) =>
getResolveUrl(
`'${escapeId(partialEncodeURIPath(relativePath))}', self.location.href`,
),
} as const satisfies Record<string, (relativePath: string) => string> to produce this output:
|
I'm not sure about the baseUrl - is it possible to have multiple instances of requirejs?
that's only valid for a single app but not for all others. No idea why rollup implemented it this way. Maybe |
If you insist, I can also send a PR to Rollup and we can check their feedback 😅 |
Thank you for the examples and the PR to Rollup. I think it makes sense. Just in case, I'd like to wait for a week for the feedback on Rollup side. |
Makes sense. I understand that while being a small change in terms of LoC, the change could potentlally be delicate and I certainly don't want to break others. If rollup people see a problem, I'm happy to discuss alternatives with you and them. |
Description
make relative imports in amd/cjs relative to the current module instead of the baseURI.
I've added an override to
customRelativeUrlMechanisms
to avoid modifying the copied code from rollup.Fixes #20860
As far as I can tell rollup itself uses
module.uri
to replaceimport.meta.url
inamd
modules. So I assume it's okay to use it.https://github.com/rollup/rollup/blob/ce6cb93098850a46fa242e37b74a919e99a5de28/src/ast/nodes/MetaProperty.ts#L209
An alternative could be to use
resolve.toUrl(module.id)
but it seems unneccessary if other parts of the code already rely onmodule.uri
.FWIW I've verified that
import.meta.url
gets transpiled tonew URL(module.uri, document.baseURI).href
in my setup with amd modules. Usingimport.meta.url
directly for the imports here does not work as it happens too late in the transpilation process (getting errors thatmodule
may not be used outside of module scope).I'm a bit lost regarding a regression test as I couldn't find a test for the relative path behavior that I could simply adjust... if you require me to implement one, could you give me a few pointers how to implement this specific one?