Skip to content

Commit 6b36801

Browse files
fix(html): exclude custom bundler from worker configuration (fixes #1201)
1 parent b65d7c1 commit 6b36801

5 files changed

Lines changed: 71 additions & 3 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@doc-kit/core': patch
3+
'@doc-kit/generator-react': patch
4+
---
5+
6+
Exclude main-thread-only bundler options from worker thread configuration to prevent DataCloneError.

‎packages/core/src/generators/types.d.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ declare global {
7070

7171
hasParallelProcessor?: boolean;
7272

73+
/** Configuration keys excluded from worker-thread messages. */
74+
workerConfigOmit?: string[];
75+
7376
/**
7477
* Receive the dependency's output as an async iterable of its chunks, as
7578
* they are produced, instead of one collected array. With one thread, the

‎packages/core/src/threading/__tests__/parallel.test.mjs‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,47 @@ describe('createParallelWorker', () => {
302302
deepStrictEqual(chunks, [[1, 2], [3, 4], [5]]);
303303
});
304304

305+
it('does not clone main-thread-only HTML bundler functions', async () => {
306+
const htmlGenerator = await loadGenerator('html');
307+
const transferred = [];
308+
const pool = {
309+
run(task) {
310+
const copy = structuredClone(task);
311+
transferred.push(copy);
312+
313+
return Promise.resolve(copy.input);
314+
},
315+
};
316+
const bundler = {
317+
buildServer: async () => 'file:///library.mjs',
318+
buildClient: async () => ({}),
319+
compile: async code => code,
320+
};
321+
const configuration = {
322+
threads: 2,
323+
chunkSize: 1,
324+
html: { title: 'Worker regression test', bundler },
325+
};
326+
const worker = createParallelWorker(
327+
'html',
328+
htmlGenerator,
329+
pool,
330+
configuration
331+
);
332+
const results = await collectStream(worker.stream([1, 2, 3], {}));
333+
334+
deepStrictEqual(results, [1, 2, 3]);
335+
strictEqual(transferred.length, 3);
336+
337+
for (const task of transferred) {
338+
deepStrictEqual(task.configuration.html, {
339+
title: 'Worker regression test',
340+
});
341+
}
342+
343+
strictEqual(configuration.html.bundler, bundler);
344+
});
345+
305346
describe('with streamed batches', () => {
306347
it(
307348
'starts a batch before the next one arrives',

‎packages/core/src/threading/parallel.mjs‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
import logger from '#logger/index.mjs';
4-
import { isAsyncIterable } from '#utils/misc.mjs';
4+
import { isAsyncIterable, omitKeys } from '#utils/misc.mjs';
55

66
import { CHUNKS_PER_THREAD } from './constants.mjs';
77

@@ -76,7 +76,15 @@ export default function createParallelWorker(
7676
) {
7777
const { threads, chunkSize } = configuration;
7878

79-
const { name } = generator;
79+
const { name, workerConfigOmit } = generator;
80+
81+
const workerConfiguration =
82+
workerConfigOmit?.length && configuration[name]
83+
? {
84+
...configuration,
85+
[name]: omitKeys(configuration[name], workerConfigOmit),
86+
}
87+
: configuration;
8088

8189
return {
8290
/**
@@ -165,7 +173,14 @@ export default function createParallelWorker(
165173
running++;
166174

167175
const task = pool.run(
168-
createTask(batch, indices, extra, configuration, specifier, name)
176+
createTask(
177+
batch,
178+
indices,
179+
extra,
180+
workerConfiguration,
181+
specifier,
182+
name
183+
)
169184
);
170185

171186
tasks.push(

‎packages/react/src/html/index.mjs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,8 @@ export default {
9696
// page count, so it is farmed out to the worker pool.
9797
hasParallelProcessor: true,
9898

99+
// The bundler runs on the main thread and contains non-cloneable functions.
100+
workerConfigOmit: ['bundler'],
101+
99102
processChunk,
100103
};

0 commit comments

Comments
 (0)