Skip to content

Commit 79a85b7

Browse files
authored
Alias all client, shared, pages dist assets for esm (vercel#41034)
Alias all existing imports from `next/dist/..` to `next/dist/esm` for edge compiler. So that we don't need checking for `process.env.NEXT_RUNTIME === 'edge'` or passing down `nextRuntime` to decide wether the esm or cjs asset to require This will also fix the issue that some layouts hook are been included twice into the bundle with cjs and esm bundle in edge runtime, now only esm chunk will be bundled in server.
1 parent 26a23e9 commit 79a85b7

File tree

18 files changed

+46
-77
lines changed

18 files changed

+46
-77
lines changed

packages/next/amp.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
module.exports =
2-
process.env.NEXT_RUNTIME === 'edge'
3-
? require('./dist/esm/shared/lib/amp')
4-
: require('./dist/shared/lib/amp')
1+
module.exports = require('./dist/shared/lib/amp')

packages/next/app.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
module.exports =
2-
process.env.NEXT_RUNTIME === 'edge'
3-
? require('./dist/esm/pages/_app')
4-
: require('./dist/pages/_app')
1+
module.exports = require('./dist/pages/_app')

packages/next/build/entries.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ export function getAppEntry(opts: {
219219
appDir: string
220220
appPaths: string[] | null
221221
pageExtensions: string[]
222-
nextRuntime: string
223222
}) {
224223
return {
225224
import: `next-app-loader?${stringify(opts)}!`,
@@ -456,7 +455,6 @@ export async function createEntrypoints(params: CreateEntrypointsParams) {
456455
appDir,
457456
appPaths: matchedAppPaths,
458457
pageExtensions,
459-
nextRuntime: 'nodejs',
460458
})
461459
} else if (isTargetLikeServerless(target)) {
462460
if (page !== '/_app' && page !== '/_document') {
@@ -481,7 +479,6 @@ export async function createEntrypoints(params: CreateEntrypointsParams) {
481479
appDir: appDir!,
482480
appPaths: matchedAppPaths,
483481
pageExtensions,
484-
nextRuntime: 'edge',
485482
}).import
486483
}
487484

packages/next/build/webpack-config.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ export default async function getBaseWebpackConfig(
788788
return prev
789789
}, [] as string[])
790790
: []),
791-
isEdgeServer ? 'next/dist/esm/pages/_app.js' : 'next/dist/pages/_app.js',
791+
'next/dist/pages/_app.js',
792792
]
793793
customAppAliases[`${PAGES_DIR_ALIAS}/_error`] = [
794794
...(pagesDir
@@ -797,9 +797,7 @@ export default async function getBaseWebpackConfig(
797797
return prev
798798
}, [] as string[])
799799
: []),
800-
isEdgeServer
801-
? 'next/dist/esm/pages/_error.js'
802-
: 'next/dist/pages/_error.js',
800+
'next/dist/pages/_error.js',
803801
]
804802
customDocumentAliases[`${PAGES_DIR_ALIAS}/_document`] = [
805803
...(pagesDir
@@ -808,9 +806,7 @@ export default async function getBaseWebpackConfig(
808806
return prev
809807
}, [] as string[])
810808
: []),
811-
isEdgeServer
812-
? `next/dist/esm/pages/_document.js`
813-
: `next/dist/pages/_document.js`,
809+
'next/dist/pages/_document.js',
814810
]
815811
}
816812

