From df53f2a8b850848768470b836e308a9a016ee9c5 Mon Sep 17 00:00:00 2001 From: avivkeller Date: Thu, 26 Nov 2026 11:37:17 -0700 Subject: [PATCH] chore: uncloneables --- .changeset/cloneable-worker-config.md | 6 +++++ docs/creating-generators.md | 6 +++-- packages/core/package.json | 1 + .../src/threading/__tests__/parallel.test.mjs | 26 +++++++++++++++++++ packages/core/src/threading/parallel.mjs | 19 +++++++------- packages/react/src/html/README.md | 10 +------ pnpm-lock.yaml | 9 +++++++ 7 files changed, 57 insertions(+), 20 deletions(-) create mode 100644 .changeset/cloneable-worker-config.md diff --git a/.changeset/cloneable-worker-config.md b/.changeset/cloneable-worker-config.md new file mode 100644 index 000000000..2cb5ca297 --- /dev/null +++ b/.changeset/cloneable-worker-config.md @@ -0,0 +1,6 @@ +--- +'@doc-kit/core': patch +'@doc-kit/generator-react': patch +--- + +Leave values that can't be cloned, such as functions, out of the configuration sent to worker threads. A configured `html.bundler`, the Vite adapter included, no longer needs `threads: 1`. diff --git a/docs/creating-generators.md b/docs/creating-generators.md index 6de55a95c..1750d002f 100644 --- a/docs/creating-generators.md +++ b/docs/creating-generators.md @@ -261,8 +261,10 @@ Filtering entries is light work, but heavier work takes the same shape. - The items, `deps`, the results, and the generator's configuration section are copied between threads with the [structured clone algorithm](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm). - Functions can't be copied (passing one throws a `DataCloneError`), and class - instances arrive as plain objects. + Functions can't be copied (passing one in the items or `deps` throws a + `DataCloneError`), and class instances arrive as plain objects. Values in the + configuration that can't be copied, such as functions and `URL`s, are left + out of the copy a worker gets. - Each worker imports your module on its own, so `processChunk` sees nothing `generate` set up: pass what it needs in `deps`. A module only `processChunk` uses can be imported inside it, as the `metadata` generator diff --git a/packages/core/package.json b/packages/core/package.json index 8da82c45e..3c6944817 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -69,6 +69,7 @@ "remark-stringify": "^11.0.0", "semver": "^7.8.5", "shiki": "^4.5.0", + "to-cloneable": "^1.0.0", "unified": "^11.0.5", "unist-builder": "^4.0.0", "unist-util-find-after": "^5.0.0", diff --git a/packages/core/src/threading/__tests__/parallel.test.mjs b/packages/core/src/threading/__tests__/parallel.test.mjs index 0f6cf1200..bbeb72d2c 100644 --- a/packages/core/src/threading/__tests__/parallel.test.mjs +++ b/packages/core/src/threading/__tests__/parallel.test.mjs @@ -241,6 +241,32 @@ describe('createParallelWorker', () => { deepStrictEqual(await result, [0, 1, 2, 3, 4]); }); + it('sends workers only what can be cloned of the configuration', async () => { + const configurations = []; + const pool = { + run: async task => { + configurations.push(structuredClone(task.configuration)); + + return task.input; + }, + }; + const worker = createParallelWorker('ast-js', astJsGenerator, pool, { + threads: 2, + chunkSize: 1, + 'ast-js': { + minify: true, + bundler: { compile: () => {}, name: 'custom' }, + }, + other: { minify: false }, + }); + + await collectStream(worker.stream([0, 1, 2], {})); + + deepStrictEqual(configurations[0], { + 'ast-js': { minify: true, bundler: { name: 'custom' } }, + }); + }); + it('propagates a later worker failure without an unhandled rejection', async () => { const tasks = []; const pool = { diff --git a/packages/core/src/threading/parallel.mjs b/packages/core/src/threading/parallel.mjs index 494264369..528cebef7 100644 --- a/packages/core/src/threading/parallel.mjs +++ b/packages/core/src/threading/parallel.mjs @@ -1,5 +1,7 @@ 'use strict'; +import { toCloneable } from 'to-cloneable'; + import logger from '#logger/index.mjs'; import { isAsyncIterable } from '#utils/misc.mjs'; @@ -32,9 +34,8 @@ const createChunks = (count, size) => { * @param {unknown[]} fullInput - Full input array * @param {number[]} indices - Indices to process * @param {Object} extra - Stuff to pass to the worker - * @param {import('../utils/configuration/types').Configuration} configuration - Serialized options + * @param {Partial} configuration - The configuration the worker gets * @param {string} generatorSpecifier - Resolved specifier of the generator - * @param {string} generatorName - Name of the generator * @returns {ParallelTaskOptions} Task data for Piscina */ const createTask = ( @@ -42,8 +43,7 @@ const createTask = ( indices, extra, configuration, - generatorSpecifier, - generatorName + generatorSpecifier ) => { return { generatorSpecifier, @@ -52,10 +52,7 @@ const createTask = ( // Remap indices to 0-based for the sliced array itemIndices: indices.map((_, i) => i), extra, - // Only pass the needed configuration to this generator - configuration: { - [generatorName]: configuration[generatorName], - }, + configuration, }; }; @@ -78,6 +75,10 @@ export default function createParallelWorker( const { name } = generator; + // Only this generator's configuration, without what can't be sent to a + // worker, such as a bundler's functions, which only this thread uses + const workerConfiguration = { [name]: toCloneable(configuration[name]) }; + return { /** * Processes items in parallel, yielding results in submission order. @@ -165,7 +166,7 @@ export default function createParallelWorker( running++; const task = pool.run( - createTask(batch, indices, extra, configuration, specifier, name) + createTask(batch, indices, extra, workerConfiguration, specifier) ); tasks.push( diff --git a/packages/react/src/html/README.md b/packages/react/src/html/README.md index 1f5861b6f..671c07ecf 100644 --- a/packages/react/src/html/README.md +++ b/packages/react/src/html/README.md @@ -534,12 +534,6 @@ When you customize the site, keep in mind that: The `bundler` option takes a small adapter, so the site can be built with Vite (the default) or any other bundler. -> [!WARNING] -> Worker threads receive a copy of the configuration, and functions can't be -> copied. A configuration that sets `bundler`, the Vite adapter included, must -> build with `threads: 1` (or `--threads 1`). With more threads, the build -> fails with a `DataCloneError`. - ### Writing an adapter An adapter is an object with three methods. The generator calls `buildServer` @@ -584,7 +578,7 @@ CSS modules, resolve the `config.imports` aliases, and serve the indefinitely. An adapter can live in your project, without changes to doc-kit. Pass it as -`bundler`, with `threads: 1`: +`bundler`: ```js displayName="webpack-bundler.mjs" export const createWebpackBundler = webpackOptions => ({ @@ -617,8 +611,6 @@ import { createViteBundler } from '@doc-kit/generator-react/html/bundlers/vite'; import myVitePlugin from './my-vite-plugin.mjs'; export default { - threads: 1, - html: { bundler: createViteBundler({ plugins: [myVitePlugin()], diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4bb6b6b10..9fcbe5dee 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -289,6 +289,9 @@ importers: shiki: specifier: ^4.5.0 version: 4.5.0 + to-cloneable: + specifier: ^1.0.0 + version: 1.0.0 unified: specifier: ^11.0.5 version: 11.0.5 @@ -3361,6 +3364,10 @@ packages: resolution: {integrity: sha512-jBrmx4lYmaC9k/mgPbylxs7kBUxHtD8256up+HjLaDFfXScKJQyil+SWXSvhAtT7XHo+yTVlpB81PGmHP8oLSQ==} engines: {node: ^20.0.0 || >=22.0.0} + to-cloneable@1.0.0: + resolution: {integrity: sha512-TMxLWj7J7i5C8HqOigb+NpPcJycWrJpoSMoEo/qOjG1PCTou0MgFbnxKSi11L7YrkA6poffPT4srIzUdk+wyIg==} + engines: {node: '>=22'} + to-valid-identifier@1.0.0: resolution: {integrity: sha512-41wJyvKep3yT2tyPqX/4blcfybknGB4D+oETKLs7Q76UiPqRpUJK3hr1nxelyYO0PHKVzJwlu0aCeEAsGI6rpw==} engines: {node: '>=20'} @@ -6713,6 +6720,8 @@ snapshots: tinypool@2.2.0: {} + to-cloneable@1.0.0: {} + to-valid-identifier@1.0.0: dependencies: '@sindresorhus/base62': 1.0.0