2021-04-18 12:28:09 +02:00
|
|
|
import path from 'path'
|
2021-03-16 22:08:35 +01:00
|
|
|
import { Worker } from 'jest-worker'
|
2021-03-08 17:53:52 +01:00
|
|
|
import * as Log from '../../build/output/log'
|
2021-04-18 12:28:09 +02:00
|
|
|
import type { CheckReasons, CheckResult } from './config-utils-worker'
|
|
|
|
import { install, shouldLoadWithWebpack5 } from './config-utils-worker'
|
2021-02-27 07:19:35 +01:00
|
|
|
|
2021-04-18 12:28:09 +02:00
|
|
|
export { install, shouldLoadWithWebpack5 }
|
2021-03-24 14:10:10 +01:00
|
|
|
|
|
|
|
function reasonMessage(reason: CheckReasons) {
|
|
|
|
switch (reason) {
|
|
|
|
case 'future-flag':
|
|
|
|
return 'future.webpack5 option enabled'
|
2021-03-29 13:42:04 +02:00
|
|
|
case 'no-future-flag':
|
2021-04-15 13:46:56 +02:00
|
|
|
return 'future.webpack5 option disabled'
|
2021-03-24 14:10:10 +01:00
|
|
|
case 'no-config':
|
|
|
|
return 'no next.config.js'
|
2021-03-29 13:42:04 +02:00
|
|
|
case 'webpack-config':
|
|
|
|
return 'custom webpack configuration in next.config.js'
|
2021-03-24 14:10:10 +01:00
|
|
|
case 'no-webpack-config':
|
|
|
|
return 'no custom webpack configuration in next.config.js'
|
|
|
|
case 'test-mode':
|
|
|
|
return 'internal test mode'
|
|
|
|
default:
|
|
|
|
return ''
|
|
|
|
}
|
2021-02-27 07:19:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function loadWebpackHook(phase: string, dir: string) {
|
|
|
|
let useWebpack5 = false
|
2021-04-18 12:28:09 +02:00
|
|
|
const worker: any = new Worker(
|
|
|
|
path.resolve(__dirname, './config-utils-worker.js'),
|
|
|
|
{
|
|
|
|
enableWorkerThreads: false,
|
|
|
|
numWorkers: 1,
|
|
|
|
}
|
|
|
|
)
|
2021-02-27 07:19:35 +01:00
|
|
|
try {
|
2021-03-24 14:10:10 +01:00
|
|
|
const result: CheckResult = await worker.shouldLoadWithWebpack5(phase, dir)
|
2021-03-29 13:42:04 +02:00
|
|
|
Log.info(
|
|
|
|
`Using webpack ${result.enabled ? '5' : '4'}. Reason: ${reasonMessage(
|
|
|
|
result.reason
|
|
|
|
)} https://nextjs.org/docs/messages/webpack5`
|
|
|
|
)
|
2021-03-24 14:10:10 +01:00
|
|
|
useWebpack5 = Boolean(result.enabled)
|
2021-02-27 07:19:35 +01:00
|
|
|
} catch {
|
|
|
|
// If this errors, it likely will do so again upon boot, so we just swallow
|
|
|
|
// it here.
|
|
|
|
} finally {
|
|
|
|
worker.end()
|
|
|
|
}
|
|
|
|
|
|
|
|
install(useWebpack5)
|
|
|
|
}
|