@@ -830,6 +826,16 @@ export default async function getBaseWebpackConfig(
830826
...nodePathList, // Support for NODE_PATH environment variable
831827
],
832828
alias: {
829+
// Alias next/dist imports to next/dist/esm assets,
830+
// let this alias hit before `next` alias.
831+
...(isEdgeServer
832+
? {
833+
'next/dist/client': 'next/dist/esm/client',
834+
'next/dist/shared': 'next/dist/esm/shared',
835+
'next/dist/pages': 'next/dist/esm/pages',
836+
}
837+
: undefined),
838+
833839
next: NEXT_PROJECT_ROOT,
834840

835841
react: reactDir,

packages/next/build/webpack/loaders/next-app-loader.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,8 @@ const nextAppLoader: webpack.LoaderDefinitionFunction<{
120120
appDir: string
121121
appPaths: string[] | null
122122
pageExtensions: string[]
123-
nextRuntime: string
124123
}> = async function nextAppLoader() {
125-
const { name, appDir, appPaths, pagePath, pageExtensions, nextRuntime } =
124+
const { name, appDir, appPaths, pagePath, pageExtensions } =
126125
this.getOptions() || {}
127126

128127
const buildInfo = getModuleBuildInfo((this as any)._module)
@@ -180,24 +179,23 @@ const nextAppLoader: webpack.LoaderDefinitionFunction<{
180179
resolveParallelSegments,
181180
})
182181

183-
const rootDistFolder = nextRuntime === 'edge' ? 'next/dist/esm' : 'next/dist'
184182
const result = `
185183
export ${treeCode}
186184
187-
export const AppRouter = require('${rootDistFolder}/client/components/app-router.client.js').default
188-
export const LayoutRouter = require('${rootDistFolder}/client/components/layout-router.client.js').default
189-
export const RenderFromTemplateContext = require('${rootDistFolder}/client/components/render-from-template-context.client.js').default
185+
export const AppRouter = require('next/dist/client/components/app-router.client.js').default
186+
export const LayoutRouter = require('next/dist/client/components/layout-router.client.js').default
187+
export const RenderFromTemplateContext = require('next/dist/client/components/render-from-template-context.client.js').default
190188
export const HotReloader = ${
191189
// Disable HotReloader component in production
192190
this.mode === 'development'
193-
? `require('${rootDistFolder}/client/components/hot-reloader.client.js').default`
191+
? `require('next/dist/client/components/hot-reloader.client.js').default`
194192
: 'null'
195193
}
196194
197-
export const staticGenerationAsyncStorage = require('${rootDistFolder}/client/components/static-generation-async-storage.js').staticGenerationAsyncStorage
198-
export const requestAsyncStorage = require('${rootDistFolder}/client/components/request-async-storage.js').requestAsyncStorage
195+
export const staticGenerationAsyncStorage = require('next/dist/client/components/static-generation-async-storage.js').staticGenerationAsyncStorage
196+
export const requestAsyncStorage = require('next/dist/client/components/request-async-storage.js').requestAsyncStorage
199197
200-
export const serverHooks = require('${rootDistFolder}/client/components/hooks-server-context.js')
198+
export const serverHooks = require('next/dist/client/components/hooks-server-context.js')
201199
202200
export const renderToReadableStream = require('next/dist/compiled/react-server-dom-webpack/writer.browser.server').renderToReadableStream
203201
export const __next_app_webpack_require__ = __webpack_require__

packages/next/client.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
module.exports =
2-
process.env.NEXT_RUNTIME === 'edge'
3-
? require('./dist/esm/client/index')
4-
: require('./dist/client/index')
1+
module.exports = require('./dist/client/index')

packages/next/config.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
module.exports =
2-
process.env.NEXT_RUNTIME === 'edge'
3-
? require('./dist/esm/shared/lib/runtime-config')
4-
: require('./dist/shared/lib/runtime-config')
1+
module.exports = require('./dist/shared/lib/runtime-config')

packages/next/constants.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
module.exports =
2-
process.env.NEXT_RUNTIME === 'edge'
3-
? require('./dist/esm/shared/lib/constants')
4-
: require('./dist/shared/lib/constants')
1+
module.exports = require('./dist/shared/lib/constants')

packages/next/document.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
module.exports =
2-
process.env.NEXT_RUNTIME === 'edge'
3-
? require('./dist/esm/pages/_document')
4-
: require('./dist/pages/_document')
1+
module.exports = require('./dist/pages/_document')

packages/next/dynamic.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
module.exports =
2-
process.env.NEXT_RUNTIME === 'edge'
3-
? require('./dist/esm/shared/lib/dynamic')
4-
: require('./dist/shared/lib/dynamic')
1+
module.exports = require('./dist/shared/lib/dynamic')

0 commit comments

Comments
 (0)