Skip to content

Commit a9ab4c7

Browse files
authored
fix(webpack-dev-server): Pass in jsconfig to get webpack config for Next.js >= 13.2.1 (#26005)
1 parent 79b2c38 commit a9ab4c7

File tree

7 files changed

+269
-3
lines changed

7 files changed

+269
-3
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-12', 'next-12.1.6', 'next-13']
4+
const WEBPACK_REACT: ProjectFixtureDir[] = ['next-12', 'next-12.1.6', 'next-13', 'next-13-tsconfig']
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

+45-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export async function nextHandler (devServerConfig: WebpackDevServerConfig): Pro
2929
*/
3030
function getNextJsPackages (devServerConfig: WebpackDevServerConfig) {
3131
const resolvePaths = { paths: [devServerConfig.cypressConfig.projectRoot] }
32-
const packages = {} as { loadConfig: Function, getNextJsBaseWebpackConfig: Function }
32+
const packages = {} as { loadConfig: Function, getNextJsBaseWebpackConfig: Function, nextLoadJsConfig: Function }
3333

3434
try {
3535
const loadConfigPath = require.resolve('next/dist/server/config', resolvePaths)
@@ -47,6 +47,14 @@ function getNextJsPackages (devServerConfig: WebpackDevServerConfig) {
4747
throw new Error(`Failed to load "next/dist/build/webpack-config" with error: ${ e.message ?? e}`)
4848
}
4949

50+
try {
51+
const loadJsConfigPath = require.resolve('next/dist/build/load-jsconfig', resolvePaths)
52+
53+
packages.nextLoadJsConfig = require(loadJsConfigPath).default
54+
} catch (e: any) {
55+
throw new Error(`Failed to load "next/dist/build/load-jsconfig" with error: ${ e.message ?? e}`)
56+
}
57+
5058
return packages
5159
}
5260

@@ -131,13 +139,46 @@ function getNextJsPackages (devServerConfig: WebpackDevServerConfig) {
131139
middlewareMatchers?: MiddlewareMatcher[]
132140
}
133141
]
142+
143+
* - v13.2.1
144+
[
145+
dir: string,
146+
options: {
147+
buildId: string
148+
config: NextConfigComplete
149+
compilerType: CompilerNameValues
150+
dev?: boolean
151+
entrypoints: webpack.EntryObject
152+
isDevFallback?: boolean
153+
pagesDir?: string
154+
reactProductionProfiling?: boolean
155+
rewrites: CustomRoutes['rewrites']
156+
runWebpackSpan: Span
157+
target?: string
158+
appDir?: string
159+
middlewareMatchers?: MiddlewareMatcher[]
160+
noMangling?: boolean
161+
jsConfig: any
162+
resolvedBaseUrl: string | undefined
163+
supportedBrowsers: string[] | undefined
164+
clientRouterFilters?: {
165+
staticFilter: ReturnType<
166+
import('../shared/lib/bloom-filter').BloomFilter['export']
167+
>
168+
dynamicFilter: ReturnType<
169+
import('../shared/lib/bloom-filter').BloomFilter['export']
170+
>
171+
}
172+
}
173+
]
134174
*/
135175
async function loadWebpackConfig (devServerConfig: WebpackDevServerConfig): Promise<Configuration> {
136-
const { loadConfig, getNextJsBaseWebpackConfig } = getNextJsPackages(devServerConfig)
176+
const { loadConfig, getNextJsBaseWebpackConfig, nextLoadJsConfig } = getNextJsPackages(devServerConfig)
137177

138178
const nextConfig = await loadConfig('development', devServerConfig.cypressConfig.projectRoot)
139179
const runWebpackSpan = getRunWebpackSpan(devServerConfig)
140180
const reactVersion = getReactVersion(devServerConfig.cypressConfig.projectRoot)
181+
const jsConfigResult = await nextLoadJsConfig?.(devServerConfig.cypressConfig.projectRoot, nextConfig)
141182

142183
const webpackConfig = await getNextJsBaseWebpackConfig(
143184
devServerConfig.cypressConfig.projectRoot,
@@ -155,6 +196,8 @@ async function loadWebpackConfig (devServerConfig: WebpackDevServerConfig): Prom
155196
compilerType: 'client',
156197
// Required for Next.js > 13
157198
hasReactRoot: reactVersion === 18,
199+
// Required for Next.js > 13.2.1 to respect TS/JS config
200+
jsConfig: jsConfigResult.jsConfig,
158201
},
159202
)
160203

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Importing global styles fails with Next.js due to restrictions on style imports.
2+
// We modify the Next Webpack config to allow importing global styles.
3+
// We are not using a relative path here because baseUrl is configured in tsconfig.json
4+
import 'globals.css'
5+
import 'Home.module.css'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/** @type {import('next').NextConfig} */
2+
const nextConfig = {
3+
reactStrictMode: true,
4+
swcMinify: true,
5+
}
6+
7+
module.exports = nextConfig
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "next-latest",
3+
"version": "0.1.0",
4+
"private": true,
5+
"scripts": {
6+
"dev": "next dev",
7+
"build": "next build",
8+
"start": "next start",
9+
"lint": "next lint"
10+
},
11+
"dependencies": {
12+
"next": "13.2.1",
13+
"react": "18.2.0",
14+
"react-dom": "18.2.0"
15+
},
16+
"projectFixtureDirectory": "next"
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// This is to test a bug in Next.js 13.2.1 where TS/JS config is not respected
2+
{
3+
"compilerOptions": {
4+
"baseUrl": ".",
5+
"paths": {
6+
"*": ["styles/*"]
7+
}
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
6+
version "13.2.1"
7+
resolved "https://registry.yarnpkg.com/@next/env/-/env-13.2.1.tgz#082d42cfc0c794e9185d7b4133d71440ba2e795d"
8+
integrity sha512-Hq+6QZ6kgmloCg8Kgrix+4F0HtvLqVK3FZAnlAoS0eonaDemHe1Km4kwjSWRE3JNpJNcKxFHF+jsZrYo0SxWoQ==
9+
10+
11+
version "13.2.1"
12+
resolved "https://registry.yarnpkg.com/@next/swc-android-arm-eabi/-/swc-android-arm-eabi-13.2.1.tgz#67f2580fbbe05ee006220688972c5e3a555fc741"
13+
integrity sha512-Yua7mUpEd1wzIT6Jjl3dpRizIfGp9NR4F2xeRuQv+ae+SDI1Em2WyM9m46UL+oeW5GpMiEHoaBagr47RScZFmQ==
14+
15+
16+
version "13.2.1"
17+
resolved "https://registry.yarnpkg.com/@next/swc-android-arm64/-/swc-android-arm64-13.2.1.tgz#460a02b69eb23bb5f402266bcea9cadae59415c1"
18+
integrity sha512-Bifcr2f6VwInOdq1uH/9lp8fH7Nf7XGkIx4XceVd32LPJqG2c6FZU8ZRBvTdhxzXVpt5TPtuXhOP4Ij9UPqsVw==
19+
20+
21+
version "13.2.1"
22+
resolved "https://registry.yarnpkg.com/@next/swc-darwin-arm64/-/swc-darwin-arm64-13.2.1.tgz#8b8530ff417802027471aee2419f78a58a863ccb"
23+
integrity sha512-gvqm+fGMYxAkwBapH0Vvng5yrb6HTkIvZfY4oEdwwYrwuLdkjqnJygCMgpNqIFmAHSXgtlWxfYv1VC8sjN81Kw==
24+
25+
26+
version "13.2.1"
27+
resolved "https://registry.yarnpkg.com/@next/swc-darwin-x64/-/swc-darwin-x64-13.2.1.tgz#80aebb3329a1e4568a28de1ee177780b3d50330c"
28+
integrity sha512-HGqVqmaZWj6zomqOZUVbO5NhlABL0iIaxTmd0O5B0MoMa5zpDGoaHSG+fxgcWMXcGcxmUNchv1NfNOYiTKoHOg==
29+
30+
31+
version "13.2.1"
32+
resolved "https://registry.yarnpkg.com/@next/swc-freebsd-x64/-/swc-freebsd-x64-13.2.1.tgz#250ea2ab7e1734f22d11c677c463fab9ac33a516"
33+
integrity sha512-N/a4JarAq+E+g+9K2ywJUmDIgU2xs2nA+BBldH0oq4zYJMRiUhL0iaN9G4e72VmGOJ61L/3W6VN8RIUOwTLoqQ==
34+
35+
36+
version "13.2.1"
37+
resolved "https://registry.yarnpkg.com/@next/swc-linux-arm-gnueabihf/-/swc-linux-arm-gnueabihf-13.2.1.tgz#fe6bb29ed348a5f8ecae3740df22a8d8130c474a"
38+
integrity sha512-WaFoerF/eRbhbE57TaIGJXbQAERADZ/RZ45u6qox9beb5xnWsyYgzX+WuN7Tkhyvga0/aMuVYFzS9CEay7D+bw==
39+
40+
41+
version "13.2.1"
42+
resolved "https://registry.yarnpkg.com/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-13.2.1.tgz#4781b927fc5e421f3cea2b29e5d38e5e4837b198"
43+
integrity sha512-R+Jhc1/RJTnncE9fkePboHDNOCm1WJ8daanWbjKhfPySMyeniKYRwGn5SLYW3S8YlRS0QVdZaaszDSZWgUcsmA==
44+
45+
46+
version "13.2.1"
47+
resolved "https://registry.yarnpkg.com/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-13.2.1.tgz#c2ba0a121b0255ba62450916bc70e6d0e26cbc98"
48+
integrity sha512-oI1UfZPidGAVddlL2eOTmfsuKV9EaT1aktIzVIxIAgxzQSdwsV371gU3G55ggkurzfdlgF3GThFePDWF0d8dmw==
49+
50+
51+
version "13.2.1"
52+
resolved "https://registry.yarnpkg.com/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-13.2.1.tgz#573c220f8b087e5d131d1fba58d3e1a670b220ad"
53+
integrity sha512-PCygPwrQmS+7WUuAWWioWMZCzZm4PG91lfRxToLDg7yIm/3YfAw5N2EK2TaM9pzlWdvHQAqRMX/oLvv027xUiA==
54+
55+
56+
version "13.2.1"
57+
resolved "https://registry.yarnpkg.com/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-13.2.1.tgz#950b5bb920b322ca7b447efbd12a9c7a10c3a642"
58+
integrity sha512-sUAKxo7CFZYGHNxheGh9nIBElLYBM6md/liEGfOTwh/xna4/GTTcmkGWkF7PdnvaYNgcPIQgHIMYiAa6yBKAVw==
59+
60+
61+
version "13.2.1"
62+
resolved "https://registry.yarnpkg.com/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-13.2.1.tgz#dbff3c4f5a3812a7059dac05804148a0f98682db"
63+
integrity sha512-qDmyEjDBpl/vBXxuOOKKWmPQOcARcZIMach1s7kjzaien0SySut/PHRlj56sosa81Wt4hTGhfhZ1R7g1n7+B8w==
64+
65+
66+
version "13.2.1"
67+
resolved "https://registry.yarnpkg.com/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-13.2.1.tgz#7d2c17be7b8d9963984f5c15cc2588127101f620"
68+
integrity sha512-2joqFQ81ZYPg6DcikIzQn3DgjKglNhPAozx6dL5sCNkr1CPMD0YIkJgT3CnYyMHQ04Qi3Npv0XX3MD6LJO8OCA==
69+
70+
71+
version "13.2.1"
72+
resolved "https://registry.yarnpkg.com/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-13.2.1.tgz#09713c6a925461f414e89422851326d1625bd4d2"
73+
integrity sha512-r3+0fSaIZT6N237iMzwUhfNwjhAFvXjqB+4iuW+wcpxW+LHm1g/IoxN8eSRcb8jPItC86JxjAxpke0QL97qd6g==
74+
75+
76+
version "0.4.14"
77+
resolved "https://registry.yarnpkg.com/@swc/helpers/-/helpers-0.4.14.tgz#1352ac6d95e3617ccb7c1498ff019654f1e12a74"
78+
integrity sha512-4C7nX/dvpzB7za4Ql9K81xK3HPxCpHMgwTZVyf+9JQ6VUbn9jjZVN7/Nkdz/Ugzs2CSjqnL/UPXroiVBVHUWUw==
79+
dependencies:
80+
tslib "^2.4.0"
81+
82+
caniuse-lite@^1.0.30001406:
83+
version "1.0.30001425"
84+
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001425.tgz#52917791a453eb3265143d2cd08d80629e82c735"
85+
integrity sha512-/pzFv0OmNG6W0ym80P3NtapU0QEiDS3VuYAZMGoLLqiC7f6FJFe1MjpQDREGApeenD9wloeytmVDj+JLXPC6qw==
86+
87+
88+
version "0.0.1"
89+
resolved "https://registry.yarnpkg.com/client-only/-/client-only-0.0.1.tgz#38bba5d403c41ab150bff64a95c85013cf73bca1"
90+
integrity sha512-IV3Ou0jSMzZrd3pZ48nLkT9DA7Ag1pnPzaiQhpW7c3RbcqqzvzzVu+L8gfqMp/8IM2MQtSiqaCxrrcfu8I8rMA==
91+
92+
"js-tokens@^3.0.0 || ^4.0.0":
93+
version "4.0.0"
94+
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
95+
integrity sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==
96+
97+
loose-envify@^1.1.0:
98+
version "1.4.0"
99+
resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.4.0.tgz#71ee51fa7be4caec1a63839f7e682d8132d30caf"
100+
integrity sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==
101+
dependencies:
102+
js-tokens "^3.0.0 || ^4.0.0"
103+
104+
nanoid@^3.3.4:
105+
version "3.3.4"
106+
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.3.4.tgz#730b67e3cd09e2deacf03c027c81c9d9dbc5e8ab"
107+
integrity sha512-MqBkQh/OHTS2egovRtLk45wEyNXwF+cokD+1YPf9u5VfJiRdAiRwB2froX5Co9Rh20xs4siNPm8naNotSD6RBw==
108+
109+
110+
version "13.2.1"
111+
resolved "https://registry.yarnpkg.com/next/-/next-13.2.1.tgz#34d823f518632b36379863228ed9f861c335b9c0"
112+
integrity sha512-qhgJlDtG0xidNViJUPeQHLGJJoT4zDj/El7fP3D3OzpxJDUfxsm16cK4WTMyvSX1ciIfAq05u+0HqFAa+VJ+Hg==
113+
dependencies:
114+
"@next/env" "13.2.1"
115+
"@swc/helpers" "0.4.14"
116+
caniuse-lite "^1.0.30001406"
117+
postcss "8.4.14"
118+
styled-jsx "5.1.1"
119+
optionalDependencies:
120+
"@next/swc-android-arm-eabi" "13.2.1"
121+
"@next/swc-android-arm64" "13.2.1"
122+
"@next/swc-darwin-arm64" "13.2.1"
123+
"@next/swc-darwin-x64" "13.2.1"
124+
"@next/swc-freebsd-x64" "13.2.1"
125+
"@next/swc-linux-arm-gnueabihf" "13.2.1"
126+
"@next/swc-linux-arm64-gnu" "13.2.1"
127+
"@next/swc-linux-arm64-musl" "13.2.1"
128+
"@next/swc-linux-x64-gnu" "13.2.1"
129+
"@next/swc-linux-x64-musl" "13.2.1"
130+
"@next/swc-win32-arm64-msvc" "13.2.1"
131+
"@next/swc-win32-ia32-msvc" "13.2.1"
132+
"@next/swc-win32-x64-msvc" "13.2.1"
133+
134+
picocolors@^1.0.0:
135+
version "1.0.0"
136+
resolved "https://registry.yarnpkg.com/picocolors/-/picocolors-1.0.0.tgz#cb5bdc74ff3f51892236eaf79d68bc44564ab81c"
137+
integrity sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ==
138+
139+
140+
version "8.4.14"
141+
resolved "https://registry.yarnpkg.com/postcss/-/postcss-8.4.14.tgz#ee9274d5622b4858c1007a74d76e42e56fd21caf"
142+
integrity sha512-E398TUmfAYFPBSdzgeieK2Y1+1cpdxJx8yXbK/m57nRhKSmk1GB2tO4lbLBtlkfPQTDKfe4Xqv1ASWPpayPEig==
143+
dependencies:
144+
nanoid "^3.3.4"
145+
picocolors "^1.0.0"
146+
source-map-js "^1.0.2"
147+
148+
149+
version "18.2.0"
150+
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-18.2.0.tgz#22aaf38708db2674ed9ada224ca4aa708d821e3d"
151+
integrity sha512-6IMTriUmvsjHUjNtEDudZfuDQUoWXVxKHhlEGSk81n4YFS+r/Kl99wXiwlVXtPBtJenozv2P+hxDsw9eA7Xo6g==
152+
dependencies:
153+
loose-envify "^1.1.0"
154+
scheduler "^0.23.0"
155+
156+
157+
version "18.2.0"
158+
resolved "https://registry.yarnpkg.com/react/-/react-18.2.0.tgz#555bd98592883255fa00de14f1151a917b5d77d5"
159+
integrity sha512-/3IjMdb2L9QbBdWiW5e3P2/npwMBaU9mHCSCUzNln0ZCYbcfTsGbTJrU/kGemdH2IWmB2ioZ+zkxtmq6g09fGQ==
160+
dependencies:
161+
loose-envify "^1.1.0"
162+
163+
scheduler@^0.23.0:
164+
version "0.23.0"
165+
resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.23.0.tgz#ba8041afc3d30eb206a487b6b384002e4e61fdfe"
166+
integrity sha512-CtuThmgHNg7zIZWAXi3AsyIzA3n4xx7aNyjwC2VJldO2LMVDhFK+63xGqq6CsJH4rTAt6/M+N4GhZiDYPx9eUw==
167+
dependencies:
168+
loose-envify "^1.1.0"
169+
170+
source-map-js@^1.0.2:
171+
version "1.0.2"
172+
resolved "https://registry.yarnpkg.com/source-map-js/-/source-map-js-1.0.2.tgz#adbc361d9c62df380125e7f161f71c826f1e490c"
173+
integrity sha512-R0XvVJ9WusLiqTCEiGCmICCMplcCkIwwR11mOSD9CR5u+IXYdiseeEuXCVAjS54zqwkLcPNnmU4OeJ6tUrWhDw==
174+
175+
176+
version "5.1.1"
177+
resolved "https://registry.yarnpkg.com/styled-jsx/-/styled-jsx-5.1.1.tgz#839a1c3aaacc4e735fed0781b8619ea5d0009d1f"
178+
integrity sha512-pW7uC1l4mBZ8ugbiZrcIsiIvVx1UmTfw7UkC3Um2tmfUq9Bhk8IiyEIPl6F8agHgjzku6j0xQEZbfA5uSgSaCw==
179+
dependencies:
180+
client-only "0.0.1"
181+
182+
tslib@^2.4.0:
183+
version "2.4.0"
184+
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.4.0.tgz#7cecaa7f073ce680a05847aa77be941098f36dc3"
185+
integrity sha512-d6xOpEDfsi2CZVlPQzGeux8XMwLT9hssAsaPYExaQMuYskwb+x1x7J371tWlbBdWHroy99KnVB6qIkUbs5X3UQ==

0 commit comments

Comments
 (0)