From 259f94808bee6eb8731157e211bdf01885cec3d0 Mon Sep 17 00:00:00 2001 From: Jimmy Lai Date: Tue, 12 Sep 2023 11:40:39 +0200 Subject: [PATCH] fix next/dist imports --- packages/next/src/build/webpack-config.ts | 13 +++++-------- test/e2e/app-dir/app-external/app-external.test.ts | 5 +++++ .../app-dir/app-external/app/async-storage/page.js | 8 ++++++++ 3 files changed, 18 insertions(+), 8 deletions(-) create mode 100644 test/e2e/app-dir/app-external/app/async-storage/page.js diff --git a/packages/next/src/build/webpack-config.ts b/packages/next/src/build/webpack-config.ts index 00f85439e00cd..a9273b46bdc2f 100644 --- a/packages/next/src/build/webpack-config.ts +++ b/packages/next/src/build/webpack-config.ts @@ -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) @@ -1514,8 +1514,7 @@ export default async function getBaseWebpackConfig( return `module ${request}` } - // Other Next.js internals need to be transpiled. - return + return resolveNextExternal(request) } // Early return if the request needs to be bundled, such as in the client layer. @@ -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. @@ -1549,7 +1546,7 @@ export default async function getBaseWebpackConfig( isEsmRequested, hasAppDir, getResolve, - isLocal ? isLocalCallback : undefined + isLocal ? resolveNextExternal : undefined ) if ('localRes' in resolveResult) { @@ -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)) diff --git a/test/e2e/app-dir/app-external/app-external.test.ts b/test/e2e/app-dir/app-external/app-external.test.ts index 38188fa77fc6f..5f7de4b9fe15b 100644 --- a/test/e2e/app-dir/app-external/app-external.test.ts +++ b/test/e2e/app-dir/app-external/app-external.test.ts @@ -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') + }) } ) diff --git a/test/e2e/app-dir/app-external/app/async-storage/page.js b/test/e2e/app-dir/app-external/app/async-storage/page.js new file mode 100644 index 0000000000000..4ec8618ff52b3 --- /dev/null +++ b/test/e2e/app-dir/app-external/app/async-storage/page.js @@ -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'