Skip to content

Commit

Permalink
optimize filters
Browse files Browse the repository at this point in the history
  • Loading branch information
shuding committed Mar 1, 2022
1 parent b6b5250 commit 1cf2592
Showing 1 changed file with 18 additions and 29 deletions.
47 changes: 18 additions & 29 deletions packages/next/build/webpack/loaders/next-flight-server-loader.ts
Expand Up @@ -5,38 +5,27 @@ import { parse } from '../../swc'
import { getBaseSWCOptions } from '../../swc/options'
import { getRawPageExtensions } from '../../utils'

const createClientComponentFilter =
(pageExtensions: string[]) => (importSource: string) => {
const hasClientExtension = new RegExp(
`\\.client(\\.(${pageExtensions.join('|')}))?`
).test(importSource)
// Special cases for Next.js APIs that are considered as client components:
return (
hasClientExtension ||
isNextComponent(importSource) ||
isImageImport(importSource)
)
}

const createServerComponentFilter =
(pageExtensions: string[]) => (importSource: string) => {
return new RegExp(`\\.server(\\.(${pageExtensions.join('|')}))?`).test(
importSource
)
}

function isNextComponent(importSource: string) {
return (
importSource.includes('next/link') || importSource.includes('next/image')
const imageExtensions = ['jpg', 'jpeg', 'png', 'webp', 'avif']

const createClientComponentFilter = (pageExtensions: string[]) => {
// Special cases for Next.js APIs that are considered as client components:
// - .client.[ext]
// - next/link, next/image
// - .[imageExt]
const regex = new RegExp(
'(' +
`\\.client(\\.(${pageExtensions.join('|')}))?|` +
`next/link|next/image|` +
`\\.(${imageExtensions.join('|')})` +
')$'
)

return (importSource: string) => regex.test(importSource)
}

function isImageImport(importSource: string) {
// TODO: share extension with next/image
// TODO: add other static assets, jpeg -> jpg
return ['jpg', 'jpeg', 'png', 'webp', 'avif'].some((imageExt) =>
importSource.endsWith('.' + imageExt)
)
const createServerComponentFilter = (pageExtensions: string[]) => {
const regex = new RegExp(`\\.server(\\.(${pageExtensions.join('|')}))?$`)
return (importSource: string) => regex.test(importSource)
}

async function parseImportsInfo({
Expand Down

0 comments on commit 1cf2592

Please sign in to comment.