Skip to content
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

Failed to import - html-dom-parser #577

Closed
MariusVatasoiu opened this issue Apr 1, 2023 · 2 comments
Closed

Failed to import - html-dom-parser #577

MariusVatasoiu opened this issue Apr 1, 2023 · 2 comments
Labels
browser Not working in Browser

Comments

@MariusVatasoiu
Copy link

MariusVatasoiu commented Apr 1, 2023

Failing module

import HTMLDOMParser from 'https://esm.sh/[email protected]/';

HTMLDOMParser('<p>Hello, World!</p>');

Error message

After onload I got this:

utilities.js:85 Uncaught TypeError: Element is not a constructor
    at formatDOM (utilities.js:85:28)
    at HTMLDOMParser2 (html-to-dom.js:34:8)

Additional info

  • esm.sh version: v113
  • Browser version: Chrome 111.0.5563.147 (but the error is present in any browser/version)

In investigated this issue and the problem seems to be how require() vs import/export works.

In domhandler/lib/esm/index.js we have:

import { ElementType } from "domelementtype";
import { Element, Text, Comment, CDATA, Document, ProcessingInstruction, } from "./node.js";
export * from "./node.js";
// Default options
const defaultOpts = {
    withStartIndices: false,
    withEndIndices: false,
    xmlMode: false,
};
export class DomHandler {...}

And in html-dom-parser/lib/client/utilities.js we have:

var domhandler = require('domhandler');
var constants = require('./constants');

var CASE_SENSITIVE_TAG_NAMES = constants.CASE_SENSITIVE_TAG_NAMES;

var Comment = domhandler.Comment;
var Element = domhandler.Element;
...
current = new Element(tagName, formatAttributes(node.attributes));
...

Cause / Solution

In html-dom-parser/lib/client/utilities.js var domhandler = require('domhandler'); acts like import domhandler from 'domhandler';, but it should act like import * as domhandler from 'domhandler';.

I don't have experience with how esm.sh transpiles the code from CJS to ES6, but this was my solution after I manually changed html-dom-parser/lib/client/utilities.js to ES6 and used import * as domhandler from 'domhandler'.

@MariusVatasoiu MariusVatasoiu added the browser Not working in Browser label Apr 1, 2023
@ije
Copy link
Member

ije commented Apr 2, 2023

thanks for the details...transplie cjs to esm is so hard cause edge cases... anyway i will fix it with your resolution

@MariusVatasoiu
Copy link
Author

The edge case here I think is this line:

export * from "./node.js";

When this is present, require() acts like an import * as foo.

If this is not present, it will act like an import foo.

I didn't completely test my supposition, but this was how it looked from my tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
browser Not working in Browser
Projects
None yet
Development

No branches or pull requests

2 participants