Unverified Commit f921b4f4 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Auto enable React's new JSX transform on 17.x (#16603)

parent 6f60a223
...@@ -96,8 +96,8 @@ jobs: ...@@ -96,8 +96,8 @@ jobs:
steps: steps:
- run: exit 0 - run: exit 0
testWebpack5: testFutureDependencies:
name: webpack 5 (Basic, Production, Acceptance) name: React 17 + webpack 5 (Basic, Production, Acceptance)
runs-on: ubuntu-latest runs-on: ubuntu-latest
env: env:
NEXT_TELEMETRY_DISABLED: 1 NEXT_TELEMETRY_DISABLED: 1
...@@ -108,6 +108,8 @@ jobs: ...@@ -108,6 +108,8 @@ jobs:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- run: git fetch --depth=1 origin +refs/tags/*:refs/tags/* - run: git fetch --depth=1 origin +refs/tags/*:refs/tags/*
- run: cat package.json | jq '.resolutions.webpack = "^5.0.0-beta.22"' > package.json.tmp && mv package.json.tmp package.json - run: cat package.json | jq '.resolutions.webpack = "^5.0.0-beta.22"' > package.json.tmp && mv package.json.tmp package.json
- run: cat package.json | jq '.resolutions.react = "^17.0.0-rc.1"' > package.json.tmp && mv package.json.tmp package.json
- run: cat package.json | jq '.resolutions."react-dom" = "^17.0.0-rc.1"' > package.json.tmp && mv package.json.tmp package.json
- run: yarn install --check-files - run: yarn install --check-files
- run: node run-tests.js test/integration/production/test/index.test.js - run: node run-tests.js test/integration/production/test/index.test.js
- run: node run-tests.js test/integration/basic/test/index.test.js - run: node run-tests.js test/integration/basic/test/index.test.js
......
...@@ -37,9 +37,9 @@ ...@@ -37,9 +37,9 @@
}, },
"pre-commit": "lint-staged", "pre-commit": "lint-staged",
"devDependencies": { "devDependencies": {
"@babel/plugin-proposal-object-rest-spread": "7.9.6", "@babel/plugin-proposal-object-rest-spread": "7.11.0",
"@babel/preset-flow": "7.9.0", "@babel/preset-flow": "7.10.4",
"@babel/preset-react": "7.9.4", "@babel/preset-react": "7.10.4",
"@fullhuman/postcss-purgecss": "1.3.0", "@fullhuman/postcss-purgecss": "1.3.0",
"@mdx-js/loader": "0.18.0", "@mdx-js/loader": "0.18.0",
"@types/cheerio": "0.22.16", "@types/cheerio": "0.22.16",
...@@ -53,7 +53,7 @@ ...@@ -53,7 +53,7 @@
"@zeit/next-sass": "1.0.2-canary.2", "@zeit/next-sass": "1.0.2-canary.2",
"@zeit/next-typescript": "1.1.2-canary.0", "@zeit/next-typescript": "1.1.2-canary.0",
"abort-controller": "3.0.0", "abort-controller": "3.0.0",
"amphtml-validator": "1.0.30", "amphtml-validator": "1.0.33",
"async-sema": "3.0.1", "async-sema": "3.0.1",
"babel-core": "7.0.0-bridge.0", "babel-core": "7.0.0-bridge.0",
"babel-eslint": "10.0.3", "babel-eslint": "10.0.3",
......
...@@ -65,6 +65,9 @@ module.exports = ( ...@@ -65,6 +65,9 @@ module.exports = (
const supportsESM = api.caller(supportsStaticESM) const supportsESM = api.caller(supportsStaticESM)
const isServer = api.caller((caller: any) => !!caller && caller.isServer) const isServer = api.caller((caller: any) => !!caller && caller.isServer)
const isModern = api.caller((caller: any) => !!caller && caller.isModern) const isModern = api.caller((caller: any) => !!caller && caller.isModern)
const hasJsxRuntime = Boolean(
api.caller((caller: any) => !!caller && caller.hasJsxRuntime)
)
const isLaxModern = const isLaxModern =
isModern || isModern ||
...@@ -113,7 +116,7 @@ module.exports = ( ...@@ -113,7 +116,7 @@ module.exports = (
// This adds @babel/plugin-transform-react-jsx-source and // This adds @babel/plugin-transform-react-jsx-source and
// @babel/plugin-transform-react-jsx-self automatically in development // @babel/plugin-transform-react-jsx-self automatically in development
development: isDevelopment || isTest, development: isDevelopment || isTest,
pragma: '__jsx', ...(hasJsxRuntime ? { runtime: 'automatic' } : { pragma: '__jsx' }),
...options['preset-react'], ...options['preset-react'],
}, },
], ],
...@@ -123,7 +126,7 @@ module.exports = ( ...@@ -123,7 +126,7 @@ module.exports = (
], ],
], ],
plugins: [ plugins: [
[ !hasJsxRuntime && [
require('./plugins/jsx-pragma'), require('./plugins/jsx-pragma'),
{ {
// This produces the following injected import for modules containing JSX: // This produces the following injected import for modules containing JSX:
...@@ -181,14 +184,10 @@ module.exports = ( ...@@ -181,14 +184,10 @@ module.exports = (
require('@babel/plugin-proposal-optional-chaining'), require('@babel/plugin-proposal-optional-chaining'),
require('@babel/plugin-proposal-nullish-coalescing-operator'), require('@babel/plugin-proposal-nullish-coalescing-operator'),
isServer && require('@babel/plugin-syntax-bigint'), isServer && require('@babel/plugin-syntax-bigint'),
[require('@babel/plugin-proposal-numeric-separator').default, false], // Always compile numeric separator because the resulting number is
// smaller.
require('@babel/plugin-proposal-numeric-separator'),
require('@babel/plugin-proposal-export-namespace-from'), require('@babel/plugin-proposal-export-namespace-from'),
].filter(Boolean), ].filter(Boolean),
overrides: [
{
test: /\.tsx?$/,
plugins: [require('@babel/plugin-proposal-numeric-separator').default],
},
],
} }
} }
...@@ -3,6 +3,7 @@ import ReactRefreshWebpackPlugin from '@next/react-refresh-utils/ReactRefreshWeb ...@@ -3,6 +3,7 @@ import ReactRefreshWebpackPlugin from '@next/react-refresh-utils/ReactRefreshWeb
import crypto from 'crypto' import crypto from 'crypto'
import { readFileSync } from 'fs' import { readFileSync } from 'fs'
import chalk from 'next/dist/compiled/chalk' import chalk from 'next/dist/compiled/chalk'
import semver from 'next/dist/compiled/semver'
import TerserPlugin from 'next/dist/compiled/terser-webpack-plugin' import TerserPlugin from 'next/dist/compiled/terser-webpack-plugin'
import path from 'path' import path from 'path'
import webpack from 'webpack' import webpack from 'webpack'
...@@ -14,6 +15,8 @@ import { ...@@ -14,6 +15,8 @@ import {
PAGES_DIR_ALIAS, PAGES_DIR_ALIAS,
} from '../lib/constants' } from '../lib/constants'
import { fileExists } from '../lib/file-exists' import { fileExists } from '../lib/file-exists'
import { getPackageVersion } from '../lib/get-package-version'
import { Rewrite } from '../lib/load-custom-routes'
import { resolveRequest } from '../lib/resolve-request' import { resolveRequest } from '../lib/resolve-request'
import { getTypeScriptConfiguration } from '../lib/typescript/getTypeScriptConfiguration' import { getTypeScriptConfiguration } from '../lib/typescript/getTypeScriptConfiguration'
import { import {
...@@ -54,7 +57,7 @@ import WebpackConformancePlugin, { ...@@ -54,7 +57,7 @@ import WebpackConformancePlugin, {
ReactSyncScriptsConformanceCheck, ReactSyncScriptsConformanceCheck,
} from './webpack/plugins/webpack-conformance-plugin' } from './webpack/plugins/webpack-conformance-plugin'
import { WellKnownErrorsPlugin } from './webpack/plugins/wellknown-errors-plugin' import { WellKnownErrorsPlugin } from './webpack/plugins/wellknown-errors-plugin'
import { Rewrite } from '../lib/load-custom-routes'
type ExcludesFalse = <T>(x: T | false) => x is T type ExcludesFalse = <T>(x: T | false) => x is T
const isWebpack5 = parseInt(webpack.version!) === 5 const isWebpack5 = parseInt(webpack.version!) === 5
...@@ -226,7 +229,13 @@ export default async function getBaseWebpackConfig( ...@@ -226,7 +229,13 @@ export default async function getBaseWebpackConfig(
} }
} }
const hasReactRefresh = dev && !isServer const reactVersion = await getPackageVersion({ cwd: dir, name: 'react' })
const hasReactRefresh: boolean = dev && !isServer
const hasJsxRuntime: boolean =
Boolean(reactVersion) &&
// 17.0.0-rc.0 had a breaking change not compatible with Next.js, but was
// fixed in rc.1.
semver.gte(reactVersion!, '17.0.0-rc.1')
const distDir = path.join(dir, config.distDir) const distDir = path.join(dir, config.distDir)
const defaultLoaders = { const defaultLoaders = {
...@@ -243,6 +252,7 @@ export default async function getBaseWebpackConfig( ...@@ -243,6 +252,7 @@ export default async function getBaseWebpackConfig(
hasModern: !!config.experimental.modern, hasModern: !!config.experimental.modern,
development: dev, development: dev,
hasReactRefresh, hasReactRefresh,
hasJsxRuntime,
}, },
}, },
// Backwards compat // Backwards compat
......
...@@ -3,9 +3,9 @@ import hash from 'next/dist/compiled/string-hash' ...@@ -3,9 +3,9 @@ import hash from 'next/dist/compiled/string-hash'
import { basename, join } from 'path' import { basename, join } from 'path'
import * as Log from '../../output/log' import * as Log from '../../output/log'
// increment 'm' to invalidate cache // increment 'n' to invalidate cache
// eslint-disable-next-line no-useless-concat // eslint-disable-next-line no-useless-concat
const cacheKey = 'babel-cache-' + 'm' + '-' const cacheKey = 'babel-cache-' + 'n' + '-'
const nextBabelPreset = require('../../babel/preset') const nextBabelPreset = require('../../babel/preset')
const getModernOptions = (babelOptions = {}) => { const getModernOptions = (babelOptions = {}) => {
...@@ -61,6 +61,7 @@ module.exports = babelLoader.custom((babel) => { ...@@ -61,6 +61,7 @@ module.exports = babelLoader.custom((babel) => {
babelPresetPlugins: opts.babelPresetPlugins, babelPresetPlugins: opts.babelPresetPlugins,
development: opts.development, development: opts.development,
hasReactRefresh: opts.hasReactRefresh, hasReactRefresh: opts.hasReactRefresh,
hasJsxRuntime: opts.hasJsxRuntime,
} }
const filename = join(opts.cwd, 'noop.js') const filename = join(opts.cwd, 'noop.js')
const loader = Object.assign( const loader = Object.assign(
...@@ -76,6 +77,7 @@ module.exports = babelLoader.custom((babel) => { ...@@ -76,6 +77,7 @@ module.exports = babelLoader.custom((babel) => {
'-new-polyfills' + '-new-polyfills' +
(opts.development ? '-development' : '-production') + (opts.development ? '-development' : '-production') +
(opts.hasReactRefresh ? '-react-refresh' : '') + (opts.hasReactRefresh ? '-react-refresh' : '') +
(opts.hasJsxRuntime ? '-jsx-runtime' : '') +
JSON.stringify( JSON.stringify(
babel.loadPartialConfig({ babel.loadPartialConfig({
filename, filename,
...@@ -99,6 +101,7 @@ module.exports = babelLoader.custom((babel) => { ...@@ -99,6 +101,7 @@ module.exports = babelLoader.custom((babel) => {
delete loader.babelPresetPlugins delete loader.babelPresetPlugins
delete loader.development delete loader.development
delete loader.hasReactRefresh delete loader.hasReactRefresh
delete loader.hasJsxRuntime
return { loader, custom } return { loader, custom }
}, },
config( config(
...@@ -113,6 +116,7 @@ module.exports = babelLoader.custom((babel) => { ...@@ -113,6 +116,7 @@ module.exports = babelLoader.custom((babel) => {
babelPresetPlugins, babelPresetPlugins,
development, development,
hasReactRefresh, hasReactRefresh,
hasJsxRuntime,
}, },
} }
) { ) {
...@@ -136,6 +140,7 @@ module.exports = babelLoader.custom((babel) => { ...@@ -136,6 +140,7 @@ module.exports = babelLoader.custom((babel) => {
options.caller.isServer = isServer options.caller.isServer = isServer
options.caller.isModern = isModern options.caller.isModern = isModern
options.caller.isDev = development options.caller.isDev = development
options.caller.hasJsxRuntime = hasJsxRuntime
const emitWarning = this.emitWarning.bind(this) const emitWarning = this.emitWarning.bind(this)
Object.defineProperty(options.caller, 'onWarning', { Object.defineProperty(options.caller, 'onWarning', {
......
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -59,24 +59,24 @@ ...@@ -59,24 +59,24 @@
}, },
"dependencies": { "dependencies": {
"@ampproject/toolbox-optimizer": "2.6.0", "@ampproject/toolbox-optimizer": "2.6.0",
"@babel/code-frame": "7.8.3", "@babel/code-frame": "7.10.4",
"@babel/core": "7.7.7", "@babel/core": "7.7.7",
"@babel/plugin-proposal-class-properties": "7.8.3", "@babel/plugin-proposal-class-properties": "7.10.4",
"@babel/plugin-proposal-export-namespace-from": "7.10.4", "@babel/plugin-proposal-export-namespace-from": "7.10.4",
"@babel/plugin-proposal-nullish-coalescing-operator": "7.8.3", "@babel/plugin-proposal-nullish-coalescing-operator": "7.10.4",
"@babel/plugin-proposal-numeric-separator": "7.8.3", "@babel/plugin-proposal-numeric-separator": "7.10.4",
"@babel/plugin-proposal-object-rest-spread": "7.9.6", "@babel/plugin-proposal-object-rest-spread": "7.11.0",
"@babel/plugin-proposal-optional-chaining": "7.9.0", "@babel/plugin-proposal-optional-chaining": "7.11.0",
"@babel/plugin-syntax-bigint": "7.8.3", "@babel/plugin-syntax-bigint": "7.8.3",
"@babel/plugin-syntax-dynamic-import": "7.8.3", "@babel/plugin-syntax-dynamic-import": "7.8.3",
"@babel/plugin-transform-modules-commonjs": "7.9.6", "@babel/plugin-transform-modules-commonjs": "7.10.4",
"@babel/plugin-transform-runtime": "7.9.6", "@babel/plugin-transform-runtime": "7.11.5",
"@babel/preset-env": "7.9.6", "@babel/preset-env": "7.11.5",
"@babel/preset-modules": "0.1.3", "@babel/preset-modules": "0.1.4",
"@babel/preset-react": "7.9.4", "@babel/preset-react": "7.10.4",
"@babel/preset-typescript": "7.9.0", "@babel/preset-typescript": "7.10.4",
"@babel/runtime": "7.9.6", "@babel/runtime": "7.11.2",
"@babel/types": "7.9.6", "@babel/types": "7.11.5",
"@next/react-dev-overlay": "9.5.3-canary.25", "@next/react-dev-overlay": "9.5.3-canary.25",
"@next/react-refresh-utils": "9.5.3-canary.25", "@next/react-refresh-utils": "9.5.3-canary.25",
"ast-types": "0.13.2", "ast-types": "0.13.2",
...@@ -160,7 +160,7 @@ ...@@ -160,7 +160,7 @@
"@types/text-table": "0.2.1", "@types/text-table": "0.2.1",
"@types/webpack-sources": "0.1.5", "@types/webpack-sources": "0.1.5",
"@zeit/ncc": "0.22.0", "@zeit/ncc": "0.22.0",
"amphtml-validator": "1.0.31", "amphtml-validator": "1.0.33",
"arg": "4.1.0", "arg": "4.1.0",
"async-retry": "1.2.3", "async-retry": "1.2.3",
"async-sema": "3.0.0", "async-sema": "3.0.0",
...@@ -169,7 +169,7 @@ ...@@ -169,7 +169,7 @@
"cache-loader": "4.1.0", "cache-loader": "4.1.0",
"chalk": "2.4.2", "chalk": "2.4.2",
"ci-info": "watson/ci-info#f43f6a1cefff47fb361c88cf4b943fdbcaafe540", "ci-info": "watson/ci-info#f43f6a1cefff47fb361c88cf4b943fdbcaafe540",
"comment-json": "3.0.2", "comment-json": "3.0.3",
"compression": "1.7.4", "compression": "1.7.4",
"conf": "5.0.0", "conf": "5.0.0",
"content-type": "1.0.4", "content-type": "1.0.4",
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
"build": "tsc -d -w -p tsconfig.json" "build": "tsc -d -w -p tsconfig.json"
}, },
"dependencies": { "dependencies": {
"@babel/code-frame": "7.8.3", "@babel/code-frame": "7.10.4",
"ally.js": "1.4.1", "ally.js": "1.4.1",
"anser": "1.4.9", "anser": "1.4.9",
"chalk": "4.0.0", "chalk": "4.0.0",
......
...@@ -238,6 +238,7 @@ test('render error not shown right after syntax error', async () => { ...@@ -238,6 +238,7 @@ test('render error not shown right after syntax error', async () => {
await session.patch( await session.patch(
'index.js', 'index.js',
` `
import * as React from 'react';
class ClassDefault extends React.Component { class ClassDefault extends React.Component {
render() { render() {
return <h1>Default Export</h1>; return <h1>Default Export</h1>;
...@@ -576,6 +577,7 @@ test('boundaries', async () => { ...@@ -576,6 +577,7 @@ test('boundaries', async () => {
'index.js', 'index.js',
` `
import FunctionDefault from './FunctionDefault.js' import FunctionDefault from './FunctionDefault.js'
import * as React from 'react'
class ErrorBoundary extends React.Component { class ErrorBoundary extends React.Component {
constructor() { constructor() {
super() super()
......
...@@ -38,6 +38,9 @@ export async function sandbox( ...@@ -38,6 +38,9 @@ export async function sandbox(
}) })
const browser = await webdriver(appPort, '/') const browser = await webdriver(appPort, '/')
// Slow down tests a bit to ensure application is ready:
await new Promise((resolve) => setTimeout(resolve, 750))
return [ return [
{ {
sandboxDirectory, sandboxDirectory,
......
...@@ -13,8 +13,8 @@ export default (context, render) => { ...@@ -13,8 +13,8 @@ export default (context, render) => {
it('should render dynamic import components', async () => { it('should render dynamic import components', async () => {
const $ = await get$('/dynamic/ssr') const $ = await get$('/dynamic/ssr')
// Make sure the client side knows it has to wait for the bundle // Make sure the client side knows it has to wait for the bundle
expect($('body').html()).toContain( expect(JSON.parse($('#__NEXT_DATA__').html()).dynamicIds).toContain(
'"dynamicIds":["./components/hello1.js"]' './components/hello1.js'
) )
expect($('body').text()).toMatch(/Hello World 1/) expect($('body').text()).toMatch(/Hello World 1/)
}) })
...@@ -22,8 +22,8 @@ export default (context, render) => { ...@@ -22,8 +22,8 @@ export default (context, render) => {
it('should render dynamic import components using a function as first parameter', async () => { it('should render dynamic import components using a function as first parameter', async () => {
const $ = await get$('/dynamic/function') const $ = await get$('/dynamic/function')
// Make sure the client side knows it has to wait for the bundle // Make sure the client side knows it has to wait for the bundle
expect($('body').html()).toContain( expect(JSON.parse($('#__NEXT_DATA__').html()).dynamicIds).toContain(
'"dynamicIds":["./components/hello1.js"]' './components/hello1.js'
) )
expect($('body').text()).toMatch(/Hello World 1/) expect($('body').text()).toMatch(/Hello World 1/)
}) })
......
...@@ -253,22 +253,9 @@ export default (context, renderViaHTTP) => { ...@@ -253,22 +253,9 @@ export default (context, renderViaHTTP) => {
'__WEBPACK_DEFAULT_EXPORT__', '__WEBPACK_DEFAULT_EXPORT__',
'Unknown' 'Unknown'
) )
).toMatchInlineSnapshot(` ).toMatch(
" 1 of 1 unhandled error 'Objects are not valid as a React child (found: /search/). If you meant to render a collection of children, use an array instead.'
Server Error )
Error: Objects are not valid as a React child (found: /search/). If you meant to render a collection of children, use an array instead.
in Unknown
in App
in Unknown
in Context.Provider
in Context.Provider
in Context.Provider
in Context.Provider
in AppContainer
This error happened while generating the page. Any console logs will be displayed in the terminal window."
`)
aboutPage.restore() aboutPage.restore()
......
...@@ -7,16 +7,16 @@ jest.setTimeout(1000 * 60 * 2) ...@@ -7,16 +7,16 @@ jest.setTimeout(1000 * 60 * 2)
const appDir = join(__dirname, '../') const appDir = join(__dirname, '../')
describe('TypeScript Exclusivity of Numeric Separator', () => { describe('Numeric Separator Support', () => {
it('should fail to build for a JavaScript file', async () => { it('should successfully build for a JavaScript file', async () => {
const { code, stderr } = await nextBuild(appDir, [], { const { code, stdout, stderr } = await nextBuild(appDir, [], {
stdout: true,
stderr: true, stderr: true,
}) })
expect(code).toBe(1) expect(code).toBe(0)
expect(stderr).toContain('Failed to compile.') expect(stdout).toContain('Compiled successfully')
expect(stderr).toContain('Syntax error') expect(stderr).not.toContain('Failed to compile')
expect(stderr).toContain('config to enable transformation')
}) })
}) })
This diff is collapsed.
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