2021-03-08 17:53:52 +01:00
|
|
|
import { loadEnvConfig } from '@next/env'
|
2021-03-16 22:08:35 +01:00
|
|
|
import { Worker } from 'jest-worker'
|
2021-02-27 07:19:35 +01:00
|
|
|
import findUp from 'next/dist/compiled/find-up'
|
|
|
|
import { init as initWebpack } from 'next/dist/compiled/webpack/webpack'
|
2021-03-08 17:53:52 +01:00
|
|
|
import { CONFIG_FILE, PHASE_DEVELOPMENT_SERVER } from '../lib/constants'
|
2021-02-27 07:19:35 +01:00
|
|
|
import { NextConfig, normalizeConfig } from './config-shared'
|
2021-03-08 17:53:52 +01:00
|
|
|
import * as Log from '../../build/output/log'
|
2021-02-27 07:19:35 +01:00
|
|
|
|
|
|
|
let installed: boolean = false
|
|
|
|
|
|
|
|
export function install(useWebpack5: boolean) {
|
|
|
|
if (installed) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
installed = true
|
|
|
|
|
|
|
|
initWebpack(useWebpack5)
|
|
|
|
|
|
|
|
// hook the Node.js require so that webpack requires are
|
|
|
|
// routed to the bundled and now initialized webpack version
|
|
|
|
require('../../build/webpack/require-hook')
|
|
|
|
}
|
|
|
|
|
2021-03-24 14:10:10 +01:00
|
|
|
type CheckReasons =
|
|
|
|
| 'test-mode'
|
|
|
|
| 'no-config'
|
|
|
|
| 'future-flag'
|
2021-03-29 13:42:04 +02:00
|
|
|
| 'no-future-flag'
|
2021-03-24 14:10:10 +01:00
|
|
|
| 'no-webpack-config'
|
|
|
|
| 'webpack-config'
|
|
|
|
|
|
|
|
type CheckResult = {
|
|
|
|
enabled: boolean
|
|
|
|
reason: CheckReasons
|
|
|
|
}
|
|
|
|
|
2021-02-27 07:19:35 +01:00
|
|
|
export async function shouldLoadWithWebpack5(
|
|
|
|
phase: string,
|
|
|
|
dir: string
|
2021-03-24 14:10:10 +01:00
|
|
|
): Promise<CheckResult> {
|
2021-03-08 17:53:52 +01:00
|
|
|
await loadEnvConfig(dir, phase === PHASE_DEVELOPMENT_SERVER, Log)
|
|
|
|
|
2021-02-27 07:19:35 +01:00
|
|
|
const path = await findUp(CONFIG_FILE, {
|
|
|
|
cwd: dir,
|
|
|
|
})
|
|
|
|
|
2021-03-03 10:37:24 +01:00
|
|
|
if (Number(process.env.NEXT_PRIVATE_TEST_WEBPACK5_MODE) > 0) {
|
2021-03-24 14:10:10 +01:00
|
|
|
return {
|
|
|
|
enabled: true,
|
|
|
|
reason: 'test-mode',
|
|
|
|
}
|
2021-03-03 10:37:24 +01:00
|
|
|
}
|
|
|
|
|
2021-02-27 07:19:35 +01:00
|
|
|
// No `next.config.js`:
|
|
|
|
if (!path?.length) {
|
2021-03-29 13:42:04 +02:00
|
|
|
// Uncomment to add auto-enable when there is no next.config.js
|
2021-03-24 14:10:10 +01:00
|
|
|
// Use webpack 5 by default in new apps:
|
|
|
|
return {
|
2021-04-12 19:52:43 +02:00
|
|
|
enabled: true,
|
|
|
|
reason: 'no-config',
|
2021-03-24 14:10:10 +01:00
|
|
|
}
|
2021-02-27 07:19:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Default to webpack 4 for backwards compatibility on boot:
|
|
|
|
install(false)
|
|
|
|
|
|
|
|
const userConfigModule = require(path)
|
|
|
|
const userConfig: Partial<NextConfig> = normalizeConfig(
|
|
|
|
phase,
|
|
|
|
userConfigModule.default || userConfigModule
|
|
|
|
)
|
|
|
|
|
2021-03-24 14:10:10 +01:00
|
|
|
// Opted-in manually
|
|
|
|
if (userConfig.future?.webpack5 === true) {
|
|
|
|
return {
|
|
|
|
enabled: true,
|
|
|
|
reason: 'future-flag',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-15 13:46:56 +02:00
|
|
|
// Opted-out manually
|
|
|
|
if (userConfig.future?.webpack5 === false) {
|
|
|
|
return {
|
|
|
|
enabled: false,
|
|
|
|
reason: 'no-future-flag',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-29 13:42:04 +02:00
|
|
|
// Uncomment to add auto-enable when there is no custom webpack config
|
2021-03-24 14:10:10 +01:00
|
|
|
// The user isn't configuring webpack
|
2021-04-12 19:52:43 +02:00
|
|
|
if (!userConfig.webpack) {
|
|
|
|
return {
|
|
|
|
enabled: true,
|
|
|
|
reason: 'no-webpack-config',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-24 14:10:10 +01:00
|
|
|
return {
|
|
|
|
enabled: false,
|
2021-04-12 19:52:43 +02:00
|
|
|
reason: 'webpack-config',
|
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-03-04 23:09:45 +01:00
|
|
|
const worker: any = new Worker(__filename, { enableWorkerThreads: false })
|
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)
|
|
|
|
}
|