Commit 14ed8675 authored by Christoph Tavan's avatar Christoph Tavan Committed by Joe Haddad

Disable worker_threads by default for firebase compatibility (#9199)

* Pass config.experimental.cpus to export during build

Currently, there is no way of specifying the number of worker threads of
`next export` when run as part of `next build`.

I suggest a sane default should be to just use the same amount of
workers that were used during the build process which currently seems to
be configured through `config.experimental.cpus`.

This setting is already respected in the two other places where
jest-workers are in use: The TerserPlugin and the staticCheckWorkers in
`next build`.

* Only enable worker threads if there is more than 1 worker

Multiple worker threads can cause problems when certain dependencies are
being used, see e.g. https://github.com/zeit/next.js/issues/7894

This patch allows disabling of worker threads by setting
`config.experimental.cpus = 1`.

The benefit of spawning 1 worker thread, if there is any at all, should
very limited anyways, so the workload can just as well be processed in
the main thread.

* Disable parallel build for firebase authentication example

* Add integration test to cover #7894

* Rename test suite and add worker_threads config

* Disable worker_threads by default

* Update index.test.js

* Use workerThreads config for TerserPlugin

* Update to use workerThreads config in
TerserPlugin for consistency

* Disable node 12 specific test
parent ad2a3d65
...@@ -337,7 +337,7 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -337,7 +337,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
const staticCheckWorkers = new Worker(staticCheckWorker, { const staticCheckWorkers = new Worker(staticCheckWorker, {
numWorkers: config.experimental.cpus, numWorkers: config.experimental.cpus,
enableWorkerThreads: true, enableWorkerThreads: config.experimental.workerThreads,
}) })
const analysisBegin = process.hrtime() const analysisBegin = process.hrtime()
...@@ -471,6 +471,7 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -471,6 +471,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
sprPages, sprPages,
silent: true, silent: true,
buildExport: true, buildExport: true,
threads: config.experimental.cpus,
pages: combinedPages, pages: combinedPages,
outdir: path.join(distDir, 'export'), outdir: path.join(distDir, 'export'),
} }
......
...@@ -171,11 +171,12 @@ export default async function getBaseWebpackConfig( ...@@ -171,11 +171,12 @@ export default async function getBaseWebpackConfig(
const webpackMode = dev ? 'development' : 'production' const webpackMode = dev ? 'development' : 'production'
const terserPluginConfig = { const terserPluginConfig = {
parallel: true,
sourceMap: false,
cache: true, cache: true,
cpus: config.experimental.cpus, cpus: config.experimental.cpus,
distDir: distDir, distDir: distDir,
parallel: true,
sourceMap: false,
workerThreads: config.experimental.workerThreads,
} }
const terserOptions = { const terserOptions = {
parse: { parse: {
......
...@@ -10,13 +10,14 @@ const writeFileP = promisify(writeFile) ...@@ -10,13 +10,14 @@ const writeFileP = promisify(writeFile)
const readFileP = promisify(readFile) const readFileP = promisify(readFile)
export default class TaskRunner { export default class TaskRunner {
constructor({ distDir, cpus, cache }) { constructor({ distDir, cpus, cache, workerThreads }) {
if (cache) { if (cache) {
mkdirp.sync((this.cacheDir = join(distDir, 'cache', 'next-minifier'))) mkdirp.sync((this.cacheDir = join(distDir, 'cache', 'next-minifier')))
} }
// In some cases cpus() returns undefined // In some cases cpus() returns undefined
// https://github.com/nodejs/node/issues/19022 // https://github.com/nodejs/node/issues/19022
this.maxConcurrentWorkers = cpus this.maxConcurrentWorkers = cpus
this.useWorkerThreads = workerThreads
} }
run(tasks, callback) { run(tasks, callback) {
...@@ -28,7 +29,7 @@ export default class TaskRunner { ...@@ -28,7 +29,7 @@ export default class TaskRunner {
if (this.maxConcurrentWorkers > 1) { if (this.maxConcurrentWorkers > 1) {
this.workers = new Worker(worker, { this.workers = new Worker(worker, {
enableWorkerThreads: true, enableWorkerThreads: this.useWorkerThreads,
numWorkers: this.maxConcurrentWorkers, numWorkers: this.maxConcurrentWorkers,
}) })
this.boundWorkers = options => this.workers.default(options) this.boundWorkers = options => this.workers.default(options)
......
...@@ -21,10 +21,12 @@ export class TerserPlugin { ...@@ -21,10 +21,12 @@ export class TerserPlugin {
cache = false, cache = false,
cpus, cpus,
distDir, distDir,
workerThreads,
} = options } = options
this.cpus = cpus this.cpus = cpus
this.distDir = distDir this.distDir = distDir
this.workerThreads = workerThreads
this.options = { this.options = {
warningsFilter, warningsFilter,
sourceMap, sourceMap,
...@@ -134,6 +136,7 @@ export class TerserPlugin { ...@@ -134,6 +136,7 @@ export class TerserPlugin {
distDir: this.distDir, distDir: this.distDir,
cpus: this.cpus, cpus: this.cpus,
cache: this.options.cache, cache: this.options.cache,
workerThreads: this.workerThreads,
}) })
const processedAssets = new WeakSet() const processedAssets = new WeakSet()
......
...@@ -267,7 +267,7 @@ export default async function( ...@@ -267,7 +267,7 @@ export default async function(
{ {
maxRetries: 0, maxRetries: 0,
numWorkers: threads, numWorkers: threads,
enableWorkerThreads: true, enableWorkerThreads: nextConfig.experimental.workerThreads,
exposedMethods: ['default'], exposedMethods: ['default'],
} }
) as any ) as any
......
...@@ -48,6 +48,7 @@ const defaultConfig: { [key: string]: any } = { ...@@ -48,6 +48,7 @@ const defaultConfig: { [key: string]: any } = {
publicDirectory: false, publicDirectory: false,
sprFlushToDisk: true, sprFlushToDisk: true,
deferScripts: false, deferScripts: false,
workerThreads: false,
}, },
future: { future: {
excludeDefaultMomentLocales: false, excludeDefaultMomentLocales: false,
......
import 'firebase/firestore'
export default () => <div>Firebase</div>
import 'firebase/firestore'
export default () => <div>Firebase</div>
/* eslint-env jest */
/* global jasmine */
import path from 'path'
import fs from 'fs-extra'
import { nextBuild } from 'next-test-utils'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 1
const appDir = path.join(__dirname, '..')
const nextConfig = path.join(appDir, 'next.config.js')
describe('Building Firebase', () => {
// TODO: investigate re-enabling this test in node 12 environment
xit('Throws an error when building with firebase dependency with worker_threads', async () => {
await fs.writeFile(
nextConfig,
`module.exports = { experimental: { workerThreads: true } }`
)
const results = await nextBuild(appDir, [], { stdout: true, stderr: true })
expect(results.stdout + results.stderr).toMatch(/Build error occurred/)
expect(results.stdout + results.stderr).toMatch(
/grpc_node\.node\. Module did not self-register\./
)
})
it('Throws no error when building with firebase dependency without worker_threads', async () => {
await fs.remove(nextConfig)
const results = await nextBuild(appDir, [], { stdout: true, stderr: true })
expect(results.stdout + results.stderr).not.toMatch(/Build error occurred/)
expect(results.stdout + results.stderr).not.toMatch(
/grpc_node\.node\. Module did not self-register\./
)
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment