tleunen / babel-plugin-module-resolver

Custom module resolver plugin for Babel
MIT License
3.46k stars 205 forks source link

fix: https://github.com/tleunen/babel-plugin-module-resolver/issues/381 #382

Closed Banou26 closed 4 years ago

Banou26 commented 4 years ago

Moves the relative path conditional under the resolvers and skip it if there is a resolver result

Add corresponding test

https://github.com/tleunen/babel-plugin-module-resolver/issues/381

Banou26 commented 4 years ago

There are warnings thrown even though the tests pass, can someone check if it's important or not ?

Banou26 commented 4 years ago

@tleunen so, i find myself in a bit of a problem, there's a test called

module-resolver › alias › with alias for a relative path (with respect to the cwd) › should alias the file path sharing a directory

which completly prevent the use of alias as extension rewriter since then all of the paths are rewritten relative to the cwd.

Any ideas to be able to have both behaviors ? Is it okay to introduce a flag to prevent that path rewriting ?

tleunen commented 4 years ago

I don't see any failing tests?

So basically, this PR mostly introduces the resolution on all paths, not just "absolute" paths. I remember we talked about this in a few other issues but I can't find them. Some people already wanted this (for us to remove the isRelativePath condition), but we never did that in fear of having bad performance since the plugin would now run everywhere and try to match aliases/root when they would not be needed most of the time. What do you think @fatfisz? Should we go and remove the condition? Or add a flag to run on all paths? (I'm not a huge fan of keeping adding flags for features too :/)

Banou26 commented 4 years ago

No tests are failing because i didn't make the breaking change/added a flag for the relative path bypass yet.

Right now the package doesn't allow to just rewrite .ts to .js, because the package will rewrite every alias paths relative to the cwd, for example,

project/
├── src/
│   ├── foo.ts
│   └── index.ts
└── package.json

./index.ts is transformed as such

- import './foo.ts'
+ import '../foo.js'

because of https://github.com/tleunen/babel-plugin-module-resolver/blob/a65c39a7e495328a9d4d70e82b567122d729e6cb/src/resolvePath.js#L79-L83

and to have the correct behavior i need, which is to only rewrite the extension, we'd have to introduce a flag to bypass the mapToRelative call, which would result in

- import './foo.ts'
+ import './foo.js'
Banou26 commented 4 years ago

Also, if you don't think your plugin should tackle this kind of behavior or don't want to add new flags i'm fine with it, i'll just fork it for my only need.

Banou26 commented 4 years ago

ended up using https://github.com/karlprieb/babel-plugin-add-import-extension to replace .ts import extension names by .js