Skip to content

module: add "worker" condition for Web Worker threads - #66635

Open
guybedford wants to merge 1 commit into
nodejs:mainfrom
guybedford:worker-condition
Open

guybedford wants to merge 1 commit into
nodejs:mainfrom
guybedford:worker-condition

Conversation

@guybedford

Copy link
Copy Markdown
Contributor

This adds a "worker" package condition which is enabled for Web Worker threads when created via the Worker global (--experimental-web-worker).

Web Workers currently inherit the parent's conditions verbatim, with no way for a package to provide a worker-specific entry point. Since Web Workers are a web platform API, a "worker" condition matching within them aligns Node.js with the bundler and runtime ecosystem.

  • worker is applied in Web Worker threads to the entry point, its static dependencies, import(), require() (classic workers and createRequire), and import.meta.resolve().
  • Threads created via worker_threads do not get the condition, including threads created from within a Web Worker; nested Web Workers retain it.
  • Async customization hooks registered from a Web Worker see worker in context.conditions on the hooks thread.
  • All other conditions (node, import, require, node-addons, module-sync, --conditions) are unaffected.

Internally this is tracked per-thread in internal/modules/helpers set from the worker bootstrap when webWorkerData is present, and spliced into both the ESM and CJS default conditions.

Test coverage in test/parallel/test-webworker-conditions.js covers all of the above cases, including the main thread and worker_threads not matching the condition. Docs updated in packages.md and globals.md.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/loaders

@nodejs-github-bot nodejs-github-bot added lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run. labels Oct 10, 2026
Adds a "worker" package condition that is applied within Web Worker
threads created via the `Worker` global, for the worker entry point,
its dependencies, `import()`, `require()` and `import.meta.resolve()`.
Threads created via `worker_threads`, including from within a Web
Worker, do not apply the condition. Other conditions are unaffected.

Assisted-by: OpenCode
Signed-off-by: Guy Bedford <guybedford@gmail.com>
@codecov

codecov Bot commented Oct 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.43%. Comparing base (ae5a0f4) to head (f38c589).

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #66635      +/-   ##
==========================================
- Coverage   92.78%   90.43%   -2.35%     
==========================================
  Files         422      791     +369     
  Lines      193692   276617   +82925     
  Branches    29881    53130   +23249     
==========================================
+ Hits       179718   250162   +70444     
- Misses      13645    16864    +3219     
- Partials      329     9591    +9262     
Files with missing lines Coverage Δ
lib/internal/main/worker_thread.js 95.04% <100.00%> (+18.14%) ⬆️
lib/internal/modules/esm/hooks.js 86.09% <100.00%> (+0.14%) ⬆️
lib/internal/modules/esm/utils.js 99.51% <100.00%> (+13.66%) ⬆️
lib/internal/modules/esm/worker.js 92.90% <100.00%> (+0.11%) ⬆️
lib/internal/modules/helpers.js 98.78% <100.00%> (+8.57%) ⬆️

... and 497 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants