Skip to content

Commit cc4244d

Browse files
authored
feat: add support for next 13 (#24396)
1 parent 7c9a0c6 commit cc4244d

File tree

11 files changed

+301
-5564
lines changed

11 files changed

+301
-5564
lines changed

npm/webpack-dev-server/cypress/e2e/next.cy.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// <reference path="../support/e2e.ts" />
22
import type { ProjectFixtureDir } from '@tooling/system-tests/lib/fixtureDirs'
33

4-
const WEBPACK_REACT: ProjectFixtureDir[] = ['next-11', 'next-12', 'next-11-webpack-4', 'next-12.1.6']
4+
const WEBPACK_REACT: ProjectFixtureDir[] = ['next-12', 'next-12.1.6', 'next-13']
55

66
// Add to this list to focus on a particular permutation
77
const ONLY_PROJECTS: ProjectFixtureDir[] = []

npm/webpack-dev-server/src/helpers/nextHandler.ts

+97
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,95 @@ function getNextJsPackages (devServerConfig: WebpackDevServerConfig) {
5050
return packages
5151
}
5252

53+
/**
54+
* Types for `getNextJsBaseWebpackConfig` based on version:
55+
* - v11.1.4
56+
[
57+
dir: string,
58+
options: {
59+
buildId: string
60+
config: NextConfigComplete
61+
dev?: boolean
62+
isServer?: boolean
63+
pagesDir: string
64+
target?: string
65+
reactProductionProfiling?: boolean
66+
entrypoints: WebpackEntrypoints
67+
rewrites: CustomRoutes['rewrites']
68+
isDevFallback?: boolean
69+
runWebpackSpan: Span
70+
}
71+
]
72+
73+
* - v12.0.0 = Same as v11.1.4
74+
75+
* - v12.1.6
76+
[
77+
dir: string,
78+
options: {
79+
buildId: string
80+
config: NextConfigComplete
81+
compilerType: 'client' | 'server' | 'edge-server'
82+
dev?: boolean
83+
entrypoints: webpack5.EntryObject
84+
hasReactRoot: boolean
85+
isDevFallback?: boolean
86+
pagesDir: string
87+
reactProductionProfiling?: boolean
88+
rewrites: CustomRoutes['rewrites']
89+
runWebpackSpan: Span
90+
target?: string
91+
}
92+
]
93+
94+
* - v13.0.0
95+
[
96+
dir: string,
97+
options: {
98+
buildId: string
99+
config: NextConfigComplete
100+
compilerType: CompilerNameValues
101+
dev?: boolean
102+
entrypoints: webpack.EntryObject
103+
hasReactRoot: boolean
104+
isDevFallback?: boolean
105+
pagesDir?: string
106+
reactProductionProfiling?: boolean
107+
rewrites: CustomRoutes['rewrites']
108+
runWebpackSpan: Span
109+
target?: string
110+
appDir?: string
111+
middlewareMatchers?: MiddlewareMatcher[]
112+
}
113+
]
114+
115+
* - v13.0.1
116+
[
117+
dir: string,
118+
options: {
119+
buildId: string
120+
config: NextConfigComplete
121+
compilerType: CompilerNameValues
122+
dev?: boolean
123+
entrypoints: webpack.EntryObject
124+
isDevFallback?: boolean
125+
pagesDir?: string
126+
reactProductionProfiling?: boolean
127+
rewrites: CustomRoutes['rewrites']
128+
runWebpackSpan: Span
129+
target?: string
130+
appDir?: string
131+
middlewareMatchers?: MiddlewareMatcher[]
132+
}
133+
]
134+
*/
53135
async function loadWebpackConfig (devServerConfig: WebpackDevServerConfig): Promise<Configuration> {
54136
const { loadConfig, getNextJsBaseWebpackConfig } = getNextJsPackages(devServerConfig)
55137

56138
const nextConfig = await loadConfig('development', devServerConfig.cypressConfig.projectRoot)
57139
const runWebpackSpan = getRunWebpackSpan(devServerConfig)
140+
const reactVersion = getReactVersion(devServerConfig.cypressConfig.projectRoot)
141+
58142
const webpackConfig = await getNextJsBaseWebpackConfig(
59143
devServerConfig.cypressConfig.projectRoot,
60144
{
@@ -69,6 +153,8 @@ async function loadWebpackConfig (devServerConfig: WebpackDevServerConfig): Prom
69153
isServer: false,
70154
// Client webpack config for Next.js > 12.1.5
71155
compilerType: 'client',
156+
// Required for Next.js > 13
157+
hasReactRoot: reactVersion === 18,
72158
},
73159
)
74160

@@ -300,3 +386,14 @@ function changeNextCachePath (webpackConfig: Configuration) {
300386
debug('Changing Next cache path from %s to %s', cacheDirectory, webpackConfig.cache.cacheDirectory)
301387
}
302388
}
389+
390+
function getReactVersion (projectRoot: string): number | undefined {
391+
try {
392+
const reactPackageJsonPath = require.resolve('react/package.json', { paths: [projectRoot] })
393+
const { version } = require(reactPackageJsonPath)
394+
395+
return Number(version.split('.')[0])
396+
} catch (e) {
397+
debug('Failed to source react with error: ', e)
398+
}
399+
}

npm/webpack-dev-server/test/handlers/nextHandler.spec.ts

-40
Original file line numberDiff line numberDiff line change
@@ -74,46 +74,6 @@ describe('nextHandler', function () {
7474
expect(sourceWebpackModulesResult.webpack.majorVersion).eq(5)
7575
})
7676

77-
it('sources from a next-11 project', async () => {
78-
const projectRoot = await scaffoldMigrationProject('next-11')
79-
80-
process.chdir(projectRoot)
81-
82-
const { frameworkConfig: webpackConfig, sourceWebpackModulesResult } = await nextHandler({
83-
framework: 'next',
84-
cypressConfig: { projectRoot } as Cypress.PluginConfigOptions,
85-
} as WebpackDevServerConfig)
86-
87-
expectWatchOverrides(webpackConfig)
88-
expectPagesDir(webpackConfig, projectRoot)
89-
expectWebpackSpan(webpackConfig)
90-
expectGlobalStyleOverrides(webpackConfig)
91-
expectCacheOverrides(webpackConfig, projectRoot)
92-
93-
expect(sourceWebpackModulesResult.webpack.importPath).to.include('next')
94-
expect(sourceWebpackModulesResult.webpack.majorVersion).eq(5)
95-
})
96-
97-
it('sources from a next-11-webpack-4 project', async () => {
98-
const projectRoot = await scaffoldMigrationProject('next-11-webpack-4')
99-
100-
process.chdir(projectRoot)
101-
102-
const { frameworkConfig: webpackConfig, sourceWebpackModulesResult } = await nextHandler({
103-
framework: 'next',
104-
cypressConfig: { projectRoot, cypressBinaryRoot: __dirname } as Cypress.PluginConfigOptions,
105-
} as WebpackDevServerConfig)
106-
107-
expectWatchOverrides(webpackConfig)
108-
expectPagesDir(webpackConfig, projectRoot)
109-
expectWebpackSpan(webpackConfig)
110-
expectGlobalStyleOverrides(webpackConfig)
111-
expectCacheOverrides(webpackConfig, projectRoot)
112-
113-
expect(sourceWebpackModulesResult.webpack.importPath).to.include('next')
114-
expect(sourceWebpackModulesResult.webpack.majorVersion).eq(4)
115-
})
116-
11777
it('throws if nodeVersion is set to bundled', async () => {
11878
const projectRoot = await scaffoldMigrationProject('next-12')
11979

packages/scaffold-config/src/dependencies.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export const WIZARD_DEPENDENCY_NEXT = {
9494
package: 'next',
9595
installer: 'next',
9696
description: 'The React Framework for Production',
97-
minVersion: '^=10.0.0 || ^=11.0.0 || ^=12.0.0',
97+
minVersion: '^=10.0.0 || ^=11.0.0 || ^=12.0.0 || ^=13.0.0',
9898
} as const
9999

100100
export const WIZARD_DEPENDENCY_ANGULAR_CLI = {

system-tests/projects/next-11-webpack-4/next.config.js

-3
This file was deleted.

0 commit comments

Comments
 (0)