Repository: next.js
Issue: Error when using `experimental.workerThreads` and webpack config
Labels: bug,Output (export/standalone)

### Link to the code that reproduces this issue

https://github.com/Janpot/next-worker-threads-repro

### To Reproduce

1. Clone https://github.com/Janpot/next-worker-threads-repro
1. Run `pnpm install`
1. Run `pnpm build`

The Next.js config combines custom `webpack` with `experimental.workerThreads`.

```tsx
// ./next.config.mjs

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  experimental: {
    workerThreads: true,
  },
  webpack: (config) => config,
};

export default nextConfig;
```

### Current vs. Expected behavior

It currently throws an error:

```
▲ Next.js 15.0.0-canary.121
- Experiments (use with caution):
  · workerThreads

✓ Linting and checking validity of types    
 Creating an optimized production build ...
✓ Compiled successfully
✓ Collecting page data    

> Build error occurred
DOMException [DataCloneError]: (config) => config could not be cloned.
  at new DOMException (node:internal/per_context/domexception:53:5)
  at Worker.postMessage (node:internal/worker:366:5)
  at ExperimentalWorker.send (~/node_modules/.pnpm/next@15.0.0-canary.121_react-dom@19.0.0-rc-d025ddd3-20240722_react@19.0.0-rc-d025ddd3-2024072_2xa4b5m2p52gsklkteytl7vbtu/node_modules/next/dist/compiled/jest-worker/index.js:1:17480)
  at WorkerPool.send (~/node_modules/.pnpm/next@15.0.0-canary.121_react-dom@19.0.0-rc-d025ddd3-20240722_react@19.0.0-rc-d025ddd3-2024072_2xa4b5m2p52gsklkteytl7vbtu/node_modules/next/dist/compiled/jest-worker/index.js:1:6183)
  at Farm._process (~/node_modules/.pnpm/next@15.0.0-canary.121_react-dom@19.0.0-rc-d025ddd3-20240722_react@19.0.0-rc-d025ddd3-2024072_2xa4b5m2p52gsklkteytl7vbtu/node_modules/next/dist/compiled/jest-worker/index.js:1:2094)
  at Farm._push (~/node_modules/.pnpm/next@15.0.0-canary.121_react-dom@19.0.0-rc-d025ddd3-20240722_react@19.0.0-rc-d025ddd3-2024072_2xa4b5m2p52gsklkteytl7vbtu/node_modules/next/dist/compiled/jest-worker/index.js:1:2278)
  at ~/node_modules/.pnpm/next@15.0.0-canary.121_react-dom@19.0.0-rc-d025ddd3-20240722_react@19.0.0-rc-d025ddd3-2024072_2xa4b5m2p52gsklkteytl7vbtu/node_modules/next/dist/compiled/jest-worker/index.js:1:1712
  at new Promise (<anonymous>)
  at Farm.doWork (~/node_modules/.pnpm/next@15.0.0-canary.121_react-dom@19.0.0-rc-d025ddd3-20240722_react@19.0.0-rc-d025ddd3-2024072_2xa4b5m2p52gsklkteytl7vbtu/node_modules/next/dist/compiled/jest-worker/index.js:1:1257)
  at Worker._callFunctionWithArgs (~/node_modules/.pnpm/next@15.0.0-canary.121_react-dom@19.0.0-rc-d025ddd3-20240722_react@19.0.0-rc-d025ddd3-2024072_2xa4b5m2p52gsklkteytl7vbtu/node_modules/next/dist/compiled/jest-worker/index.js:1:23770)
 Generating static pages (0/3)  [    ] ELIFECYCLE  Command failed with exit code 1.
```

### Provide environment information

```bash
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.6.0: Mon Jul 29 21:14:21 PDT 2024; root:xnu-10063.141.2~1/RELEASE_ARM64_T8103
  Available memory (MB): 16384
  Available CPU cores: 8
Binaries:
  Node: 18.20.2
  npm: 10.5.0
  Yarn: 1.22.22
  pnpm: 9.7.1
Relevant Packages:
  next: 14.2.5 // Latest available version is detected (14.2.5).
  eslint-config-next: N/A
  react: 18.3.1
  react-dom: 18.3.1
  typescript: N/A
Next.js Config:
  output: export
```


### Which area(s) are affected? (Select all that apply)

Output (export/standalone)

### Which stage(s) are affected? (Select all that apply)

next build (local)

### Additional context

Discovered in https://github.com/mui/material-ui/pull/42824#issuecomment-2298562178