-
-
Notifications
You must be signed in to change notification settings - Fork 133
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
Incompatible default exports between ESM and CJS #1329
Comments
@literat If I added a named export, would that solve your issue? For example: import { htmlToReact } from 'html-react-parser'; Which is the same as: const { htmlToReact } = require('html-react-parser'); |
I am afraid that this will not work when the Neither will using I have tried to simulate what for instance does Babel with this code and it looks like it treats it the same way. So every However, the named export should be working when the So then when I use |
@literat Unfortunately I don't think I'm able to remove the Since this library exports both ESM and CJS, I wonder if there's an import strategy that works for your use case. Perhaps all you need is to import |
Ok, understand the problem with the major breaking change. So I picked your import htmlToDOM from 'html-dom-parser';
import domToReact from 'html-react-parser/lib/dom-to-react';
export const htmlReactParser = (html: string): ReturnType<typeof domToReact> => {
if (typeof html !== 'string') {
throw new TypeError('First argument must be a string');
}
if (!html) {
return [];
}
return domToReact(htmlToDOM(html));
}; It is a little bit simplified but this seems to be working. It is still a workaround so I hope you will be able to address this problem in some future major release. Thank you for your suggestions, they help a lot :-) |
@literat Nice solution! Glad that the workaround is working. |
Hi,
I have encountered a problem in my package while using
html-react-parser
. For context, I am building and maintaining a design system react package where we use your package to render SVG icons.We are providing our package in both the CommonJS and Ecma Script module.
The package is written in Typescript using ESM, so we are importing this package as
import htmlReactParser from 'html-react-parser'
. The problem is when we transpile the code from the Typescript to ESM and then to the CJS, it looks likevar htmlReactParser = require('html-react-parser')
. That is the problem because when used for example in the Next.js app there is an errorhtmlReactParser is not a function
. According to your documentation, we should usevar htmlReactParser = require('html-react-parser').default
.I have created a workaround (lmc-eu/spirit-design-system#1289) where I just post-process the CJS files and replace the broken code. Because I have not found any way how to tell the Rollup to treat the transformation that way.
However, I have looked into the exported code of this package and saw that your build process creates
exports.default = ...
. Whenmodule.exports = ...
will be used instead it will be working and there will be no need forrequire('html-react-parser').default
, I think.I would appreciate it if there would be a way to make the exports more compatible. I would also appreciate any advice given that could help me to set up a build process the correct way. Thanks :-)
Also, thanks for this package :-). It helped us a lot with rendering the icons both on the client and the server side 👍
Question
Can be the default import/require same in both CommonJS and EcmaScriptModule
Keywords
commonjs, ecmascript, module, default exports, build
The text was updated successfully, but these errors were encountered: