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:
- Created 3 years ago
- Reactions:1
- Comments:8 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

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.
This has been supported for a while.