Skip to content
Merged
Changes from all commits
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
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ function createMacro(macro, options = {}) {
}

function nodeResolvePath(source, basedir) {
return resolve.sync(source, {basedir})
return resolve.sync(source, {
basedir,
// This is here to support the package being globally installed
// read more: https://github.com/kentcdodds/babel-plugin-macros/pull/138
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