ESM default export + CommonJS transpilation
Nowadays opened this issue · comments
Hi, it's me again;
I may have spoken too soon when I said it wasn't a deal breaker 😬
In a node ESM project, if Jest is used as the test framework, you need to transpile the code to CommonJS because Jest doesn't support Esm as of now.
But due to the fact that the exported function is imported as a default
property of the imported object, once you transpile the code it ends up like this:
Which of course doesn't work. Since I've never used Typescript I don't know how to setup the compiler to export the function as default
and not as a default
property of the imported object.
My best bet is that you know how to fix this 😂
Hi, thanks for the contribution! I'll take a look.
you need to transpile the code to CommonJS because Jest doesn't support Esm as of now.
Have you tried to exclude node_modules
from transpilation? It seems that module was double transpalied.
Well, the issue is that in a native ESM env doing:
import {default as axiosBetterStacktrace } from 'axiosBetterStacktrace';
is actually the equivalent of:
import axiosBetterStacktrace from 'axiosBetterStacktrace';
After importing the module, the function must be used as follow:
axiosBetterStacktrace.default(instance);
So when it gets transpiled it ends up as:
_axiosBetterStacktrace.default.default(instance);
which of course doesn't work.
There is no double transpilation
The fastest way and less intrusive way to fix this issue would be to replace:
export default axiosBetterStacktrace
to export = axiosBetterStacktrace
; in src/axiosBetterStacktrace.ts
Where the output would be:
module.exports = axiosBetterStacktrace;
which works both in CJS and ESM, and would not require using axiosBetterStacktrace.default
in an ESM env
I can edit the PR to do that, it's the best of both world
One downside that I noticed is that TS projects won't be able to use this plugin exported as export = axiosBetterStacktrace
without esModuleInterop
enabled, and approach in 47fe367 makes it work OOTB which is nice.
Let me know if v2.1.0 version works for you as expected.