Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply optimization to middleware when using babel #51067

Merged
merged 1 commit into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/next/src/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,14 @@ export default async function getBaseWebpackConfig(
: []
const swcLoaderForMiddlewareLayer = useSWCLoader
? getSwcLoader({ hasServerComponents: false })
: getBabelLoader()
: // When using Babel, we will have to use SWC to do the optimization
// for middleware to tree shake the unused default optimized imports like "next/server".
// This will cause some performance overhead but
// acceptable as Babel will not be recommended.
[
getSwcLoader({ hasServerComponents: false, isServerLayer: false }),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
getSwcLoader({ hasServerComponents: false, isServerLayer: false }),
getSwcLoader({ hasServerComponents: false }),

isServerLayer: false is not necessary here.

getBabelLoader(),
]

// Loader for API routes needs to be differently configured as it shouldn't
// have RSC transpiler enabled, so syntax checks such as invalid imports won't
Expand Down
5 changes: 5 additions & 0 deletions test/e2e/app-dir/with-babel/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { NextResponse } from 'next/server'

export async function middleware() {
return NextResponse.next()
}
10 changes: 9 additions & 1 deletion test/e2e/app-dir/with-babel/with-babel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ createNextDescribe(
files: __dirname,
skipDeployment: true,
},
({ next }) => {
({ next, isNextStart }) => {
it('should support babel in app dir', async () => {
const $ = await next.render$('/')
expect($('h1').text()).toBe('hello')
})

if (isNextStart) {
it('should contain og package files in middleware', async () => {
const middleware = await next.readFile('.next/server/middleware.js')
// @vercel/og default font should be bundled
expect(middleware).not.toContain('noto-sans-v27-latin-regular.ttf')
})
}
}
)