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

build: fix externals resolution when importing from next/dist #55269

Merged
merged 1 commit into from
Sep 12, 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
13 changes: 5 additions & 8 deletions packages/next/src/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1430,7 +1430,7 @@ export default async function getBaseWebpackConfig(
* This is used to ensure that files used across the rendering runtime(s) and the user code are one and the same. The logic in this function
* will rewrite the require to the correct bundle location depending on the layer at which the file is being used.
*/
const isLocalCallback = (localRes: string) => {
const resolveNextExternal = (localRes: string) => {
const isSharedRuntime = sharedRuntimePattern.test(localRes)
const isExternal = externalPattern.test(localRes)

Expand Down Expand Up @@ -1514,8 +1514,7 @@ export default async function getBaseWebpackConfig(
return `module ${request}`
}

// Other Next.js internals need to be transpiled.
return
return resolveNextExternal(request)
huozhi marked this conversation as resolved.
Show resolved Hide resolved
}

// Early return if the request needs to be bundled, such as in the client layer.
Expand All @@ -1535,9 +1534,7 @@ export default async function getBaseWebpackConfig(
const fullRequest = isRelative
? path.join(context, request).replace(/\\/g, '/')
: request
const resolveNextExternal = isLocalCallback(fullRequest)

return resolveNextExternal
return resolveNextExternal(fullRequest)
}

// TODO-APP: Let's avoid this resolve call as much as possible, and eventually get rid of it.
Expand All @@ -1549,7 +1546,7 @@ export default async function getBaseWebpackConfig(
isEsmRequested,
hasAppDir,
getResolve,
isLocal ? isLocalCallback : undefined
isLocal ? resolveNextExternal : undefined
)

if ('localRes' in resolveResult) {
Expand Down Expand Up @@ -1610,7 +1607,7 @@ export default async function getBaseWebpackConfig(
hasAppDir,
isEsmRequested,
getResolve,
isLocal ? isLocalCallback : undefined
isLocal ? resolveNextExternal : undefined
)
if (pkgRes.res) {
resolvedExternalPackageDirs.set(pkg, path.dirname(pkgRes.res))
Expand Down
5 changes: 5 additions & 0 deletions test/e2e/app-dir/app-external/app-external.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,5 +221,10 @@ createNextDescribe(
const middlewareBundle = await next.readFile('.next/server/middleware.js')
expect(middlewareBundle).not.toContain('image-response')
})

it('should use the same async storages if imported directly', async () => {
const html = await next.render('/async-storage')
expect(html).toContain('success')
})
}
)
8 changes: 8 additions & 0 deletions test/e2e/app-dir/app-external/app/async-storage/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { requestAsyncStorage } from 'next/dist/client/components/request-async-storage.external'

export default async function Page() {
// cookies is undefined if not set
return !!requestAsyncStorage.getStore().cookies ? 'success' : 'fail'
}

export const dynamic = 'force-dynamic'