Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make Worker constructors configurable #16422

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Danielku15
Copy link

Description

This PR addresses some first parts of #16128

Background

As described in the issue the ultimate goal is to make the Web Worker generation reusable from other plugins. This way a custom plugin for a specific library can tell vite more specifically to generate workers for this specific library.

In preparation to such flexibility, this PR adds the possibility to use alternative constructors than Worker and SharedWorker.

Use Cases

  1. It allows developers to adjust which type constructors are picked up as web workers. This can be useful when 3rd party libraries offer worker-alike types but during the import the constructor might get renamed. The custom aliases can also have benefits fro mocking and testing purposes.
// vite.config.js
// worker: { constructors: [ { constructor: 'NodeWorkerThreads', kind: 'worker' },  { constructor: 'ThreadsWorker', kind: 'worker' } ]

import { Worker as NodeWorkerThreads } from 'worker_threads';
import { Worker as ThreadsWorker } from 'threads'; // https://www.npmjs.com/package/threads

const nodeWorker = new NodeWorkerThreads(new URL('./worker.js', import.meta.url));
const threadsWorker = new ThreadsWorker(new URL('./worker.js', import.meta.url));
  1. I envision as a next step that the functionality behind workerImportMetaUrlPlugin is exposed for plugin authors to trigger worker generation specifically for their plugin.

  2. Adding audio and paint worklets as new types of "workers" will require a certain degree of flexibility to tell vite which syntax to pick up as worklets. (e.g. context.audioWorklet.addModule(new URL('./worklet.js', import.meta.url)) could be added as worker: [{ function: 'context.audioWorklet.addModule', kind: 'worklet' }]

Dependency

The escape-string-regexp is needed as new dependency to escape the input strings in the constructor regex matching. The dependency seems quite widely adopted but is also very lightweight:

https://www.npmjs.com/package/escape-string-regexp
https://github.com/sindresorhus/escape-string-regexp/blob/main/index.js#L8

Copy link

stackblitz bot commented Apr 14, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@Danielku15 Danielku15 marked this pull request as ready for review April 15, 2024 07:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant