asapach / babel-plugin-rewire-exports

Babel plugin for stubbing [ES6, ES2015] module exports
MIT License
66 stars 7 forks source link

export functional component is imported as _[component_name] #20

Closed adl-g closed 5 years ago

adl-g commented 5 years ago

I encounter a problem when trying to use named export / default export of React functional component.

it only happens if I export and define my component at the same line. it only happens if I export functional component, not occur for class component.

if i export my component this way:

export function Foo() (
    <div>...</div>;
);

or with default export this way:

export default function Foo() (
    <div>...</div>;
);

then my tests fails because they try to find a component with the name Foo, but the component is imported as _Foo so the render output is: <_Foo>...</_Foo> btw, I'm using Mocha, Chai, Enzyme

but if I export the component after its declaration like this:

function Foo() (
    <div>...</div>;
);
export { Foo }

or with default export this way:

function Foo() (
    <div>...</div>;
);
export default Foo;

then my tests work as expected and Foo is rendered as <Foo>

Is there something I missed? Can you fix it?

Thanks a lot!

sergei-startsev commented 5 years ago

The original names should be exported for both cases (default and named exports), it's covered by following tests: transform-named-export-function and transform-default-export-named-function. As you can see from the output it respects the original names:

export { foo };
export { foo as default };

Could you create a repo to reproduce your issue? it will help us to investigate the issue.

adl-g commented 5 years ago

I created a repository for it: https://github.com/ADL-dev/babel-plugin-rewire-exports-issue-20

see comments in src/Title.js. uncomment the second version in Title.js file and you will see the tests failing.

asapach commented 5 years ago

@ADL-dev, your example is missing .babelrc, but I got it working. Thanks, I'll take a look.

adl-g commented 5 years ago

oh sorry, added .babelrc file now

asapach commented 5 years ago

@ADL-dev, could you please try the new pre-release version? npm install -D babel-plugin-rewire-exports@alpha

I'll release a new stable version once I run more tests on it.

adl-g commented 5 years ago

@asapach I tried it and it is working great! thanks you!