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

Cannot run inside WebWorker #60

Closed
4 tasks done
siefkenj opened this issue Oct 1, 2023 · 7 comments
Closed
4 tasks done

Cannot run inside WebWorker #60

siefkenj opened this issue Oct 1, 2023 · 7 comments
Labels
🙋 no/question This does not need any changes 👎 phase/no Post cannot or will not be acted on

Comments

@siefkenj
Copy link

siefkenj commented Oct 1, 2023

Initial checklist

Affected packages and versions

2

Link to runnable example

No response

Steps to reproduce

In a browser console, run

response = `import {toMarkdown} from 'https://esm.sh/mdast-util-to-markdown@2'; console.log("hi there")`;
blob = new Blob([response], {type: 'application/javascript'});
worker = new Worker(URL.createObjectURL(blob), {type: "module"});

Expected behavior

"hi there" should be printed to the console.

Actual behavior

An error

index.dom.js:5 Uncaught ReferenceError: document is not defined
    at index.dom.js:5:17

is displayed.

Affected runtime and version

Firefox 118 / Chrome 116

Affected package manager and version

No response

Affected OS and version

No response

Build and bundle tools

No response

@github-actions github-actions bot added 👋 phase/new Post is being triaged automatically 🤞 phase/open Post is being triaged manually and removed 👋 phase/new Post is being triaged automatically labels Oct 1, 2023
@ChristianMurphy
Copy link
Member

ChristianMurphy commented Oct 2, 2023

Welcome @siefkenj! 👋
Sorry you ran into a spot of trouble.
A few things to note here:

  1. this error does not come from mdast-util-to-markdown itself, running your example in Firefox traces it back to it's actual root
    ReferenceError: document is not defined [decode-named-character-reference.mjs:2:7](https://esm.sh/v132/decode-named-character-reference@1.0.2/es2022/decode-named-character-reference.mjs)
    <anonymous> https://esm.sh/v132/decode-named-character-reference@1.0.2/es2022/decode-named-character-reference.mjs:2
    
  2. decode-named-character-reference does offer a worker version https://github.com/wooorm/decode-named-character-reference/blob/4347630741f82d7d4c7fb38763ba158951b2a1be/package.json#L36, the issue is with your configuration/build tool for not choosing the worker version (ref: Using document to parse entities makes the lib (and deps) unusable in web workers wooorm/parse-entities#19)
  3. esm.sh already supports this with the ?worker option documented in the readme https://github.com/esm-dev/esm.sh#web-worker
  4. If you are experience an issue with esm.sh's ability to build the code, please report it to them

@ChristianMurphy ChristianMurphy closed this as not planned Won't fix, can't repro, duplicate, stale Oct 2, 2023
@ChristianMurphy ChristianMurphy added the 🙋 no/question This does not need any changes label Oct 2, 2023
@github-actions

This comment has been minimized.

@github-actions github-actions bot added 👎 phase/no Post cannot or will not be acted on and removed 🤞 phase/open Post is being triaged manually labels Oct 2, 2023
@siefkenj
Copy link
Author

siefkenj commented Oct 2, 2023

Thank you for the information. It would be really nice if an error with a little more information could be thrown in the case of a misloaded module :-).

As per your suggestion, I tried

response = `import {toMarkdown} from 'https://esm.sh/mdast-util-to-markdown@2?worker'; console.log("hi there")`;
blob = new Blob([response], {type: 'application/javascript'});
worker = new Worker(URL.createObjectURL(blob), {type: "module"}); 

but it still errors...

@remcohaszing
Copy link
Member

decode-named-character-reference uses the worker export condition. This is not the same as loading a module as a worker using the ?worker query parameter.

The problem is that esm.sh does resolve the browser condition, but not the worker condition. You can make it do so using https://esm.sh/mdast-util-to-markdown@2?conditions=worker

I think it’s a great idea to use the worker condition, but it’s not standardized. Maybe we should propose it to https://runtime-keys.proposal.wintercg.org.

@ChristianMurphy
Copy link
Member

ChristianMurphy commented Oct 2, 2023

@siefkenj I'll repeat again context matters, please include it when asking for support.
"it still errors" or "it doesn't work" gives zero context.
Share a stacktrace, the browser you are using, and ideally a runnable example in a sandbox.


response = `import {toMarkdown} from 'https://esm.sh/mdast-util-to-markdown@2?worker'; console.log("hi there")`;
blob = new Blob([response], {type: 'application/javascript'});
worker = new Worker(URL.createObjectURL(blob), {type: "module"}); 

I'm not sure what to tell you.
I ran this code in FireFox 118 and Chrome 117 and neither threw an error.
Consider using a modern and up-to-date browser if you aren't already, and make sure that you are running in a clean test environment.

In addition consider adding @remcohaszing's suggestion above to ensure the condition is picked up.

@siefkenj
Copy link
Author

siefkenj commented Oct 3, 2023

I'm sorry for not including enough context. I am running Firefox 118 and Chrome 116 (as stated in the original bug report) on Linux. I am running these commands in the web console on the the page https://esm.sh/mdast-util-to-markdown@2.1.0.

The code

response = `import {toMarkdown} from 'https://esm.sh/mdast-util-to-markdown@2?worker'; console.log("hi there")`;
blob = new Blob([response], {type: 'application/javascript'});
worker = new Worker(URL.createObjectURL(blob), {type: "module"}); 

should print "hi there" to the console if it executes correctly. However "hi there" is not printed in Firefox or Chrome. No error message is given to me in either browser, but importing a worker-safe module makes "hi there" be printed to the console. For example:

response = `import React from "https://esm.sh/react@18.2.0";\n console.log("hi there")`;
blob = new Blob([response], {type: 'application/javascript'});
worker = new Worker(URL.createObjectURL(blob), {type: "module"}); 

Changing the import line import {toMarkdown} from 'https://esm.sh/mdast-util-to-markdown@2?worker' to import {toMarkdown} from 'https://esm.sh/mdast-util-to-markdown@2?conditions=worker' does cause an error Uncaught ReferenceError: document is not defined to be printed in Firefox and Chrome. And "hi there" is still not printed.

@ChristianMurphy
Copy link
Member

import {toMarkdown} from 'https://esm.sh/mdast-util-to-markdown@2?conditions=worker' does cause an error Uncaught ReferenceError: document is not defined to be printed in Firefox and Chrome.

This is a bug in esm.sh then, it is not applying the condition as requested

response = `import {toMarkdown} from 'https://esm.sh/mdast-util-to-markdown@2?worker';
console.log("hi there")`;
blob = new Blob([response], {type: 'application/javascript'});
worker = new Worker(URL.createObjectURL(blob), {type: "module"}); 

should print "hi there" to the console if it executes correctly. However "hi there" is not printed in Firefox or Chrome. No error message is given to me in either browser, but importing a worker-safe module makes "hi there" be printed to the console.

Please read the docs that were shared on ?worker https://github.com/esm-dev/esm.sh#web-worker
esm.sh's worker mode has already placed the code inside a webworker, and returns a worker factory as the default export.
You are pulling from a named export, that does not exist in esm.sh's worker mode.


To reiterate from the first response

If you are experience an issue with esm.sh's ability to build the code, please report it to them

https://github.com/esm-dev/esm.sh/issues is their issue tracker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🙋 no/question This does not need any changes 👎 phase/no Post cannot or will not be acted on
Development

No branches or pull requests

3 participants