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

Web worker inside npm package built with parcel #3959

Closed
ameerthehacker opened this issue Jan 4, 2020 · 9 comments
Closed

Web worker inside npm package built with parcel #3959

ameerthehacker opened this issue Jan 4, 2020 · 9 comments

Comments

@ameerthehacker
Copy link
Contributor

Hey how I can embed web workers inside a npm package. The problem Im facing is when I import my npm package for a demo it is trying to dowload the web worker from the public path of the dev server which does not exists

@mischnic
Copy link
Member

mischnic commented Jan 4, 2020

Parcel will automatically parse new Worker("./myworker.js") calls, both inside and outside of node_modules.
Please provide a concrete code example if you need more help.

@ameerthehacker
Copy link
Contributor Author

This is the place in my npm library I'm using worker and it gets bundled in the dist

Screenshot 2020-01-05 at 4 57 19 PM

but when I use it in another parcel project by installing my library(with worker) it says it cannot find worker.js in my new project
Screenshot 2020-01-05 at 4 57 47 PM

@beautyfree
Copy link

Same

@mischnic
Copy link
Member

Can someone provide a code sample where this fails? Seems to work for me in a simple example.

(@lgarron ?)

@github-actions github-actions bot removed the Stale Inactive issues label Jan 30, 2021
@lgarron
Copy link

lgarron commented Jan 30, 2021

Can someone provide a code sample where this fails? Seems to work for me in a simple example.

(@lgarron ?)

https://github.com/lgarron/parcel-2-web-worker-package-import

esm-import fails with Uncaught SyntaxError: Cannot use 'import.meta' outside a module
(See https://github.com/lgarron/web-worker-compat-problems/#problem-7-calculating-relative-paths-requires-importmetaurl, #5473, #5430)

cjs-import fails with Uncaught SyntaxError: Unexpected token '<' getting served HTML where JS was expected. This is because the published package uses __dirname but not inline (https://unpkg.com/browse/[email protected]/dist/cjs/scrambles.js), which causes the absolute disk file path to be used as the worker file path on the dev server origin.

I can publish more package versions with other combinations of workarounds. :-P

@lgarron
Copy link

lgarron commented Jan 30, 2021

the published package uses __dirname but not inline

In particular, note that this is required for node-compatible CJS code that instantiates a worker. The worker instantiation is not inline because we need to conditionally import the Worker implementation on node ("conditionally" because we need to avoid trying to load it in a browser).

(Also note that the current implementation is not Windows-compatible, because I hard-coded the slash direction. There would normally be another level of abstraction for cross-platform node compatibility, using path.join().)

@lgarron
Copy link

lgarron commented Jan 30, 2021

For what it's worth, I'd be happy to drop CJS support and just publish/consume:

new Worker(new URL("./worker.js", import.meta.url));

Which I think should stay intact when building a library target, but would be appropriate for Parcel to compile to non-ESM code for browser targets while Firefox and Safari still don't support module syntax for workers. (Browsers that syntax error if import.meta.url appears anywhere in the syntax tree of a non-module script, even if it appears in a code path that would not be reached.)

I don't know any remotely reasonable way to publish a CJS build with worker instantiation code that works in node (including Windows) and browsers, and can also be handled by most bundlers. I believe web-worker is supposed to help with this, but I never got it to work.

@mischnic
Copy link
Member

Ok, so all of that should be covered by #5430

-> #5430 (comment)

@jeffpeck10x
Copy link
Contributor

Not necessarily related, but as a heads up to anybody who finds this and it is not already obvious to, you need { type: "module" } when making a web worker:

    const worker = new Worker(
      new URL("./workers/my-worker.ts", import.meta.url)
      { type: "module" }
    );

See here:
#6261

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

5 participants