Commit 7dbf6f82 authored by Steven's avatar Steven

fix: babel usage with next/image (#61835)

The `process/browser` lib was imported and being transformed, but
`process.browser = true` turns to `true = true` that causes the
compilation error, we need to exclude them from certain compilation
layers

- Fixes https://github.com/vercel/next.js/issues/61116

Closes NEXT-2410
Closes NEXT-2328

---------
Co-authored-by: 's avatarJiachi Liu <inbox@huozhi.im>
parent 3efc842a
...@@ -110,8 +110,16 @@ const babelIncludeRegexes: RegExp[] = [ ...@@ -110,8 +110,16 @@ const babelIncludeRegexes: RegExp[] = [
/[\\/](strip-ansi|ansi-regex|styled-jsx)[\\/]/, /[\\/](strip-ansi|ansi-regex|styled-jsx)[\\/]/,
] ]
const preCompileReactRegex = const browserNonTranspileModules = [
/next[\\/]dist[\\/]compiled[\\/](react|react-dom|react-server-dom-webpack)(-experimental)?($|[\\/])/ // Transpiling `process/browser` will trigger babel compilation error due to value replacement.
// TypeError: Property left of AssignmentExpression expected node to be of a type ["LVal"] but instead got "BooleanLiteral"
// e.g. `process.browser = true` will become `true = true`.
/[\\/]node_modules[\\/]process[\\/]browser/,
// Exclude precompiled react packages from browser compilation due to SWC helper insertion (#61791),
// We fixed the issue but it's safer to exclude them from compilation since they don't need to be re-compiled.
/[\\/]next[\\/]dist[\\/]compiled[\\/](react|react-dom|react-server-dom-webpack)(-experimental)?($|[\\/])/,
]
const precompileRegex = /[\\/]next[\\/]dist[\\/]compiled[\\/]/
const asyncStoragesRegex = const asyncStoragesRegex =
/next[\\/]dist[\\/](esm[\\/])?client[\\/]components[\\/](static-generation-async-storage|action-async-storage|request-async-storage)/ /next[\\/]dist[\\/](esm[\\/])?client[\\/]components[\\/](static-generation-async-storage|action-async-storage|request-async-storage)/
...@@ -1400,7 +1408,12 @@ export default async function getBaseWebpackConfig( ...@@ -1400,7 +1408,12 @@ export default async function getBaseWebpackConfig(
? [ ? [
{ {
test: codeCondition.test, test: codeCondition.test,
exclude: [codeCondition.exclude, transpilePackagesRegex], exclude: [
// exclude unchanged modules from react-refresh
codeCondition.exclude,
transpilePackagesRegex,
precompileRegex,
],
issuerLayer: WEBPACK_LAYERS.appPagesBrowser, issuerLayer: WEBPACK_LAYERS.appPagesBrowser,
use: reactRefreshLoaders, use: reactRefreshLoaders,
resolve: { resolve: {
...@@ -1443,7 +1456,8 @@ export default async function getBaseWebpackConfig( ...@@ -1443,7 +1456,8 @@ export default async function getBaseWebpackConfig(
{ {
test: codeCondition.test, test: codeCondition.test,
issuerLayer: WEBPACK_LAYERS.appPagesBrowser, issuerLayer: WEBPACK_LAYERS.appPagesBrowser,
exclude: preCompileReactRegex, // Exclude the transpilation of the app layer due to compilation issues
exclude: browserNonTranspileModules,
use: appBrowserLayerLoaders, use: appBrowserLayerLoaders,
resolve: { resolve: {
mainFields: getMainField(compilerType, true), mainFields: getMainField(compilerType, true),
......
{
"presets": ["next/babel"],
"plugins": []
}
export const metadata = {
title: 'Next.js',
description: 'Generated by Next.js',
}
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
import Image from 'next/image'
export default function Home() {
return (
<Image
alt="red square"
src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8DwHwAFBQIAX8jx0gAAAABJRU5ErkJggg=="
width={64}
height={64}
/>
)
}
/* eslint-env jest */
import { fetchViaHTTP, findPort, killApp, launchApp } from 'next-test-utils'
;(process.env.TURBOPACK ? describe.skip : describe)('babel-next-image', () => {
let appPort
let app
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(__dirname, appPort)
})
afterAll(() => killApp(app))
it('should work with babel and next/image', async () => {
const res = await fetchViaHTTP(appPort, '/')
expect(res.status).toBe(200)
})
})
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