Skip to content

Commit e1502bc

Browse files
timneutkensijjk
andauthored
Enable webpack5 for all apps (vercel#25639)
* Enable webpack 5 by default for all apps Still provides a way to opt-out using `webpack5: false` in next.config.js. Also throws an error for `future.webpack5`. * Update tests * Update test to run on webpack 4 instead of webpack 5 * disable webpack5 for legacy tests * Fix stats-config for webpack4 * update tests * update size for webpack4 test * move basic suite first * update basic test * Add logs * remove outdated testFutureDependencies job Co-authored-by: JJ Kasper <[email protected]>
1 parent 069f7e8 commit e1502bc

File tree

18 files changed

+116
-112
lines changed

18 files changed

+116
-112
lines changed

.github/workflows/build_test_deploy.yml

+4-32
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,15 @@ jobs:
156156
steps:
157157
- run: exit 0
158158

159-
testFutureDependencies:
160-
name: Webpack 5 (Basic, Production, Acceptance)
159+
testLegacyWebpack:
160+
name: Webpack 4 (Basic, Production, Acceptance)
161161
runs-on: ubuntu-latest
162162
needs: build
163163
env:
164164
NEXT_TELEMETRY_DISABLED: 1
165165
NEXT_TEST_JOB: 1
166166
HEADLESS: true
167-
NEXT_PRIVATE_TEST_WEBPACK5_MODE: 1
167+
NEXT_PRIVATE_TEST_WEBPACK4_MODE: 1
168168

169169
steps:
170170
- uses: actions/cache@v2
@@ -174,37 +174,9 @@ jobs:
174174
path: ./*
175175
key: ${{ github.sha }}
176176

177-
- run: xvfb-run node run-tests.js test/integration/{fallback-modules,link-ref,production,basic,async-modules,font-optimization,ssr-ctx}/test/index.test.js test/acceptance/*.test.js
177+
- run: xvfb-run node run-tests.js test/integration/{basic,fallback-modules,link-ref,production,async-modules,font-optimization,ssr-ctx}/test/index.test.js test/acceptance/*.test.js
178178
if: ${{needs.build.outputs.docsChange != 'docs only change'}}
179179

180-
testLegacyWebpack:
181-
name: Webpack 4 (Basic, Production, Acceptance)
182-
runs-on: ubuntu-latest
183-
env:
184-
NEXT_TELEMETRY_DISABLED: 1
185-
NEXT_TEST_JOB: 1
186-
HEADLESS: true
187-
188-
steps:
189-
- uses: actions/checkout@v2
190-
with:
191-
fetch-depth: 25
192-
193-
- run: echo ::set-output name=DOCS_CHANGE::$(node skip-docs-change.js echo 'not-docs-only-change')
194-
id: docs-change
195-
196-
- run: git fetch --depth=1 origin +refs/tags/*:refs/tags/*
197-
if: ${{ steps.docs-change.outputs.DOCS_CHANGE != 'docs only change' }}
198-
199-
- run: yarn install --check-files
200-
if: ${{ steps.docs-change.outputs.DOCS_CHANGE != 'docs only change' }}
201-
202-
- run: yarn list react react-dom
203-
if: ${{ steps.docs-change.outputs.DOCS_CHANGE != 'docs only change' }}
204-
205-
- run: xvfb-run node run-tests.js test/integration/{link-ref,production,basic,async-modules,font-optimization,ssr-ctx,worker-loader}/test/index.test.js test/acceptance/*.test.js
206-
if: ${{ steps.docs-change.outputs.DOCS_CHANGE != 'docs only change' }}
207-
208180
testFirefox:
209181
name: Test Firefox (production)
210182
runs-on: ubuntu-latest
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# `future.webpack5` has been moved to `webpack5`
2+
3+
#### Why This Error Occurred
4+
5+
The `future.webpack5` option has been moved to `webpack5` in `next.config.js`.
6+
7+
#### Possible Ways to Fix It
8+
9+
If you had the value `true` you can removed the option as webpack 5 is now the default for all Next.js apps unless opted out.
10+
11+
If you had he value `false` you can update `next.config.js`:
12+
13+
Change `future.webpack5` to `webpack5`.
14+
15+
Current `next.config.js`:
16+
17+
```js
18+
// next.config.js
19+
module.exports = {
20+
future: {
21+
webpack5: false,
22+
},
23+
}
24+
```
25+
26+
Updated `next.config.js`:
27+
28+
```js
29+
// next.config.js
30+
module.exports = {
31+
webpack5: false,
32+
}
33+
```

errors/webpack5.md

+11-9
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
#### Why This Message Occurred
44

5-
Next.js will soon adopt webpack 5 as the default for compilation. We've spent a lot of effort into ensuring the transition from webpack 4 to 5 will be as smooth as possible. For example Next.js now comes with both webpack 4 and 5 allowing you to adopt webpack 5 by adding a flag to your `next.config.js`:
5+
Next.js has adopted webpack 5 as the default for compilation. We've spent a lot of effort into ensuring the transition from webpack 4 to 5 will be as smooth as possible. For example Next.js now comes with both webpack 4 and 5 allowing you to adopt webpack 5 by adding a flag to your `next.config.js`:
66

77
```js
88
module.exports = {
9-
future: {
10-
webpack5: true,
11-
},
9+
// Webpack 5 is enabled by default
10+
// You can still use webpack 4 while upgrading to the latest version of Next.js by adding the "webpack5: false" flag
11+
webpack5: false,
1212
}
1313
```
1414

15-
Adopting webpack 5 in your application has many benefits, notably:
15+
Using webpack 5 in your application has many benefits, notably:
1616

1717
- Improved Disk Caching: `next build` is significantly faster on subsequent builds
1818
- Improved Fast Refresh: Fast Refresh work is prioritized
@@ -22,11 +22,13 @@ Adopting webpack 5 in your application has many benefits, notably:
2222
- Support for web workers using `new Worker(new URL("worker.js", import.meta.url))`
2323
- Support for `exports`/`imports` field in `package.json`
2424

25-
In upcoming releases we'll gradually roll out webpack 5 to applications that are compatible with webpack 5:
25+
In the past releases we have gradually rolled out webpack 5 to Next.js applications:
2626

27-
- In the next minor version we'll automatically opt-in applications without custom webpack configuration in `next.config.js`
28-
- In the next minor version we'll automatically opt-in applications that do not have a `next.config.js`
29-
- In the next major version we'll enable webpack 5 by default. You'll still be able to opt-out and use webpack 4 to help with backwards compatibility
27+
- In Next.js 10.2 we automatically opted-in applications without custom webpack configuration in `next.config.js`
28+
- In Next.js 10.2 we automatically opted-in applications that do not have a `next.config.js`
29+
- In Next.js 11 webpack 5 was enabled by default for all applications. You can still opt-out and use webpack 4 to help with backwards compatibility using `webpack5: false` in `next.config.js`
30+
31+
In the next major version webpack 4 support will be removed.
3032

3133
#### Custom webpack configuration
3234

packages/next/next-server/server/config-shared.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,16 @@ export type NextConfig = { [key: string]: any } & {
2727
}
2828
>
2929
redirects?: () => Promise<Redirect[]>
30-
3130
trailingSlash?: boolean
31+
webpack5?: false
3232

3333
future: {
34+
/**
35+
* @deprecated this options was moved to the top level
36+
*/
37+
webpack5?: false
3438
strictPostcssConfiguration?: boolean
3539
excludeDefaultMomentLocales?: boolean
36-
webpack5?: boolean
3740
}
3841
experimental: {
3942
cpus?: number
@@ -112,10 +115,11 @@ export const defaultConfig: NextConfig = {
112115
disableOptimizedLoading: true,
113116
gzipSize: true,
114117
},
118+
webpack5:
119+
Number(process.env.NEXT_PRIVATE_TEST_WEBPACK4_MODE) > 0 ? false : undefined,
115120
future: {
116121
strictPostcssConfiguration: false,
117122
excludeDefaultMomentLocales: false,
118-
webpack5: Number(process.env.NEXT_PRIVATE_TEST_WEBPACK5_MODE) > 0,
119123
},
120124
serverRuntimeConfig: {},
121125
publicRuntimeConfig: {},

packages/next/next-server/server/config-utils-worker.ts

+12-28
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@ export function install(useWebpack5: boolean) {
2222

2323
export type CheckReasons =
2424
| 'test-mode'
25-
| 'no-config'
25+
| 'default'
26+
| 'flag-disabled'
2627
| 'future-flag'
27-
| 'no-future-flag'
28-
| 'no-webpack-config'
29-
| 'webpack-config'
3028

3129
export type CheckResult = {
3230
enabled: boolean
@@ -43,20 +41,18 @@ export async function shouldLoadWithWebpack5(
4341
cwd: dir,
4442
})
4543

46-
if (Number(process.env.NEXT_PRIVATE_TEST_WEBPACK5_MODE) > 0) {
44+
if (Number(process.env.NEXT_PRIVATE_TEST_WEBPACK4_MODE) > 0) {
4745
return {
48-
enabled: true,
46+
enabled: false,
4947
reason: 'test-mode',
5048
}
5149
}
5250

53-
// No `next.config.js`:
51+
// Use webpack 5 by default in apps that do not have next.config.js
5452
if (!path?.length) {
55-
// Uncomment to add auto-enable when there is no next.config.js
56-
// Use webpack 5 by default in new apps:
5753
return {
5854
enabled: true,
59-
reason: 'no-config',
55+
reason: 'default',
6056
}
6157
}
6258

@@ -69,33 +65,21 @@ export async function shouldLoadWithWebpack5(
6965
userConfigModule.default || userConfigModule
7066
)
7167

72-
// Opted-in manually
73-
if (userConfig.future?.webpack5 === true) {
68+
if (userConfig.future?.webpack5) {
7469
return {
75-
enabled: true,
70+
enabled: false,
7671
reason: 'future-flag',
7772
}
7873
}
79-
80-
// Opted-out manually
81-
if (userConfig.future?.webpack5 === false) {
74+
if (userConfig.webpack5 === false) {
8275
return {
8376
enabled: false,
84-
reason: 'no-future-flag',
85-
}
86-
}
87-
88-
// Uncomment to add auto-enable when there is no custom webpack config
89-
// The user isn't configuring webpack
90-
if (!userConfig.webpack) {
91-
return {
92-
enabled: true,
93-
reason: 'no-webpack-config',
77+
reason: 'flag-disabled',
9478
}
9579
}
9680

9781
return {
98-
enabled: false,
99-
reason: 'webpack-config',
82+
enabled: true,
83+
reason: 'default',
10084
}
10185
}

packages/next/next-server/server/config-utils.ts

+22-18
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,10 @@ export { install, shouldLoadWithWebpack5 }
99

1010
function reasonMessage(reason: CheckReasons) {
1111
switch (reason) {
12-
case 'future-flag':
13-
return 'future.webpack5 option enabled'
14-
case 'no-future-flag':
15-
return 'future.webpack5 option disabled'
16-
case 'no-config':
17-
return 'no next.config.js'
18-
case 'webpack-config':
19-
return 'custom webpack configuration in next.config.js'
20-
case 'no-webpack-config':
21-
return 'no custom webpack configuration in next.config.js'
12+
case 'default':
13+
return 'Enabled by default'
14+
case 'flag-disabled':
15+
return 'webpack5 flag is set to false in next.config.js'
2216
case 'test-mode':
2317
return 'internal test mode'
2418
default:
@@ -27,7 +21,8 @@ function reasonMessage(reason: CheckReasons) {
2721
}
2822

2923
export async function loadWebpackHook(phase: string, dir: string) {
30-
let useWebpack5 = false
24+
let useWebpack5 = true
25+
let usesRemovedFlag = false
3126
const worker: any = new Worker(
3227
path.resolve(__dirname, './config-utils-worker.js'),
3328
{
@@ -37,13 +32,16 @@ export async function loadWebpackHook(phase: string, dir: string) {
3732
)
3833
try {
3934
const result: CheckResult = await worker.shouldLoadWithWebpack5(phase, dir)
40-
// Don't log which webpack version is being used when booting production server as it's not used after build
41-
if (phase !== PHASE_PRODUCTION_SERVER) {
42-
Log.info(
43-
`Using webpack ${result.enabled ? '5' : '4'}. Reason: ${reasonMessage(
44-
result.reason
45-
)} https://nextjs.org/docs/messages/webpack5`
46-
)
35+
if (result.reason === 'future-flag') {
36+
usesRemovedFlag = true
37+
} else {
38+
if (phase !== PHASE_PRODUCTION_SERVER) {
39+
Log.info(
40+
`Using webpack ${result.enabled ? '5' : '4'}. Reason: ${reasonMessage(
41+
result.reason
42+
)} https://nextjs.org/docs/messages/webpack5`
43+
)
44+
}
4745
}
4846

4947
useWebpack5 = Boolean(result.enabled)
@@ -54,5 +52,11 @@ export async function loadWebpackHook(phase: string, dir: string) {
5452
worker.end()
5553
}
5654

55+
if (usesRemovedFlag) {
56+
throw new Error(
57+
'`future.webpack5` in `next.config.js` has moved to the top level `webpack5` flag https://nextjs.org/docs/messages/future-webpack5-moved-to-webpack5'
58+
)
59+
}
60+
5761
install(useWebpack5)
5862
}

test/.stats-app/stats-config.js

+2-9
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@ module.exports = {
6565
content: `
6666
module.exports = {
6767
generateBuildId: () => 'BUILD_ID',
68-
future: {
69-
webpack5: true
70-
},
7168
webpack(config) {
7269
config.optimization.minimize = false
7370
config.optimization.minimizer = undefined
@@ -138,9 +135,7 @@ module.exports = {
138135
content: `
139136
module.exports = {
140137
generateBuildId: () => 'BUILD_ID',
141-
future: {
142-
webpack5: false
143-
},
138+
webpack5: false,
144139
webpack(config) {
145140
config.optimization.minimize = false
146141
config.optimization.minimizer = undefined
@@ -157,9 +152,7 @@ module.exports = {
157152
content: `
158153
module.exports = {
159154
generateBuildId: () => 'BUILD_ID',
160-
future: {
161-
webpack5: false
162-
}
155+
webpack5: false
163156
}
164157
`,
165158
},

test/integration/basic/test/index.test.js

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ describe('Basic Features', () => {
3636

3737
it('should polyfill Node.js modules', async () => {
3838
const browser = await webdriver(context.appPort, '/node-browser-polyfills')
39+
40+
console.error({
41+
logs: await browser.log('browser'),
42+
content: await browser.eval('document.documentElement.innerHTML'),
43+
})
44+
3945
await browser.waitForCondition('window.didRender')
4046

4147
const data = await browser

test/integration/config/next.config.js

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const withSass = require('@zeit/next-sass')
33
const path = require('path')
44
module.exports = withCSS(
55
withSass({
6+
// @zeit/next-sass is not supported with webpack 5
7+
webpack5: false,
68
onDemandEntries: {
79
// Make sure entries are not getting disposed.
810
maxInactiveAge: 1000 * 60 * 60,

test/integration/css-fixtures/custom-configuration-legacy/next.config.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const withCSS = require('@zeit/next-css')
22

33
module.exports = withCSS({
4+
// @zeit/next-css is not supported with webpack 5
5+
webpack5: false,
46
onDemandEntries: {
57
// Make sure entries are not getting disposed.
68
maxInactiveAge: 1000 * 60 * 60,

test/integration/externals/next.config.js

-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,4 @@ module.exports = {
77

88
return config
99
},
10-
future: {
11-
webpack5: true,
12-
},
1310
}

test/integration/fallback-modules/test/index.test.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ describe('Build Output', () => {
4848
expect(parseFloat(indexSize)).toBeGreaterThanOrEqual(2)
4949
expect(indexSize.endsWith('kB')).toBe(true)
5050

51-
expect(parseFloat(indexFirstLoad)).toBeLessThanOrEqual(67.9)
51+
expect(parseFloat(indexFirstLoad)).toBeLessThanOrEqual(
52+
process.env.NEXT_PRIVATE_TEST_WEBPACK4_MODE ? 68 : 67.9
53+
)
5254
expect(parseFloat(indexFirstLoad)).toBeGreaterThanOrEqual(60)
5355
expect(indexFirstLoad.endsWith('kB')).toBe(true)
5456
})
+4-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
const withSass = require('@zeit/next-sass')
2-
module.exports = withSass()
2+
module.exports = withSass({
3+
// @zeit/next-sass is not supported with webpack 5
4+
webpack5: false,
5+
})

0 commit comments

Comments
 (0)