vite icon indicating copy to clipboard operation
vite copied to clipboard

Import aliases stopped working from @marko/[email protected] onwards

Open vwong opened this issue 9 months ago • 5 comments

Version: 4.1.5 - 4.1.10

I'm trying to use import aliases https://vitejs.dev/config/shared-options#resolve-alias.

This was working fine in @marko/[email protected] and earlier, but is broken in every version from 4.1.5 onwards.

A minimal reproduction repo is available https://github.com/vwong/marko-vite-bug

vwong avatar Apr 28 '24 23:04 vwong

@vwong FYI the issue isn't that import aliases are not working, it's specifically that ~ doesn't work as an alias anymore since we add a default one here https://github.com/marko-js/vite/blob/main/src/index.ts#L364.

The reason this was added was to (somewhat hackily) improve support with webpack and lasso based apps which for less/css files resolve ~ using the bundlers resolution. Vite doesn't handle ~ in the same way as the others and so we made @marko/vite normalize this for you.

If you change your alias from eg ~ to @ it should work fine.

However I think we could update this alias to be less aggressive, eg /^~(?!\/)/ which would allow your specific remap to slide through.

DylanPiercey avatar Apr 29 '24 22:04 DylanPiercey

I'll also add that it may make more sense for you to use package json imports for this since it's essentially intended to replace a lot of this aliasing stuff.

Eg

"imports": {
  "#app/*": "./src/*.ts"
}

and then

import "~/thing";
// becomes
import "#app/thing";

DylanPiercey avatar Apr 30 '24 15:04 DylanPiercey

Thanks for the tip. I've can now use package.json imports with @marko/[email protected].

vwong avatar Apr 30 '24 20:04 vwong

Additional tip: Make sure to update tsconfig.json as well.

vwong avatar Apr 30 '24 20:04 vwong

@vwong do you have this configured? https://www.typescriptlang.org/docs/handbook/modules/reference.html#packagejson-imports-and-self-name-imports

DylanPiercey avatar Apr 30 '24 20:04 DylanPiercey

This issue is now resolved with package.json imports.

vwong avatar Jul 24 '24 00:07 vwong