tleunen / babel-plugin-module-resolver

Custom module resolver plugin for Babel
MIT License
3.44k stars 203 forks source link

proxyquire relative path issue #263

Open hualu00 opened 6 years ago

hualu00 commented 6 years ago

Bellowing is my original code sample:

const ProxyInboxModel = proxyquire('../model.js', {
        'messages': {
          ...
        }
}).default

It didn't work as 'messages' is not resolved by module-resolver, after reading this great explanation,
https://github.com/tleunen/babel-plugin-module-resolver/blob/master/DOCS.md#usage-with-proxyquire
I've changed my code to this:

const resolvePath =  c => c
const ProxyInboxModel = proxyquire('../model.js', {
       [ resolvePath('messages')]: {
          ...
        }
}).default

This time 'messages' was resolved, but with the wrong path. And the root cause is I use proxyquire in a test file, and this test file is not at the same folder with its test-ee.
Let's say
'a/b/c.js' and 'a/b/test/c-test.js'
in 'a/b/c.js', 'messages' is resolved as '../../path/messages',
but in 'a/b/test/c-test.js' , it is '../../../path/messages'

tleunen commented 6 years ago

Did you also add the function to transformFunctions?

Shouldn't your code be like this?

const resolvePath =  c => c
const ProxyInboxModel = proxyquire('../model.js', {
       [ resolvePath('../model.js')]: {
          ...
        }
}).default

I'm not familiar with proxyquire, so I'm just trying to find the error with you :)

patrickleet commented 3 years ago

seeing this in a project I inherited and am fixing up - moving the test to the same level as the file fixed it - not ideal, but works