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

Fix missing SWC transforms for optimized barrel files #57474

Merged
merged 4 commits into from
Oct 26, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/next/src/build/webpack/loaders/next-barrel-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ import type webpack from 'webpack'

import path from 'path'
import { transform } from '../../swc'
import { WEBPACK_LAYERS } from '../../../lib/constants'

// This is a in-memory cache for the mapping of barrel exports. This only applies
// to the packages that we optimize. It will never change (e.g. upgrading packages)
Expand All @@ -103,6 +104,7 @@ const barrelTransformMappingCache = new Map<
>()

async function getBarrelMapping(
layer: string | null | undefined,
resourcePath: string,
swcCacheDir: string,
resolve: (context: string, request: string) => Promise<string>,
Expand Down Expand Up @@ -133,6 +135,9 @@ async function getBarrelMapping(
optimizeBarrelExports: {
wildcard: isWildcard,
},
serverComponents: {
isReactServerLayer: layer === WEBPACK_LAYERS.reactServerComponents,
},
jsc: {
parser: {
syntax: isTypeScript ? 'typescript' : 'ecmascript',
Expand Down Expand Up @@ -244,6 +249,7 @@ const NextBarrelLoader = async function (
})

const mapping = await getBarrelMapping(
this._module?.layer,
this.resourcePath,
swcCacheDir,
resolve,
Expand Down
5 changes: 5 additions & 0 deletions test/development/basic/barrel-optimization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,9 @@ describe('optimizePackageImports', () => {
const html = await next.render('/client')
expect(html).toContain('this is a client component')
})

it('should support "use client" directive in barrel file', async () => {
const html = await next.render('/client-boundary')
expect(html).toContain('<button>0</button>')
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Button } from 'my-client-lib'

export default function Page() {
return <Button />
}
3 changes: 2 additions & 1 deletion test/development/basic/barrel-optimization/next.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
transpilePackages: ['my-client-lib'],
experimental: {
optimizePackageImports: ['my-lib', 'recursive-barrel'],
optimizePackageImports: ['my-lib', 'recursive-barrel', 'my-client-lib'],
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { useState } from 'react'

export function Button() {
const [count, setCount] = useState(0)
return <button onClick={() => setCount(count + 1)}>{count}</button>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use client'

export { Button } from './button'
Loading