ilearnio / module-alias

Register aliases of directories and custom module paths in Node
MIT License
1.76k stars 69 forks source link

Make module-alias work in cli mode #76

Closed Kehrlann closed 5 years ago

Kehrlann commented 5 years ago

Fixes #65

It builds on top of @dgobaud's PR, and adds tests.

There might be some rare cases when you want to use module-alias while running in a REPL (e.g. $ node -i). In that case, require.main is undefined.

This means introducing a way of having "require.main" undefined in our module. The require function is different between the test module and the module under test, so we can't just do require.main = null in the test function, as it won't affect the "require" function of the prod code. So we need to introduce a helper function for evaluating require.main, and inject a flag in the test module when running the tests.

ilearnio commented 5 years ago

Awesome stuff! Thanks @Kehrlann!

ilearnio commented 5 years ago

Published as v2.2.2