Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/rspack/etc/core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4509,7 +4509,7 @@ class LoaderObject {
// (undocumented)
options?: string | object;
// (undocumented)
parallel?: boolean;
parallel?: boolean | number;
// (undocumented)
path: string;
// (undocumented)
Expand Down Expand Up @@ -7048,7 +7048,7 @@ export type RuleSetLoaderOptions = string | Record<string, any>;
export type RuleSetLoaderWithOptions = {
ident?: string;
loader: RuleSetLoader;
parallel?: boolean;
parallel?: boolean | number;
options?: RuleSetLoaderOptions;
};

Expand Down
2 changes: 1 addition & 1 deletion packages/rspack/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ export type RuleSetLoaderWithOptions = {

loader: RuleSetLoader;

parallel?: boolean;
parallel?: boolean | number;

options?: RuleSetLoaderOptions;
};
Expand Down
8 changes: 6 additions & 2 deletions packages/rspack/src/loader-runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class LoaderObject {
pitch?: Function;
raw?: boolean;
type?: "module" | "commonjs";
parallel?: boolean;
parallel?: boolean | number;
/**
* @internal This field is rspack internal. Do not edit.
*/
Expand Down Expand Up @@ -995,7 +995,11 @@ export async function runLoaders(
loaderState,
args
},
getWorkerLoaderHandlers()
getWorkerLoaderHandlers(),
typeof currentLoaderObject?.parallel === "number"
? // requested number of threads
currentLoaderObject.parallel
: undefined
)) || [];
} else {
if (loaderState === JsLoaderState.Normal)
Expand Down
14 changes: 9 additions & 5 deletions packages/rspack/src/loader-runner/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@ import path from "node:path";
import type { Tinypool } from "tinypool" with { "resolution-mode": "import" };

let pool: Promise<Tinypool> | undefined;
const ensureLoaderWorkerPool = async () => {
const ensureLoaderWorkerPool = async (requestedThreads?: number) => {
if (pool) {
return pool;
}
return (pool = import("tinypool").then(({ Tinypool }) => {
const cpus = require("node:os").cpus().length;
const availableThreads = Math.max(cpus - 1, 1);
const threadCount = requestedThreads
? Math.max(requestedThreads, 1)
: undefined;

const pool = new Tinypool({
filename: path.resolve(__dirname, "worker.js"),
useAtomics: false,

maxThreads: availableThreads,
minThreads: availableThreads,
maxThreads: threadCount || availableThreads,
minThreads: threadCount || availableThreads,
concurrentTasksPerWorker: 1
});

Expand Down Expand Up @@ -199,9 +202,10 @@ export const run = async (
task: any,
options: RunOptions & {
handleIncomingRequest: HandleIncomingRequest;
}
},
requestedThreads?: number
) =>
ensureLoaderWorkerPool().then(async pool => {
ensureLoaderWorkerPool(requestedThreads).then(async pool => {
const { MessageChannel } = await import("node:worker_threads");
const { port1: mainPort, port2: workerPort } = new MessageChannel();
// Create message channel for processing sync API requests from worker
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
it("should run loader in parallel", () => {
expect(require("./lib.js")).toBe(true)
})
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function () {
return `module.exports = ${this.parallel}`;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports = {
context: __dirname,
module: {
rules: [
{
test: /lib\.js/,
use: [
{
loader: "./unclonable.js",
options: {
notclonable() {}
}
},
{
loader: "./loader-in-worker.js",
parallel: 2,
options: {}
}
]
}
]
},
experiments: {
parallelLoader: true
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function(content) {
return content
}
Loading