forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: load webpack hook before config is required (vercel#22583)
This pull request ensures the webpack hook is installed before an attempt is made to load the configuration. This pull request is tested by the PnP tests, which should now be passing as a result of this change. --- Fixes vercel#21679
- Loading branch information
Showing
7 changed files
with
182 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import os from 'os' | ||
import { Header, Redirect, Rewrite } from '../../lib/load-custom-routes' | ||
import { imageConfigDefault } from './image-config' | ||
|
||
export type DomainLocales = Array<{ | ||
http?: true | ||
domain: string | ||
locales?: string[] | ||
defaultLocale: string | ||
}> | ||
|
||
export type NextConfig = { [key: string]: any } & { | ||
i18n?: { | ||
locales: string[] | ||
defaultLocale: string | ||
domains?: DomainLocales | ||
localeDetection?: false | ||
} | null | ||
|
||
headers?: () => Promise<Header[]> | ||
rewrites?: () => Promise<Rewrite[]> | ||
redirects?: () => Promise<Redirect[]> | ||
|
||
trailingSlash?: boolean | ||
|
||
future: { | ||
strictPostcssConfiguration: boolean | ||
excludeDefaultMomentLocales: boolean | ||
webpack5: boolean | ||
} | ||
} | ||
|
||
export const defaultConfig: NextConfig = { | ||
env: [], | ||
webpack: null, | ||
webpackDevMiddleware: null, | ||
distDir: '.next', | ||
assetPrefix: '', | ||
configOrigin: 'default', | ||
useFileSystemPublicRoutes: true, | ||
generateBuildId: () => null, | ||
generateEtags: true, | ||
pageExtensions: ['tsx', 'ts', 'jsx', 'js'], | ||
target: 'server', | ||
poweredByHeader: true, | ||
compress: true, | ||
analyticsId: process.env.VERCEL_ANALYTICS_ID || '', | ||
images: imageConfigDefault, | ||
devIndicators: { | ||
buildActivity: true, | ||
}, | ||
onDemandEntries: { | ||
maxInactiveAge: 60 * 1000, | ||
pagesBufferLength: 2, | ||
}, | ||
amp: { | ||
canonicalBase: '', | ||
}, | ||
basePath: '', | ||
sassOptions: {}, | ||
trailingSlash: false, | ||
i18n: null, | ||
productionBrowserSourceMaps: false, | ||
experimental: { | ||
cpus: Math.max( | ||
1, | ||
(Number(process.env.CIRCLE_NODE_TOTAL) || | ||
(os.cpus() || { length: 1 }).length) - 1 | ||
), | ||
plugins: false, | ||
profiling: false, | ||
sprFlushToDisk: true, | ||
reactMode: 'legacy', | ||
workerThreads: false, | ||
pageEnv: false, | ||
optimizeFonts: false, | ||
optimizeImages: false, | ||
optimizeCss: false, | ||
scrollRestoration: false, | ||
scriptLoader: false, | ||
stats: false, | ||
}, | ||
future: { | ||
strictPostcssConfiguration: false, | ||
excludeDefaultMomentLocales: false, | ||
webpack5: Number(process.env.NEXT_PRIVATE_TEST_WEBPACK5_MODE) > 0, | ||
}, | ||
serverRuntimeConfig: {}, | ||
publicRuntimeConfig: {}, | ||
reactStrictMode: false, | ||
} | ||
|
||
export function normalizeConfig(phase: string, config: any) { | ||
if (typeof config === 'function') { | ||
config = config(phase, { defaultConfig }) | ||
|
||
if (typeof config.then === 'function') { | ||
throw new Error( | ||
'> Promise returned in next config. https://err.sh/vercel/next.js/promise-in-next-config' | ||
) | ||
} | ||
} | ||
return config | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import Worker from 'jest-worker' | ||
import findUp from 'next/dist/compiled/find-up' | ||
import { init as initWebpack } from 'next/dist/compiled/webpack/webpack' | ||
import { CONFIG_FILE } from '../lib/constants' | ||
import { NextConfig, normalizeConfig } from './config-shared' | ||
|
||
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') | ||
} | ||
|
||
export async function shouldLoadWithWebpack5( | ||
phase: string, | ||
dir: string | ||
): Promise<boolean> { | ||
const path = await findUp(CONFIG_FILE, { | ||
cwd: dir, | ||
}) | ||
|
||
// No `next.config.js`: | ||
if (!path?.length) { | ||
return false // TODO: return true to default to webpack 5 | ||
} | ||
|
||
// Default to webpack 4 for backwards compatibility on boot: | ||
install(false) | ||
|
||
const userConfigModule = require(path) | ||
const userConfig: Partial<NextConfig> = normalizeConfig( | ||
phase, | ||
userConfigModule.default || userConfigModule | ||
) | ||
|
||
// TODO: enable commented branch to enable webpack 5 | ||
return userConfig.future?.webpack5 === true /* || !userConfig.webpack */ | ||
} | ||
|
||
export async function loadWebpackHook(phase: string, dir: string) { | ||
let useWebpack5 = false | ||
const worker: any = new Worker(__filename, { enableWorkerThreads: true }) | ||
try { | ||
useWebpack5 = Boolean(await worker.shouldLoadWithWebpack5(phase, dir)) | ||
} catch { | ||
// If this errors, it likely will do so again upon boot, so we just swallow | ||
// it here. | ||
} finally { | ||
worker.end() | ||
} | ||
|
||
install(useWebpack5) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters