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

Alias nextjs api entry to esm version for app router #59852

Merged
merged 5 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
improve app alias
  • Loading branch information
huozhi committed Dec 21, 2023
commit 325cad68ed84ca098b8578424f3e31bd10166f01
2 changes: 2 additions & 0 deletions packages/next/src/api/app-dynamic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from '../shared/lib/app-dynamic'
export { default } from '../shared/lib/app-dynamic'
33 changes: 17 additions & 16 deletions packages/next/src/build/create-compiler-aliases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,22 +204,34 @@ export function createNextApiEsmAliases() {
navigation: 'next/dist/api/navigation',
headers: 'next/dist/api/headers',
og: 'next/dist/api/og',
// pages api
document: 'next/dist/api/document',
app: 'next/dist/api/app',
huozhi marked this conversation as resolved.
Show resolved Hide resolved
}
const aliasMap: Record<string, string> = {}
// Handle fully specified imports like `next/image.js`
for (const [key, value] of Object.entries(mapping)) {
const nextApiFilePath = path.join(NEXT_PROJECT_ROOT, key)
// filepath alias
aliasMap[nextApiFilePath] = value
aliasMap[nextApiFilePath + '.js'] = value
// api alias
aliasMap['next/' + key] = value
aliasMap['next/' + key + '.js'] = value
}

return aliasMap
}

export function createAppRouterApiAliases() {
const mapping = {
head: 'next/dist/client/components/noop-head',
dynamic: 'next/dist/api/app-dynamic',
}

const aliasMap: Record<string, string> = {}
for (const [key, value] of Object.entries(mapping)) {
const nextApiFilePath = path.join(NEXT_PROJECT_ROOT, key)
aliasMap[nextApiFilePath + '.js'] = value
}
return aliasMap
}

export function createRSCAliases(
bundledReactChannel: string,
{
Expand Down Expand Up @@ -358,14 +370,3 @@ function getReactProfilingInProduction(): CompilerAliases {
'react-dom$': 'react-dom/profiling',
}
}
export function createServerComponentsNoopAliases(): CompilerAliases {
return {
[require.resolve('next/head')]: require.resolve(
'next/dist/client/components/noop-head'
),
// Alias next/dynamic
[require.resolve('next/dynamic')]: require.resolve(
'next/dist/shared/lib/app-dynamic'
),
}
}
27 changes: 6 additions & 21 deletions packages/next/src/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,9 @@ import {
createWebpackAliases,
createServerOnlyClientOnlyAliases,
createRSCAliases,
createServerComponentsNoopAliases,
createNextApiEsmAliases,
createAppRouterApiAliases,
} from './create-compiler-aliases'
// import { createServerOnlyClientOnlyAliases } from './create-compiler-aliases'
// import { createRSCAliases } from './create-compiler-aliases'
// import { createServerComponentsNoopAliases } from './create-compiler-aliases'
import { hasCustomExportOutput } from '../export/utils'

type ExcludesFalse = <T>(x: T | false) => x is T
Expand Down Expand Up @@ -1243,12 +1240,6 @@ export default async function getBaseWebpackConfig(
or: WEBPACK_LAYERS.GROUP.nonClientServerTarget,
},
},
{
issuerLayer: isWebpackAppLayer,
resolve: {
alias: createNextApiEsmAliases(),
},
},
...(hasAppDir
? [
{
Expand Down Expand Up @@ -1279,18 +1270,12 @@ export default async function getBaseWebpackConfig(
test: /next[\\/]dist[\\/](esm[\\/])?server[\\/]future[\\/]route-modules[\\/]app-page[\\/]module/,
},
{
// All app dir layers need to use this configured resolution logic
issuerLayer: {
or: [
WEBPACK_LAYERS.reactServerComponents,
WEBPACK_LAYERS.serverSideRendering,
WEBPACK_LAYERS.appPagesBrowser,
WEBPACK_LAYERS.actionBrowser,
WEBPACK_LAYERS.shared,
],
},
issuerLayer: isWebpackAppLayer,
resolve: {
alias: createServerComponentsNoopAliases(),
alias: {
...createNextApiEsmAliases(),
...createAppRouterApiAliases(),
},
},
},
]
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ const WEBPACK_LAYERS = {
WEBPACK_LAYERS_NAMES.appRouteHandler,
WEBPACK_LAYERS_NAMES.serverSideRendering,
WEBPACK_LAYERS_NAMES.appPagesBrowser,
WEBPACK_LAYERS_NAMES.shared,
],
},
}
Expand Down