Skip to content
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

feat(resolve): add fallback path to be able to resolve modules relatively to babel-plugin-macros #138

Merged
merged 2 commits into from
Dec 1, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ function createMacro(macro, options = {}) {
}

function nodeResolvePath(source, basedir) {
return resolve.sync(source, {basedir})
return resolve.sync(source, {
basedir,
paths: [p.resolve(__dirname, '../../')]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we include process.cwd() in this as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because it is a fallback only. If it cannot resolve relatively to basedir using node's built in resolve mechanism, then it tries with the provided paths as well (in the fallback case there is no travesing up to the parent, that's why ../../ is necessary).

I was using with process.cwd() included tho, but removing it didn't cause any issues. That case can only happen if plugin is installed outside of cwd and basedir is also not with-in cwd, which is highly unlikely imo. Should I add it just for safety?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My use case is that I have my devtools globally installed, so at project level I basically don't have any node_modules folder. This means babel-plugin-macros also being installed globally.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's pretty odd. But as this doesn't break anything I'm fine with it.

})
}

function macrosPlugin(
Expand Down