Skip to content

Commit

Permalink
fix: babel usage with next/image (#61835)
Browse files Browse the repository at this point in the history
### Why

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

### What

- Fixes #61116

Closes NEXT-2410
Closes NEXT-2328

---------

Co-authored-by: Jiachi Liu <inbox@huozhi.im>
  • Loading branch information
styfle and huozhi authored Feb 8, 2024
1 parent 2ca8d3d commit 698fcbb
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@
"p-limit": "3.1.0",
"path-browserify": "1.0.1",
"path-to-regexp": "6.1.0",
"picomatch": "3.0.1",
"picomatch": "4.0.1",
"platform": "1.3.6",
"postcss-flexbugs-fixes": "5.0.2",
"postcss-modules-extract-imports": "3.0.0",
Expand Down
22 changes: 18 additions & 4 deletions packages/next/src/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,16 @@ const babelIncludeRegexes: RegExp[] = [
/[\\/](strip-ansi|ansi-regex|styled-jsx)[\\/]/,
]

const preCompileReactRegex =
/next[\\/]dist[\\/]compiled[\\/](react|react-dom|react-server-dom-webpack)(-experimental)?($|[\\/])/
const browserNonTranspileModules = [
// 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 =
/next[\\/]dist[\\/](esm[\\/])?client[\\/]components[\\/](static-generation-async-storage|action-async-storage|request-async-storage)/
Expand Down Expand Up @@ -1423,7 +1431,12 @@ export default async function getBaseWebpackConfig(
? [
{
test: codeCondition.test,
exclude: [codeCondition.exclude, transpilePackagesRegex],
exclude: [
// exclude unchanged modules from react-refresh
codeCondition.exclude,
transpilePackagesRegex,
precompileRegex,
],
issuerLayer: WEBPACK_LAYERS.appPagesBrowser,
use: reactRefreshLoaders,
resolve: {
Expand Down Expand Up @@ -1471,7 +1484,8 @@ export default async function getBaseWebpackConfig(
{
test: codeCondition.test,
issuerLayer: WEBPACK_LAYERS.appPagesBrowser,
exclude: preCompileReactRegex,
// Exclude the transpilation of the app layer due to compilation issues
exclude: browserNonTranspileModules,
use: appBrowserLayerLoaders,
resolve: {
mainFields: getMainField(compilerType, true),
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/compiled/picomatch/index.js

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions test/integration/babel-next-image/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"presets": ["next/babel"],
"plugins": []
}
12 changes: 12 additions & 0 deletions test/integration/babel-next-image/app/layout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const metadata = {
title: 'Next.js',
description: 'Generated by Next.js',
}

export default function RootLayout({ children }) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
12 changes: 12 additions & 0 deletions test/integration/babel-next-image/app/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Image from 'next/image'

export default function Home() {
return (
<Image
alt="red square"
src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8DwHwAFBQIAX8jx0gAAAABJRU5ErkJggg=="
width={64}
height={64}
/>
)
}
19 changes: 19 additions & 0 deletions test/integration/babel-next-image/babel-next-image.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* 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)
})
})

0 comments on commit 698fcbb

Please sign in to comment.