Giter Site home page Giter Site logo

Comments (5)

literat avatar literat commented on May 28, 2024 2

Ok, understand the problem with the major breaking change.

So I picked your HTMLReactParser function and implemented it on my side with the named imports from the html-dom-parser and html-react-parser/lib/dom-to-react.

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 :-)

from html-react-parser.

remarkablemark avatar remarkablemark commented on May 28, 2024

@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');

from html-react-parser.

literat avatar literat commented on May 28, 2024

I am afraid that this will not work when the default export will be present. I have tried to import attributesToProps which are present in a package like import { attributesToProps } from 'html-react-parser';. This was transformed into var htmlReactParser = require('html-react-parser'); and htmlReactParser.attributesToProps(); in CJS. Again, when I used the package build with this I got the error TypeError: htmlReactParser__namespace.default is not a function.

Neither will using import { default as parser } from 'html-react-parser';. Again it will end up as var parser = require('html-react-parser');.

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 export default Something will be transformed into exports.default = Something.

However, the named export should be working when the default will be removed. I have tried to tweak the package in the node_modules and for instance exports.htmlReactParser = HTMLReactParser; is working just fine. Same as for export const htmlReactParser = HTMLReactParser;

So then when I use import { htmlReactParser } from 'html-react-parser'; as you suggested, it will work.

from html-react-parser.

remarkablemark avatar remarkablemark commented on May 28, 2024

@literat Unfortunately I don't think I'm able to remove the default export for the time being as that would constitute a major breaking change for everybody.

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 html-dom-parser and dom-to-react separately.

from html-react-parser.

remarkablemark avatar remarkablemark commented on May 28, 2024

@literat Nice solution! Glad that the workaround is working.

from html-react-parser.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.