5bdef6aabe
* import next-server logic during the time the configuration is loaded * load minimizer plugins only when used * load ReactDevOverlay only when used * load only meta information of tsconfig for validation * make worker for configuration loading lighter * only load runTypeCheck when used * load postcss config only when used
53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
import path from 'path'
|
|
import { Worker } from 'jest-worker'
|
|
import * as Log from '../../build/output/log'
|
|
import type { CheckReasons, CheckResult } from './config-utils-worker'
|
|
import { install, shouldLoadWithWebpack5 } from './config-utils-worker'
|
|
|
|
export { install, shouldLoadWithWebpack5 }
|
|
|
|
function reasonMessage(reason: CheckReasons) {
|
|
switch (reason) {
|
|
case 'future-flag':
|
|
return 'future.webpack5 option enabled'
|
|
case 'no-future-flag':
|
|
return 'future.webpack5 option disabled'
|
|
case 'no-config':
|
|
return 'no next.config.js'
|
|
case 'webpack-config':
|
|
return 'custom webpack configuration in next.config.js'
|
|
case 'no-webpack-config':
|
|
return 'no custom webpack configuration in next.config.js'
|
|
case 'test-mode':
|
|
return 'internal test mode'
|
|
default:
|
|
return ''
|
|
}
|
|
}
|
|
|
|
export async function loadWebpackHook(phase: string, dir: string) {
|
|
let useWebpack5 = false
|
|
const worker: any = new Worker(
|
|
path.resolve(__dirname, './config-utils-worker.js'),
|
|
{
|
|
enableWorkerThreads: false,
|
|
numWorkers: 1,
|
|
}
|
|
)
|
|
try {
|
|
const result: CheckResult = await worker.shouldLoadWithWebpack5(phase, dir)
|
|
Log.info(
|
|
`Using webpack ${result.enabled ? '5' : '4'}. Reason: ${reasonMessage(
|
|
result.reason
|
|
)} https://nextjs.org/docs/messages/webpack5`
|
|
)
|
|
useWebpack5 = Boolean(result.enabled)
|
|
} catch {
|
|
// If this errors, it likely will do so again upon boot, so we just swallow
|
|
// it here.
|
|
} finally {
|
|
worker.end()
|
|
}
|
|
|
|
install(useWebpack5)
|
|
}
|