question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WebWorkers: Support new Worker( new URL() ) syntax

See original GitHub issue

🙋 feature request

As of v5 Webpack has introduced support for WebWorkers with the following syntax:

const worker = new Worker( new URL( './worker.js', import.meta.url ) );

This is the right way to load a Worker with a relative path from a module. However this doesn’t work in Parcel. It would be great if parcel supported this for Workers (or parsing new URL in general) to support this use case so packages can be written and used consistently across bundlers.

🤔 Expected Behavior

The following should instantiate a new Worker next to the current module file.

const worker = new Worker( new URL( './worker.js', import.meta.url ) );

😯 Current Behavior

The worker is loaded from the wrong path.

💁 Possible Solution

Account for new URL( <path>, import.meta.url ) when processing Workers.

🔦 Context

My valid, browser-runnable code cannot be built with Parcel but can be built with Webpack. I would like to be able to write code that works consistently across browsers and bundlers.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
devongovettcommented, Jan 30, 2021

We should possibly drop the second syntax (what we currently support). In the browser, relative urls resolve from the page url not the script, so parcel currently doesn’t match.

0reactions
devongovettcommented, Aug 3, 2021

This has been supported for a while.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Web Workers - Web APIs - MDN Web Docs
Spawning a dedicated worker. Creating a new worker is simple. All you need to do is call the Worker() constructor, specifying the URI...
Read more >
Web Workers | webpack - JS.ORG
As of webpack 5, you can use Web Workers without worker-loader . Syntax. new Worker(new URL('./worker.js', import.meta.url)); // or customize the chunk name ......
Read more >
How Web Workers Work in JavaScript – With a Practical JS ...
You can create a web worker using the following syntax: const worker = new Worker("<worker_file>.js");. Worker is an API interface that lets ...
Read more >
Loading web workers using Webpack 5 · mmazzarolo.com
meta object (an object that exposes context-specific metadata) to provide the module URL to the Worker() constructor: const myWorker = new ...
Read more >
Introducing WebSockets - Bringing Sockets to the Web
Introducing Web Workers: Bring Threading to JavaScript # ... If the specified file exists, the browser will spawn a new worker thread, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found