The content of this section is derived from the content of the following links and is subject to the CC BY 4.0 license.
The following contents can be assumed to be the result of modifications and deletions based on the original contents if not specifically stated.
Rspack provides built-in support for Web Workers, which means you don't need any loader to use Web Workers directly.
Basic Worker creation syntax:
You can customize the Worker's chunk name through the name
property (the property value must be statically analyzable). This name will replace the [name]
placeholder in the generated chunk filename:
This syntax was chosen for its strong standards compliance. It's built on the standard ECMAScript module specification, which means it works seamlessly even without build tools. For example, it runs natively in modern browsers with ES module support.
In addition to new Worker()
, Rspack also supports the following syntax by default:
new SharedWorker()
, see SharedWorkerimport { Worker } from "node:worker_threads"
: commonly used in Node.js environments, see Worker threadsnavigator.serviceWorker.register()
: used to register Service Workers, see ServiceWorkerContainerTo support additional custom syntax, you can configure it through module.parser.javascript.worker
.
For usage examples, see:
Note that new Worker
can also accept a string representation of a URL, but only passing in URLs is supported in Rspack.
Rspack does not support the use of variables in new Worker
. For example, the following code will not work:
This is because Rspack cannot statically analyze the syntax. Please be sure to note this limitation when using the Worker syntax in Rspack.
Not support /* webpackEntryOptions: { filename: "workers/[name].js" } */
magic comments for now.
worker-loader
is provided only as a temporary solution to facilitate project migration to Rspack. It is recommended to use the new Worker()
syntax instead.
Rspack also supports worker-loader. However, since worker-loader is no longer maintained, please use worker-rspack-loader as a replacement.
Use resolveLoader to replace worker-loader with worker-rspack-loader: