Unverified Commit 437c35f1 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

feat: automatically enable webpack 5 (#22323)

Co-authored-by: 's avatarTim Neutkens <timneutkens@me.com>
Co-authored-by: 's avatarTim Neutkens <tim@timneutkens.nl>
parent 5c7475a6
...@@ -349,10 +349,10 @@ export default async function getBaseWebpackConfig( ...@@ -349,10 +349,10 @@ export default async function getBaseWebpackConfig(
} }
const clientResolveRewrites = require.resolve( const clientResolveRewrites = require.resolve(
'next/dist/next-server/lib/router/utils/resolve-rewrites' '../next-server/lib/router/utils/resolve-rewrites'
) )
const clientResolveRewritesNoop = require.resolve( const clientResolveRewritesNoop = require.resolve(
'next/dist/next-server/lib/router/utils/resolve-rewrites-noop' '../next-server/lib/router/utils/resolve-rewrites-noop'
) )
const resolveConfig = { const resolveConfig = {
...@@ -386,6 +386,9 @@ export default async function getBaseWebpackConfig( ...@@ -386,6 +386,9 @@ export default async function getBaseWebpackConfig(
...getReactProfilingInProduction(), ...getReactProfilingInProduction(),
[clientResolveRewrites]: hasRewrites [clientResolveRewrites]: hasRewrites
? clientResolveRewrites ? clientResolveRewrites
: // With webpack 5 an alias can be pointed to false to noop
isWebpack5
? false
: clientResolveRewritesNoop, : clientResolveRewritesNoop,
}, },
...(isWebpack5 && !isServer ...(isWebpack5 && !isServer
......
...@@ -21,10 +21,22 @@ export function install(useWebpack5: boolean) { ...@@ -21,10 +21,22 @@ export function install(useWebpack5: boolean) {
require('../../build/webpack/require-hook') require('../../build/webpack/require-hook')
} }
type CheckReasons =
| 'test-mode'
| 'no-config'
| 'future-flag'
| 'no-webpack-config'
| 'webpack-config'
type CheckResult = {
enabled: boolean
reason: CheckReasons
}
export async function shouldLoadWithWebpack5( export async function shouldLoadWithWebpack5(
phase: string, phase: string,
dir: string dir: string
): Promise<boolean> { ): Promise<CheckResult> {
await loadEnvConfig(dir, phase === PHASE_DEVELOPMENT_SERVER, Log) await loadEnvConfig(dir, phase === PHASE_DEVELOPMENT_SERVER, Log)
const path = await findUp(CONFIG_FILE, { const path = await findUp(CONFIG_FILE, {
...@@ -32,12 +44,19 @@ export async function shouldLoadWithWebpack5( ...@@ -32,12 +44,19 @@ export async function shouldLoadWithWebpack5(
}) })
if (Number(process.env.NEXT_PRIVATE_TEST_WEBPACK5_MODE) > 0) { if (Number(process.env.NEXT_PRIVATE_TEST_WEBPACK5_MODE) > 0) {
return true return {
enabled: true,
reason: 'test-mode',
}
} }
// No `next.config.js`: // No `next.config.js`:
if (!path?.length) { if (!path?.length) {
return false // TODO: return true to default to webpack 5 // Use webpack 5 by default in new apps:
return {
enabled: true,
reason: 'no-config',
}
} }
// Default to webpack 4 for backwards compatibility on boot: // Default to webpack 4 for backwards compatibility on boot:
...@@ -49,15 +68,57 @@ export async function shouldLoadWithWebpack5( ...@@ -49,15 +68,57 @@ export async function shouldLoadWithWebpack5(
userConfigModule.default || userConfigModule userConfigModule.default || userConfigModule
) )
// TODO: enable commented branch to enable webpack 5 // Opted-in manually
return userConfig.future?.webpack5 === true /* || !userConfig.webpack */ if (userConfig.future?.webpack5 === true) {
return {
enabled: true,
reason: 'future-flag',
}
}
// The user isn't configuring webpack
if (!userConfig.webpack) {
return {
enabled: true,
reason: 'no-webpack-config',
}
}
return {
enabled: false,
reason: 'webpack-config',
}
}
function reasonMessage(reason: CheckReasons) {
switch (reason) {
case 'future-flag':
return 'future.webpack5 option enabled'
case 'no-config':
return 'no next.config.js'
case 'no-webpack-config':
return 'no custom webpack configuration in next.config.js'
case 'test-mode':
return 'internal test mode'
default:
return ''
}
} }
export async function loadWebpackHook(phase: string, dir: string) { export async function loadWebpackHook(phase: string, dir: string) {
let useWebpack5 = false let useWebpack5 = false
const worker: any = new Worker(__filename, { enableWorkerThreads: false }) const worker: any = new Worker(__filename, { enableWorkerThreads: false })
try { try {
useWebpack5 = Boolean(await worker.shouldLoadWithWebpack5(phase, dir)) const result: CheckResult = await worker.shouldLoadWithWebpack5(phase, dir)
if (result.enabled) {
Log.info(`Using webpack 5. Reason: ${reasonMessage(result.reason)}`)
} else {
Log.info(
'Using webpack 4. Reason: the next.config.js has custom webpack configuration',
reasonMessage(result.reason)
)
}
useWebpack5 = Boolean(result.enabled)
} catch { } catch {
// If this errors, it likely will do so again upon boot, so we just swallow // If this errors, it likely will do so again upon boot, so we just swallow
// it here. // it here.
......
...@@ -82,6 +82,7 @@ ...@@ -82,6 +82,7 @@
"crypto-browserify": "3.12.0", "crypto-browserify": "3.12.0",
"cssnano-simple": "1.2.2", "cssnano-simple": "1.2.2",
"domain-browser": "4.19.0", "domain-browser": "4.19.0",
"encoding": "0.1.13",
"eslint": "7.9.0", "eslint": "7.9.0",
"etag": "1.8.1", "etag": "1.8.1",
"find-cache-dir": "3.3.1", "find-cache-dir": "3.3.1",
......
...@@ -90,21 +90,21 @@ describe('Build Output', () => { ...@@ -90,21 +90,21 @@ describe('Build Output', () => {
expect(parseFloat(size)).toBeGreaterThan(0) expect(parseFloat(size)).toBeGreaterThan(0)
} }
// should be no bigger than 265 bytes // should be no bigger than 291 bytes
expect(parseFloat(indexSize) - 266).toBeLessThanOrEqual(0) expect(parseFloat(indexSize) - 291).toBeLessThanOrEqual(0)
expect(indexSize.endsWith('B')).toBe(true) expect(indexSize.endsWith('B')).toBe(true)
// should be no bigger than 64.6 kb // should be no bigger than 64.8 kb
expect(parseFloat(indexFirstLoad)).toBeCloseTo(64.6, 1) expect(parseFloat(indexFirstLoad)).toBeCloseTo(65.4, 1)
expect(indexFirstLoad.endsWith('kB')).toBe(true) expect(indexFirstLoad.endsWith('kB')).toBe(true)
expect(parseFloat(err404Size) - 3.7).toBeLessThanOrEqual(0) expect(parseFloat(err404Size) - 3.7).toBeLessThanOrEqual(0)
expect(err404Size.endsWith('kB')).toBe(true) expect(err404Size.endsWith('kB')).toBe(true)
expect(parseFloat(err404FirstLoad)).toBeCloseTo(67.8, 0) expect(parseFloat(err404FirstLoad)).toBeCloseTo(68.5, 0)
expect(err404FirstLoad.endsWith('kB')).toBe(true) expect(err404FirstLoad.endsWith('kB')).toBe(true)
expect(parseFloat(sharedByAll)).toBeCloseTo(64.4, 1) expect(parseFloat(sharedByAll)).toBeCloseTo(65.1, 1)
expect(sharedByAll.endsWith('kB')).toBe(true) expect(sharedByAll.endsWith('kB')).toBe(true)
if (_appSize.endsWith('kB')) { if (_appSize.endsWith('kB')) {
...@@ -115,7 +115,7 @@ describe('Build Output', () => { ...@@ -115,7 +115,7 @@ describe('Build Output', () => {
expect(_appSize.endsWith(' B')).toBe(true) expect(_appSize.endsWith(' B')).toBe(true)
} }
expect(parseFloat(webpackSize) - 753).toBeLessThanOrEqual(0) expect(parseFloat(webpackSize) - 950).toBeLessThanOrEqual(0)
expect(webpackSize.endsWith(' B')).toBe(true) expect(webpackSize.endsWith(' B')).toBe(true)
expect(parseFloat(mainSize) - 7.3).toBeLessThanOrEqual(0) expect(parseFloat(mainSize) - 7.3).toBeLessThanOrEqual(0)
......
...@@ -8,6 +8,8 @@ import { ...@@ -8,6 +8,8 @@ import {
killApp, killApp,
nextBuild, nextBuild,
nextStart, nextStart,
check,
waitFor,
} from 'next-test-utils' } from 'next-test-utils'
jest.setTimeout(1000 * 60 * 2) jest.setTimeout(1000 * 60 * 2)
...@@ -24,22 +26,28 @@ const runTests = () => { ...@@ -24,22 +26,28 @@ const runTests = () => {
expect(props.params).toEqual({ slug: 'first' }) expect(props.params).toEqual({ slug: 'first' })
await browser.elementByCss('#add-query-shallow').click() await browser.elementByCss('#add-query-shallow').click()
await waitFor(1000)
const props2 = JSON.parse(await browser.elementByCss('#props').text()) const props2 = JSON.parse(await browser.elementByCss('#props').text())
expect(props2).toEqual(props) expect(props2).toEqual(props)
await browser.elementByCss('#remove-query-shallow').click() await browser.elementByCss('#remove-query-shallow').click()
await waitFor(1000)
const props3 = JSON.parse(await browser.elementByCss('#props').text()) const props3 = JSON.parse(await browser.elementByCss('#props').text())
expect(props3).toEqual(props) expect(props3).toEqual(props)
await browser.elementByCss('#to-another').click() await browser.elementByCss('#to-another').click()
await waitFor(1000)
await check(() => browser.elementByCss('#props').text(), /another/)
const props4 = JSON.parse(await browser.elementByCss('#props').text()) const props4 = JSON.parse(await browser.elementByCss('#props').text())
expect(props4.params).toEqual({ slug: 'another' }) expect(props4.params).toEqual({ slug: 'another' })
expect(props4.random).not.toBe(props.random) expect(props4.random).not.toBe(props.random)
await browser.back() await browser.back()
await waitFor(1000)
const props5 = JSON.parse(await browser.elementByCss('#props').text()) const props5 = JSON.parse(await browser.elementByCss('#props').text())
expect(props5.params).toEqual({ slug: 'first' }) expect(props5.params).toEqual({ slug: 'first' })
...@@ -53,23 +61,27 @@ const runTests = () => { ...@@ -53,23 +61,27 @@ const runTests = () => {
expect(props.params).toEqual({ slug: 'first' }) expect(props.params).toEqual({ slug: 'first' })
await browser.elementByCss('#add-query-shallow').click() await browser.elementByCss('#add-query-shallow').click()
await waitFor(1000)
const props2 = JSON.parse(await browser.elementByCss('#props').text()) const props2 = JSON.parse(await browser.elementByCss('#props').text())
expect(props2).toEqual(props) expect(props2).toEqual(props)
await browser.elementByCss('#to-another').click() await browser.elementByCss('#to-another').click()
await waitFor(1000)
const props3 = JSON.parse(await browser.elementByCss('#props').text()) const props3 = JSON.parse(await browser.elementByCss('#props').text())
expect(props3.params).toEqual({ slug: 'another' }) expect(props3.params).toEqual({ slug: 'another' })
expect(props3.random).not.toBe(props2.random) expect(props3.random).not.toBe(props2.random)
await browser.back() await browser.back()
await waitFor(1000)
const props4 = JSON.parse(await browser.elementByCss('#props').text()) const props4 = JSON.parse(await browser.elementByCss('#props').text())
expect(props4.params).toEqual({ slug: 'first' }) expect(props4.params).toEqual({ slug: 'first' })
expect(props4.random).not.toBe(props3.random) expect(props4.random).not.toBe(props3.random)
await browser.forward() await browser.forward()
await waitFor(1000)
const props5 = JSON.parse(await browser.elementByCss('#props').text()) const props5 = JSON.parse(await browser.elementByCss('#props').text())
expect(props5.params).toEqual({ slug: 'another' }) expect(props5.params).toEqual({ slug: 'another' })
......
...@@ -20,7 +20,7 @@ describe('externalize next/dist/next-server', () => { ...@@ -20,7 +20,7 @@ describe('externalize next/dist/next-server', () => {
escapeStringRegexp( escapeStringRegexp(
`module.exports = require("next/dist/next-server/lib/head.js");` `module.exports = require("next/dist/next-server/lib/head.js");`
) + ) +
'$', ';?$',
'm' 'm'
) )
) )
......
...@@ -15,7 +15,7 @@ import { ...@@ -15,7 +15,7 @@ import {
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import { join } from 'path' import { join } from 'path'
jest.setTimeout(1000 * 30) jest.setTimeout(1000 * 60)
const appDir = join(__dirname, '../') const appDir = join(__dirname, '../')
const nextConfig = join(appDir, 'next.config.js') const nextConfig = join(appDir, 'next.config.js')
......
...@@ -17,7 +17,7 @@ import { ...@@ -17,7 +17,7 @@ import {
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import { join } from 'path' import { join } from 'path'
jest.setTimeout(1000 * 30) jest.setTimeout(1000 * 60)
const appDir = join(__dirname, '../') const appDir = join(__dirname, '../')
const nextConfig = join(appDir, 'next.config.js') const nextConfig = join(appDir, 'next.config.js')
......
...@@ -44,7 +44,7 @@ describe('TypeScript Image Component', () => { ...@@ -44,7 +44,7 @@ describe('TypeScript Image Component', () => {
it('should render the valid Image usage and not print error', async () => { it('should render the valid Image usage and not print error', async () => {
const html = await renderViaHTTP(appPort, '/valid', {}) const html = await renderViaHTTP(appPort, '/valid', {})
expect(html).toMatch(/This is valid usage of the Image component/) expect(html).toMatch(/This is valid usage of the Image component/)
expect(output).not.toMatch(/Error/) expect(output).not.toMatch(/Error: Image/)
}) })
it('should print error when invalid Image usage', async () => { it('should print error when invalid Image usage', async () => {
......
...@@ -10,7 +10,7 @@ import { ...@@ -10,7 +10,7 @@ import {
} from 'next-test-utils' } from 'next-test-utils'
import fs from 'fs-extra' import fs from 'fs-extra'
jest.setTimeout(1000 * 30) jest.setTimeout(1000 * 60)
const appDir = join(__dirname, '../') const appDir = join(__dirname, '../')
const nextConfig = join(appDir, 'next.config.js') const nextConfig = join(appDir, 'next.config.js')
......
...@@ -6,7 +6,7 @@ const $comment = ({ gipQuery }) => { ...@@ -6,7 +6,7 @@ const $comment = ({ gipQuery }) => {
return ( return (
<> <>
<p> <p id={query.comment}>
I am {query.comment} on {query.name} I am {query.comment} on {query.name}
</p> </p>
<span>gip {gipQuery && gipQuery.name}</span> <span>gip {gipQuery && gipQuery.name}</span>
......
...@@ -3,7 +3,7 @@ import { useRouter } from 'next/router' ...@@ -3,7 +3,7 @@ import { useRouter } from 'next/router'
const Page = () => { const Page = () => {
const router = useRouter() const router = useRouter()
const { query } = router const { query } = router
return <p>This is {query.name}</p> return <p id={query.name}>This is {query.name}</p>
} }
Page.getInitialProps = () => ({}) Page.getInitialProps = () => ({})
......
...@@ -49,9 +49,9 @@ function runTests() { ...@@ -49,9 +49,9 @@ function runTests() {
try { try {
browser = await webdriver(appPort, '/') browser = await webdriver(appPort, '/')
await browser.elementByCss('#view-post-1').click() await browser.elementByCss('#view-post-1').click()
await browser.waitForElementByCss('p') await browser.waitForElementByCss('p#post-1')
const text = await browser.elementByCss('p').text() const text = await browser.elementByCss('p#post-1').text()
expect(text).toMatch(/this is.*?post-1/i) expect(text).toMatch(/this is.*?post-1/i)
} finally { } finally {
if (browser) await browser.close() if (browser) await browser.close()
...@@ -63,9 +63,9 @@ function runTests() { ...@@ -63,9 +63,9 @@ function runTests() {
try { try {
browser = await webdriver(appPort, '/') browser = await webdriver(appPort, '/')
await browser.elementByCss('#view-post-1-comment-1').click() await browser.elementByCss('#view-post-1-comment-1').click()
await browser.waitForElementByCss('p') await browser.waitForElementByCss('p#comment-1')
const text = await browser.elementByCss('p').text() const text = await browser.elementByCss('p#comment-1').text()
expect(text).toMatch(/i am.*comment-1.*on.*post-1/i) expect(text).toMatch(/i am.*comment-1.*on.*post-1/i)
} finally { } finally {
if (browser) await browser.close() if (browser) await browser.close()
......
...@@ -383,6 +383,11 @@ describe('Telemetry CLI', () => { ...@@ -383,6 +383,11 @@ describe('Telemetry CLI', () => {
expect(event1).toMatch(/hasReportWebVitals.*?false/) expect(event1).toMatch(/hasReportWebVitals.*?false/)
// Case 2: When _app.js exist with reportWebVitals function. // Case 2: When _app.js exist with reportWebVitals function.
await fs.utimes(
path.join(appDir, 'pages', '_app_withreportwebvitals.empty'),
new Date(),
new Date()
)
await fs.rename( await fs.rename(
path.join(appDir, 'pages', '_app_withreportwebvitals.empty'), path.join(appDir, 'pages', '_app_withreportwebvitals.empty'),
path.join(appDir, 'pages', '_app.js') path.join(appDir, 'pages', '_app.js')
...@@ -402,6 +407,11 @@ describe('Telemetry CLI', () => { ...@@ -402,6 +407,11 @@ describe('Telemetry CLI', () => {
expect(event1).toMatch(/hasReportWebVitals.*?true/) expect(event1).toMatch(/hasReportWebVitals.*?true/)
// Case 3: When _app.js exist without reportWebVitals function. // Case 3: When _app.js exist without reportWebVitals function.
await fs.utimes(
path.join(appDir, 'pages', '_app_withoutreportwebvitals.empty'),
new Date(),
new Date()
)
await fs.rename( await fs.rename(
path.join(appDir, 'pages', '_app_withoutreportwebvitals.empty'), path.join(appDir, 'pages', '_app_withoutreportwebvitals.empty'),
path.join(appDir, 'pages', '_app.js') path.join(appDir, 'pages', '_app.js')
......
...@@ -5324,7 +5324,7 @@ configstore@3.1.2: ...@@ -5324,7 +5324,7 @@ configstore@3.1.2:
write-file-atomic "^2.0.0" write-file-atomic "^2.0.0"
xdg-basedir "^3.0.0" xdg-basedir "^3.0.0"
console-browserify@1.2.0, console-browserify@^1.1.0: console-browserify@^1.1.0:
version "1.2.0" version "1.2.0"
resolved "https://registry.yarnpkg.com/console-browserify/-/console-browserify-1.2.0.tgz#67063cef57ceb6cf4993a2ab3a55840ae8c49336" resolved "https://registry.yarnpkg.com/console-browserify/-/console-browserify-1.2.0.tgz#67063cef57ceb6cf4993a2ab3a55840ae8c49336"
integrity sha512-ZMkYO/LkF17QvCPqM0gxw8yUzigAOZOSWSHg91FH6orS7vcEj5dVZTidN2fQ14yBSdg97RqhSNwLUXInd52OTA== integrity sha512-ZMkYO/LkF17QvCPqM0gxw8yUzigAOZOSWSHg91FH6orS7vcEj5dVZTidN2fQ14yBSdg97RqhSNwLUXInd52OTA==
...@@ -6493,19 +6493,19 @@ encodeurl@~1.0.2: ...@@ -6493,19 +6493,19 @@ encodeurl@~1.0.2:
version "1.0.2" version "1.0.2"
resolved "https://registry.yarnpkg.com/encodeurl/-/encodeurl-1.0.2.tgz#ad3ff4c86ec2d029322f5a02c3a9a606c95b3f59" resolved "https://registry.yarnpkg.com/encodeurl/-/encodeurl-1.0.2.tgz#ad3ff4c86ec2d029322f5a02c3a9a606c95b3f59"
encoding@^0.1.11: encoding@0.1.13, encoding@^0.1.12:
version "0.1.12"
resolved "https://registry.yarnpkg.com/encoding/-/encoding-0.1.12.tgz#538b66f3ee62cd1ab51ec323829d1f9480c74beb"
dependencies:
iconv-lite "~0.4.13"
encoding@^0.1.12:
version "0.1.13" version "0.1.13"
resolved "https://registry.yarnpkg.com/encoding/-/encoding-0.1.13.tgz#56574afdd791f54a8e9b2785c0582a2d26210fa9" resolved "https://registry.yarnpkg.com/encoding/-/encoding-0.1.13.tgz#56574afdd791f54a8e9b2785c0582a2d26210fa9"
integrity sha512-ETBauow1T35Y/WZMkio9jiM0Z5xjHHmJ4XmjZOq1l/dXz3lr2sRn87nJy20RupqSh1F2m3HHPSp8ShIPQJrJ3A== integrity sha512-ETBauow1T35Y/WZMkio9jiM0Z5xjHHmJ4XmjZOq1l/dXz3lr2sRn87nJy20RupqSh1F2m3HHPSp8ShIPQJrJ3A==
dependencies: dependencies:
iconv-lite "^0.6.2" iconv-lite "^0.6.2"
encoding@^0.1.11:
version "0.1.12"
resolved "https://registry.yarnpkg.com/encoding/-/encoding-0.1.12.tgz#538b66f3ee62cd1ab51ec323829d1f9480c74beb"
dependencies:
iconv-lite "~0.4.13"
end-of-stream@^1.0.0, end-of-stream@^1.1.0, end-of-stream@^1.4.1: end-of-stream@^1.0.0, end-of-stream@^1.1.0, end-of-stream@^1.4.1:
version "1.4.4" version "1.4.4"
resolved "https://registry.yarnpkg.com/end-of-stream/-/end-of-stream-1.4.4.tgz#5ae64a5f45057baf3626ec14da0ca5e4b2431eb0" resolved "https://registry.yarnpkg.com/end-of-stream/-/end-of-stream-1.4.4.tgz#5ae64a5f45057baf3626ec14da0ca5e4b2431eb0"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment