Skip to content

Add a 'webworker' field for bundler #14681

@stevemk14ebr

Description

@stevemk14ebr

Feature request

What is the expected behavior?
A target of webworker should first look for a webworker field, rather than browser first as it currently is.

What is motivation or use case for adding/changing the behavior?

from: https://webpack.js.org/configuration/resolve/#resolvemainfields
When importing from an npm package, ... this option will determine which fields in its package.json are checked. The
default values will vary based upon the target specified in your webpack configuration. When the target property is set to webworker, web, or left unspecified the following fields are checked in order
mainFields: ['browser', 'module', 'main'],

some libraries guard usage of keywords like document or window behind bundler fields like browser. This is important for code minification and switching implementation APIs depending on the final environment. There currently is no default field to describe the environment web workers live in. They are mostly the browser environment but without access to DOM apis, which is an important distinction. Trying to using a library that uses DOM apis in a web worker will fail. Currently library authors have no way to specify browser vs webworker difference.

Please see wooorm/parse-entities#19 for a case where this matters. This library uses document.createElement for the browser implementation, and has an alternative field react-native for non-browser API implementation. By default webpack uses the browser field which causes webworkers to bundle incorrectly. There is no way for the library author to fix this, as there is no default webworker field that is tried first when the target is set to webworker. Here is where the library defines it's two fields to switch implementation depending on the bundler target

How should this be implemented in your opinion?
A new field webworker should be introduced, and checked for before browser when the target webworker is used. Please see https://webpack.js.org/configuration/resolve/#resolvemainfields which documents the current order as browser, then module, then main.

Are you willing to work on this yourself?
no, sorry.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions