Skip to content

Unable to set target for Web Workers #17512

@kingller

Description

@kingller

Bug report

What is the current behavior?

Unable to set target for Web Workers

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

My worker uses the package named decode-named-character-reference, the package exports as bellow,

"exports": {
    ".": {
      "deno": "./index.js",
      "react-native": "./index.js",
      "worker": "./index.js",
      "browser": "./index.dom.js",
      "default": "./index.js"
    }
  }

When I compile my website with webpack, the target is to use Defaults to browserslist, and it compile my web worker use its exports "browser": "./index.dom.js", the error happens ReferenceError: document is not defined.

If the current behavior is a bug, please provide the steps to reproduce.

What is the expected behavior?

How can I set my web worker's target to webworker separately?
I want to compile the package named decode-named-character-reference use its exports "worker": "./index.js", then it can work well.

Other relevant information:
webpack version: 5.73.0
Node.js version: 14.20.0
Operating System: macOS Big Sur
Additional tools: VS code

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions