Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 6 additions & 2 deletions packages/rspack/etc/core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4509,7 +4509,9 @@ class LoaderObject {
// (undocumented)
options?: string | object;
// (undocumented)
parallel?: boolean;
parallel?: boolean | {
maxWorkers?: number;
};
// (undocumented)
path: string;
// (undocumented)
Expand Down Expand Up @@ -7048,7 +7050,9 @@ export type RuleSetLoaderOptions = string | Record<string, any>;
export type RuleSetLoaderWithOptions = {
ident?: string;
loader: RuleSetLoader;
parallel?: boolean;
parallel?: boolean | {
maxWorkers?: 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 | { maxWorkers?: number };

options?: RuleSetLoaderOptions;
};
Expand Down
7 changes: 5 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 | { maxWorkers?: number };
/**
* @internal This field is rspack internal. Do not edit.
*/
Expand Down Expand Up @@ -995,7 +995,10 @@ export async function runLoaders(
loaderState,
args
},
getWorkerLoaderHandlers()
getWorkerLoaderHandlers(),
typeof currentLoaderObject?.parallel === "object"
? currentLoaderObject.parallel
: undefined
)) || [];
} else {
if (loaderState === JsLoaderState.Normal)
Expand Down
16 changes: 11 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,25 @@ 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 (workerOptions?: {
maxWorkers?: 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 maxWorkers = workerOptions?.maxWorkers
? Math.max(workerOptions.maxWorkers, 1)
: undefined;

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

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

Expand Down Expand Up @@ -199,9 +204,10 @@ export const run = async (
task: any,
options: RunOptions & {
handleIncomingRequest: HandleIncomingRequest;
}
},
workerOptions?: { maxWorkers?: number }
) =>
ensureLoaderWorkerPool().then(async pool => {
ensureLoaderWorkerPool(workerOptions).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: { maxWorkers: 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