new URL
broken when using alias or subpath imports together with template
#18676
Labels
new URL
broken when using alias or subpath imports together with template
#18676
Describe the bug
Summary:
code like
does not work as expected and simply return undefined.
Issue
This code
is converted to
Unfortunately this does not work because
import.meta.glob
return an object where the keys are the path of the resolved assets from the project root (because it interpret the template as an 'absolute path' as it does not start with a.
) while the 'resolution' key is the non transformed template string.Solutions:
I wrote 2 possible solutions, please let me know which one is the preferred one (or suggest an alternative approach) and i'll raise a PR.
Solution 1:
modify import.meta.glob to 'remap' keys to the original 'query'
this is done by looking at the input globs and at their resolution and by applying the 'reverse' transformation to the resolved files.
if this is the preferred option it's possible to leave the 'current' keys in place in order to not make this a breaking change.
or could add an option to import.meta.resolve, like
remapToGlob
and apply that transformation only if it's true.at this point the new URL code works because the expected key is actually defined
this is probably the best solution as otherwise import.meta.glob , even when used 'directly', returns unexpected results if used together with aliases/subpathImports
Solution 2:
leave import.meta.glob as it is but modify 'access' key.
this is done by resolving the glob pattern (in the example
#images/**/image.png
to the full path (let's say/root/dev/project/path/to/images/**/image.png
, converting it to a path relative to the project root (/path/to/images/**/image.png
), then swapping the prefixes (anything before the first**
in the resolved relative path and everythign before the${
in the template string )that generates
another related bug
There's an error in the resolve plugin when resolving subpath imports:
this should instead be
that's because resolveSubpathImports returns globs relative to the importer, not to the project root (
vite/packages/vite/src/node/plugins/resolve.ts
Line 511 in cf813cc
Reproduction
https://stackblitz.com/edit/vitejs-vite-3mn974
Steps to reproduce
No response
System Info
Used Package Manager
npm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: