Replies: 3 comments 4 replies
-
what's the behavior when user pass loader options that doesn't follow the specification? |
Beta Was this translation helpful? Give feedback.
1 reply
-
it seems thread-loader is faster than |
Beta Was this translation helpful? Give feedback.
2 replies
-
Does this feature support the sass-loader? I tested it and encountered a DataCloneError. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
PR: #9807
Motivation
Try to use
worker_threads
to run JavaScript loaders. This adds parallelism to the current JS loader.User guide
To enable parallelism, set either
Rule.use.parallel = true
andexperiments.parallelLoader = true
:Detailed design
Worker threads
Unlike
thread-loader
, rspack uses worker threads to drive loader tasks, usingtinypool
under the hood. That said, loader options need to be serializable in order to be sent to the worker thread and not all methods are available inLoaderContext
. Currently,importModule
and most of the methods of_compilation
,_compiler
,_module
are missing.Loader runner
thread-loader
dispatches remaining loaders by default. If I writeuse: ["thread-loader", "css-loader", "less-loader"]
,css-loader
andless-loader
would end up in the worker thread. This is not the same in rspack. You may consider the worker as a loader runner but in worker. If rspack encounters a loader that is setparallel: true
, loader runner on the main thread would send the execution state to worker and yield if the next loader is notparallel
or it's a rust builtin loader. This chains loaders withparallel
enabled automatically, achieving the best performance.Handling synchronous calls
Worker threads are spawned on demand and, by default, is using the max thread available on the OS. In each worker, a channel is created to pass requests to the main thread for on-demand requesting, such as
LoaderContext["getResolve"]
. That is to say, each request to the main thread is running asynchronously. This also requires most of the method ofLoaderContext
to beasync
. To make it compatible with synchronous loader context calls to the main thread, rspack uses an internal wrapper to wrap anasync
method into sync, by exposingwait()
to each request. For example,addDependency
does not resolve until the main thread yield, butgetDependencies
will wait until all previous adding operation to finish and get the latest dependencies in a synchronous call. This was achieved by putting the thread to sleep and waking it up when it's necessary.Caveats
importModule
,_compilation
,_compiler
and_module
methods.Benchmark
Building 100 copies of
antd.less
withcss-loader
,rspack-plugin-extract-css
andless-loader
:Building 100 copies of
antd.less
withexperiments.css
andless-loader
:Benchmark was running on Apple M2 Max, 64G.
antd.less
is referenced from this.Beta Was this translation helpful? Give feedback.
All reactions