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

Add Deno support #181

Open
Stephan-C opened this issue Jan 20, 2022 · 8 comments
Open

Add Deno support #181

Stephan-C opened this issue Jan 20, 2022 · 8 comments

Comments

@Stephan-C
Copy link

Expected Behavior

html-dom-parser uses a server dom parser implementation on deno, similar to nodejs. But with guards in place if there is no document defined.

Actual Behavior

Throwing exception that document.implementation doesn't exist. There is no document defined on deno.

Steps to Reproduce

Import html-dom-parser on deno. In my case I used a library that uses html-react-parser, which uses html-dom-parser underneath.

Reproducible Demo

Save this to a file like test.js and then run it on deno: deno run test.js

import parse from 'https://cdn.skypack.dev/html-dom-parser';

console.log(parse('<p class="foo" style="color: #bada55">Hello, <em>world</em>!</p>'));
@remarkablemark
Copy link
Owner

@Stephan-C html-dom-parser should be loading htmlparser2 on the server-side, which doesn't use document.

Does Deno end up using the browser field from package.json?

@Stephan-C
Copy link
Author

Interesting. Yeah, I guess it must be loading the browser field, which might be caused by how the package is imported into deno, I have tried skypack, esm.sh and jspm.

@Stephan-C
Copy link
Author

Stephan-C commented Feb 4, 2022

I did some more investigation and came across the following: Looks like Rollup sets the browser one as the default and node is separate. Hence why Deno is loading the browser version. The only reference I could find to something similar was here rollup/rollup#3634 (comment)

I sort of found a workaround, but I don't know how to apply this exclusively to html-dom-parser, for a library that depends on html-dom-parser.
esm.sh allows you to set a target.
This works in deno:

import parse from 'https://esm.sh/html-dom-parser?target=node';

console.log(parse('<p class="foo" style="color: #bada55">Hello, <em>world</em>!</p>'));

So the real question is, how can we configure rollup to serve the server version for deno targets and not the browser version.

@remarkablemark
Copy link
Owner

remarkablemark commented Feb 4, 2022

I was able to reproduce the error in https://replit.com/@remarkablemark/html-dom-parser-181#index.ts

I believe a potential fix would be to add ES Module support for this package. See https://unpkg.com/html-dom-parser@1.0.4?module:

Package html-dom-parser@1.0.4 does not contain an ES module

The approach will be similar to:

@remarkablemark
Copy link
Owner

remarkablemark commented Feb 5, 2022

Added ESM support for html-dom-parser in #203 (comment)

@remarkablemark
Copy link
Owner

remarkablemark commented Feb 5, 2022

Upgraded html-dom-parser in remarkablemark/html-react-parser#444 (comment)

@Stephan-C
Copy link
Author

Nice. Unfortunately, it still loads the browser version in deno if you try the new 1.1.0 version.

@remarkablemark
Copy link
Owner

remarkablemark commented Feb 5, 2022

Got it, then I'm not exactly sure what can be done to resolve this.

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

No branches or pull requests

2 participants