Skip to content

Commit

Permalink
Fix(web-react): Add backward compatibility for CJS when building to it
Browse files Browse the repository at this point in the history
  * there is a problem in both packages of how they expose default export and
    how the default export is transpiled by the bundler
  * so better to check it if the function is defined on the `default` or not

refs #DS-1227
  • Loading branch information
literat committed Apr 22, 2024
1 parent 2788887 commit 7a35154
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions packages/web-react/src/utils/htmlReactParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
*
* @see { @link https://github.com/remarkablemark/html-react-parser/issues/1329 }
*/
import htmlToDOM from 'html-dom-parser';
import domToReact from 'html-react-parser/lib/dom-to-react';
import htmlDomParser from 'html-dom-parser';
import domToReactLib from 'html-react-parser/lib/dom-to-react';

export const htmlReactParser = (html: string): ReturnType<typeof domToReact> => {
export const htmlReactParser = (html: string): ReturnType<typeof domToReactLib> => {
if (typeof html !== 'string') {
throw new TypeError('First argument must be a string');
}
Expand All @@ -16,5 +16,11 @@ export const htmlReactParser = (html: string): ReturnType<typeof domToReact> =>
return [];
}

// support backwards compatibility for ES Module
// @ts-expect-error Property 'default' does not exists on type -- exactly, we need to check it first
const htmlToDOM = typeof htmlDomParser.default === 'function' ? htmlDomParser.default : htmlDomParser;
// @ts-expect-error Property 'default' does not exists on type -- exactly, we need to check it first
const domToReact = typeof domToReactLib.default === 'function' ? domToReactLib.default : domToReactLib;

return domToReact(htmlToDOM(html));
};

0 comments on commit 7a35154

Please sign in to comment.