Unverified Commit 79a85b73 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Alias all client, shared, pages dist assets for esm (#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.
parent 26a23e98
module.exports = module.exports = require('./dist/shared/lib/amp')
process.env.NEXT_RUNTIME === 'edge'
? require('./dist/esm/shared/lib/amp')
: require('./dist/shared/lib/amp')
module.exports = module.exports = require('./dist/pages/_app')
process.env.NEXT_RUNTIME === 'edge'
? require('./dist/esm/pages/_app')
: require('./dist/pages/_app')
...@@ -219,7 +219,6 @@ export function getAppEntry(opts: { ...@@ -219,7 +219,6 @@ export function getAppEntry(opts: {
appDir: string appDir: string
appPaths: string[] | null appPaths: string[] | null
pageExtensions: string[] pageExtensions: string[]
nextRuntime: string
}) { }) {
return { return {
import: `next-app-loader?${stringify(opts)}!`, import: `next-app-loader?${stringify(opts)}!`,
...@@ -456,7 +455,6 @@ export async function createEntrypoints(params: CreateEntrypointsParams) { ...@@ -456,7 +455,6 @@ export async function createEntrypoints(params: CreateEntrypointsParams) {
appDir, appDir,
appPaths: matchedAppPaths, appPaths: matchedAppPaths,
pageExtensions, pageExtensions,
nextRuntime: 'nodejs',
}) })
} else if (isTargetLikeServerless(target)) { } else if (isTargetLikeServerless(target)) {
if (page !== '/_app' && page !== '/_document') { if (page !== '/_app' && page !== '/_document') {
...@@ -481,7 +479,6 @@ export async function createEntrypoints(params: CreateEntrypointsParams) { ...@@ -481,7 +479,6 @@ export async function createEntrypoints(params: CreateEntrypointsParams) {
appDir: appDir!, appDir: appDir!,
appPaths: matchedAppPaths, appPaths: matchedAppPaths,
pageExtensions, pageExtensions,
nextRuntime: 'edge',
}).import }).import
} }
......
...@@ -788,7 +788,7 @@ export default async function getBaseWebpackConfig( ...@@ -788,7 +788,7 @@ export default async function getBaseWebpackConfig(
return prev return prev
}, [] as string[]) }, [] as string[])
: []), : []),
isEdgeServer ? 'next/dist/esm/pages/_app.js' : 'next/dist/pages/_app.js', 'next/dist/pages/_app.js',
] ]
customAppAliases[`${PAGES_DIR_ALIAS}/_error`] = [ customAppAliases[`${PAGES_DIR_ALIAS}/_error`] = [
...(pagesDir ...(pagesDir
...@@ -797,9 +797,7 @@ export default async function getBaseWebpackConfig( ...@@ -797,9 +797,7 @@ export default async function getBaseWebpackConfig(
return prev return prev
}, [] as string[]) }, [] as string[])
: []), : []),
isEdgeServer 'next/dist/pages/_error.js',
? 'next/dist/esm/pages/_error.js'
: 'next/dist/pages/_error.js',
] ]
customDocumentAliases[`${PAGES_DIR_ALIAS}/_document`] = [ customDocumentAliases[`${PAGES_DIR_ALIAS}/_document`] = [
...(pagesDir ...(pagesDir
...@@ -808,9 +806,7 @@ export default async function getBaseWebpackConfig( ...@@ -808,9 +806,7 @@ export default async function getBaseWebpackConfig(
return prev return prev
}, [] as string[]) }, [] as string[])
: []), : []),
isEdgeServer 'next/dist/pages/_document.js',
? `next/dist/esm/pages/_document.js`
: `next/dist/pages/_document.js`,
] ]
} }
...@@ -830,6 +826,16 @@ export default async function getBaseWebpackConfig( ...@@ -830,6 +826,16 @@ export default async function getBaseWebpackConfig(
...nodePathList, // Support for NODE_PATH environment variable ...nodePathList, // Support for NODE_PATH environment variable
], ],
alias: { alias: {
// Alias next/dist imports to next/dist/esm assets,
// let this alias hit before `next` alias.
...(isEdgeServer
? {
'next/dist/client': 'next/dist/esm/client',
'next/dist/shared': 'next/dist/esm/shared',
'next/dist/pages': 'next/dist/esm/pages',
}
: undefined),
next: NEXT_PROJECT_ROOT, next: NEXT_PROJECT_ROOT,
react: reactDir, react: reactDir,
......
...@@ -120,9 +120,8 @@ const nextAppLoader: webpack.LoaderDefinitionFunction<{ ...@@ -120,9 +120,8 @@ const nextAppLoader: webpack.LoaderDefinitionFunction<{
appDir: string appDir: string
appPaths: string[] | null appPaths: string[] | null
pageExtensions: string[] pageExtensions: string[]
nextRuntime: string
}> = async function nextAppLoader() { }> = async function nextAppLoader() {
const { name, appDir, appPaths, pagePath, pageExtensions, nextRuntime } = const { name, appDir, appPaths, pagePath, pageExtensions } =
this.getOptions() || {} this.getOptions() || {}
const buildInfo = getModuleBuildInfo((this as any)._module) const buildInfo = getModuleBuildInfo((this as any)._module)
...@@ -180,24 +179,23 @@ const nextAppLoader: webpack.LoaderDefinitionFunction<{ ...@@ -180,24 +179,23 @@ const nextAppLoader: webpack.LoaderDefinitionFunction<{
resolveParallelSegments, resolveParallelSegments,
}) })
const rootDistFolder = nextRuntime === 'edge' ? 'next/dist/esm' : 'next/dist'
const result = ` const result = `
export ${treeCode} export ${treeCode}
export const AppRouter = require('${rootDistFolder}/client/components/app-router.client.js').default export const AppRouter = require('next/dist/client/components/app-router.client.js').default
export const LayoutRouter = require('${rootDistFolder}/client/components/layout-router.client.js').default export const LayoutRouter = require('next/dist/client/components/layout-router.client.js').default
export const RenderFromTemplateContext = require('${rootDistFolder}/client/components/render-from-template-context.client.js').default export const RenderFromTemplateContext = require('next/dist/client/components/render-from-template-context.client.js').default
export const HotReloader = ${ export const HotReloader = ${
// Disable HotReloader component in production // Disable HotReloader component in production
this.mode === 'development' this.mode === 'development'
? `require('${rootDistFolder}/client/components/hot-reloader.client.js').default` ? `require('next/dist/client/components/hot-reloader.client.js').default`
: 'null' : 'null'
} }
export const staticGenerationAsyncStorage = require('${rootDistFolder}/client/components/static-generation-async-storage.js').staticGenerationAsyncStorage export const staticGenerationAsyncStorage = require('next/dist/client/components/static-generation-async-storage.js').staticGenerationAsyncStorage
export const requestAsyncStorage = require('${rootDistFolder}/client/components/request-async-storage.js').requestAsyncStorage export const requestAsyncStorage = require('next/dist/client/components/request-async-storage.js').requestAsyncStorage
export const serverHooks = require('${rootDistFolder}/client/components/hooks-server-context.js') export const serverHooks = require('next/dist/client/components/hooks-server-context.js')
export const renderToReadableStream = require('next/dist/compiled/react-server-dom-webpack/writer.browser.server').renderToReadableStream export const renderToReadableStream = require('next/dist/compiled/react-server-dom-webpack/writer.browser.server').renderToReadableStream
export const __next_app_webpack_require__ = __webpack_require__ export const __next_app_webpack_require__ = __webpack_require__
......
module.exports = module.exports = require('./dist/client/index')
process.env.NEXT_RUNTIME === 'edge'
? require('./dist/esm/client/index')
: require('./dist/client/index')
module.exports = module.exports = require('./dist/shared/lib/runtime-config')
process.env.NEXT_RUNTIME === 'edge'
? require('./dist/esm/shared/lib/runtime-config')
: require('./dist/shared/lib/runtime-config')
module.exports = module.exports = require('./dist/shared/lib/constants')
process.env.NEXT_RUNTIME === 'edge'
? require('./dist/esm/shared/lib/constants')
: require('./dist/shared/lib/constants')
module.exports = module.exports = require('./dist/pages/_document')
process.env.NEXT_RUNTIME === 'edge'
? require('./dist/esm/pages/_document')
: require('./dist/pages/_document')
module.exports = module.exports = require('./dist/shared/lib/dynamic')
process.env.NEXT_RUNTIME === 'edge'
? require('./dist/esm/shared/lib/dynamic')
: require('./dist/shared/lib/dynamic')
module.exports = module.exports = require('./dist/pages/_error')
process.env.NEXT_RUNTIME === 'edge'
? require('./dist/esm/pages/_error')
: require('./dist/pages/_error')
module.exports = module.exports = require('./dist/shared/lib/head')
process.env.NEXT_RUNTIME === 'edge'
? require('./dist/esm/shared/lib/head')
: require('./dist/shared/lib/head')
module.exports = module.exports = require('./dist/client/image')
process.env.NEXT_RUNTIME === 'edge'
? require('./dist/esm/client/image')
: require('./dist/client/image')
module.exports = module.exports = require('./dist/client/link')
process.env.NEXT_RUNTIME === 'edge'
? require('./dist/esm/client/link')
: require('./dist/client/link')
module.exports = module.exports = require('./dist/client/router')
process.env.NEXT_RUNTIME === 'edge'
? require('./dist/esm/client/router')
: require('./dist/client/router')
...@@ -626,7 +626,6 @@ export default class HotReloader { ...@@ -626,7 +626,6 @@ export default class HotReloader {
), ),
appDir: this.appDir!, appDir: this.appDir!,
pageExtensions: this.config.pageExtensions, pageExtensions: this.config.pageExtensions,
nextRuntime: 'edge',
}).import }).import
: undefined : undefined
...@@ -706,7 +705,6 @@ export default class HotReloader { ...@@ -706,7 +705,6 @@ export default class HotReloader {
), ),
appDir: this.appDir!, appDir: this.appDir!,
pageExtensions: this.config.pageExtensions, pageExtensions: this.config.pageExtensions,
nextRuntime: 'nodejs',
}) })
: relativeRequest, : relativeRequest,
appDir: this.config.experimental.appDir, appDir: this.config.experimental.appDir,
......
'client'
import { useSelectedLayoutSegment } from 'next/dist/client/components/hooks-client'
export default function Layout({ children }: { children: React.ReactNode }) {
// useSelectedLayoutSegment should not be thrown
useSelectedLayoutSegment()
return children
}
export const config = {
runtime: 'experimental-edge',
}
export default function App({ Component, pageProps }) {
return (
<div className="app-client-root">
<Component {...pageProps} />
</div>
)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment