Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(webpack-dev-server): Pass in jsconfig to get webpack config for Next.js >= 13.2.1 #26005

Merged
merged 4 commits into from
Mar 6, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions npm/webpack-dev-server/src/helpers/nextHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function nextHandler (devServerConfig: WebpackDevServerConfig): Pro
*/
function getNextJsPackages (devServerConfig: WebpackDevServerConfig) {
const resolvePaths = { paths: [devServerConfig.cypressConfig.projectRoot] }
const packages = {} as { loadConfig: Function, getNextJsBaseWebpackConfig: Function }
const packages = {} as { loadConfig: Function, getNextJsBaseWebpackConfig: Function, nextLoadProjectInfo: Function }

try {
const loadConfigPath = require.resolve('next/dist/server/config', resolvePaths)
Expand All @@ -42,7 +42,10 @@ function getNextJsPackages (devServerConfig: WebpackDevServerConfig) {
try {
const getNextJsBaseWebpackConfigPath = require.resolve('next/dist/build/webpack-config', resolvePaths)

packages.getNextJsBaseWebpackConfig = require(getNextJsBaseWebpackConfigPath).default
const { default: getNextJsBaseWebpackConfig, loadProjectInfo } = require(getNextJsBaseWebpackConfigPath)

packages.getNextJsBaseWebpackConfig = getNextJsBaseWebpackConfig
packages.nextLoadProjectInfo = loadProjectInfo
} catch (e: any) {
throw new Error(`Failed to load "next/dist/build/webpack-config" with error: ${ e.message ?? e}`)
}
Expand Down Expand Up @@ -131,13 +134,46 @@ function getNextJsPackages (devServerConfig: WebpackDevServerConfig) {
middlewareMatchers?: MiddlewareMatcher[]
}
]

* - v13.2.1
[
dir: string,
options: {
buildId: string
config: NextConfigComplete
compilerType: CompilerNameValues
dev?: boolean
entrypoints: webpack.EntryObject
isDevFallback?: boolean
pagesDir?: string
reactProductionProfiling?: boolean
rewrites: CustomRoutes['rewrites']
runWebpackSpan: Span
target?: string
appDir?: string
middlewareMatchers?: MiddlewareMatcher[]
noMangling?: boolean
jsConfig: any
resolvedBaseUrl: string | undefined
supportedBrowsers: string[] | undefined
clientRouterFilters?: {
staticFilter: ReturnType<
import('../shared/lib/bloom-filter').BloomFilter['export']
>
dynamicFilter: ReturnType<
import('../shared/lib/bloom-filter').BloomFilter['export']
>
}
}
]
*/
async function loadWebpackConfig (devServerConfig: WebpackDevServerConfig): Promise<Configuration> {
const { loadConfig, getNextJsBaseWebpackConfig } = getNextJsPackages(devServerConfig)
const { loadConfig, getNextJsBaseWebpackConfig, nextLoadProjectInfo } = getNextJsPackages(devServerConfig)

const nextConfig = await loadConfig('development', devServerConfig.cypressConfig.projectRoot)
const runWebpackSpan = getRunWebpackSpan(devServerConfig)
const reactVersion = getReactVersion(devServerConfig.cypressConfig.projectRoot)
const projectInfo = await nextLoadProjectInfo?.({ dir: devServerConfig.cypressConfig.projectRoot, config: nextConfig, dev: true })
astone123 marked this conversation as resolved.
Show resolved Hide resolved

const webpackConfig = await getNextJsBaseWebpackConfig(
devServerConfig.cypressConfig.projectRoot,
Expand All @@ -155,6 +191,8 @@ async function loadWebpackConfig (devServerConfig: WebpackDevServerConfig): Prom
compilerType: 'client',
// Required for Next.js > 13
hasReactRoot: reactVersion === 18,
// Required for Next.js > 13.2.1 to respect TS/JS config
jsConfig: projectInfo?.jsConfig,
},
)

Expand Down