Unverified Commit 05bdd715 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Use import to load page and layout (#42325)

Fixes #42534

* Use eager `import()` to load page/layout to avoid esm module resolution error, eager is to make sure all the sub resources like css are also included
* Fix layer detection, should use `module.layer` directly since `module.resourceResolveData` is not alway presented. It lost when switching from `require()` to `import()` for page/layout component

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`
parent e0b946a4
...@@ -921,11 +921,7 @@ export default async function getBaseWebpackConfig( ...@@ -921,11 +921,7 @@ export default async function getBaseWebpackConfig(
...customRootAliases, ...customRootAliases,
...(pagesDir ? { [PAGES_DIR_ALIAS]: pagesDir } : {}), ...(pagesDir ? { [PAGES_DIR_ALIAS]: pagesDir } : {}),
...(appDir ...(appDir ? { [APP_DIR_ALIAS]: appDir } : {}),
? {
[APP_DIR_ALIAS]: appDir,
}
: {}),
[ROOT_DIR_ALIAS]: dir, [ROOT_DIR_ALIAS]: dir,
[DOT_NEXT_ALIAS]: distDir, [DOT_NEXT_ALIAS]: distDir,
...(isClient || isEdgeServer ? getOptimizedAliases() : {}), ...(isClient || isEdgeServer ? getOptimizedAliases() : {}),
......
...@@ -8,7 +8,7 @@ import { verifyRootLayout } from '../../../lib/verifyRootLayout' ...@@ -8,7 +8,7 @@ import { verifyRootLayout } from '../../../lib/verifyRootLayout'
import * as Log from '../../../build/output/log' import * as Log from '../../../build/output/log'
import { APP_DIR_ALIAS } from '../../../lib/constants' import { APP_DIR_ALIAS } from '../../../lib/constants'
export const FILE_TYPES = { const FILE_TYPES = {
layout: 'layout', layout: 'layout',
template: 'template', template: 'template',
error: 'error', error: 'error',
...@@ -68,7 +68,7 @@ async function createTreeCodeFromPath({ ...@@ -68,7 +68,7 @@ async function createTreeCodeFromPath({
// Use '' for segment as it's the page. There can't be a segment called '' so this is the safest way to add it. // Use '' for segment as it's the page. There can't be a segment called '' so this is the safest way to add it.
props[parallelKey] = `['', {}, { props[parallelKey] = `['', {}, {
page: [() => require(${JSON.stringify( page: [() => import(/* webpackMode: "eager" */ ${JSON.stringify(
resolvedPagePath resolvedPagePath
)}), ${JSON.stringify(resolvedPagePath)}]}]` )}), ${JSON.stringify(resolvedPagePath)}]}]`
continue continue
...@@ -106,7 +106,7 @@ async function createTreeCodeFromPath({ ...@@ -106,7 +106,7 @@ async function createTreeCodeFromPath({
if (filePath === undefined) { if (filePath === undefined) {
return '' return ''
} }
return `'${file}': [() => require(${JSON.stringify( return `'${file}': [() => import(/* webpackMode: "eager" */ ${JSON.stringify(
filePath filePath
)}), ${JSON.stringify(filePath)}],` )}), ${JSON.stringify(filePath)}],`
}) })
...@@ -245,16 +245,16 @@ const nextAppLoader: webpack.LoaderDefinitionFunction<{ ...@@ -245,16 +245,16 @@ const nextAppLoader: webpack.LoaderDefinitionFunction<{
export ${treeCode} export ${treeCode}
export const pages = ${JSON.stringify(pages)} export const pages = ${JSON.stringify(pages)}
export const AppRouter = require('next/dist/client/components/app-router.js').default export { default as AppRouter } from 'next/dist/client/components/app-router'
export const LayoutRouter = require('next/dist/client/components/layout-router.js').default export { default as LayoutRouter } from 'next/dist/client/components/layout-router'
export const RenderFromTemplateContext = require('next/dist/client/components/render-from-template-context.js').default export { default as RenderFromTemplateContext } from 'next/dist/client/components/render-from-template-context'
export const staticGenerationAsyncStorage = require('next/dist/client/components/static-generation-async-storage').staticGenerationAsyncStorage export { staticGenerationAsyncStorage } from 'next/dist/client/components/static-generation-async-storage'
export const requestAsyncStorage = require('next/dist/client/components/request-async-storage.js').requestAsyncStorage export { requestAsyncStorage } from 'next/dist/client/components/request-async-storage'
export const serverHooks = require('next/dist/client/components/hooks-server-context.js') export * as serverHooks from 'next/dist/client/components/hooks-server-context'
export const renderToReadableStream = require('next/dist/compiled/react-server-dom-webpack/server.browser').renderToReadableStream export { renderToReadableStream } from 'next/dist/compiled/react-server-dom-webpack/server.browser'
export const __next_app_webpack_require__ = __webpack_require__ export const __next_app_webpack_require__ = __webpack_require__
` `
......
...@@ -85,13 +85,11 @@ export class FlightClientEntryPlugin { ...@@ -85,13 +85,11 @@ export class FlightClientEntryPlugin {
const recordModule = (modId: string, mod: any) => { const recordModule = (modId: string, mod: any) => {
const modResource = mod.resourceResolveData?.path || mod.resource const modResource = mod.resourceResolveData?.path || mod.resource
if ( if (mod.layer !== WEBPACK_LAYERS.client) {
mod.resourceResolveData?.context?.issuerLayer !==
WEBPACK_LAYERS.client
) {
return return
} }
// Check mod resource to exclude the empty resource module like virtual module created by next-flight-client-entry-loader
if (typeof modId !== 'undefined' && modResource) { if (typeof modId !== 'undefined' && modResource) {
// Note that this isn't that reliable as webpack is still possible to assign // Note that this isn't that reliable as webpack is still possible to assign
// additional queries to make sure there's no conflict even using the `named` // additional queries to make sure there's no conflict even using the `named`
......
...@@ -915,7 +915,7 @@ export async function renderToHTMLOrFlight( ...@@ -915,7 +915,7 @@ export async function renderToHTMLOrFlight(
} }
if (head) { if (head) {
const Head = await interopDefault(head[0]()) const Head = await interopDefault(await head[0]())
return <Head params={currentParams} /> return <Head params={currentParams} />
} }
...@@ -993,7 +993,7 @@ export async function renderToHTMLOrFlight( ...@@ -993,7 +993,7 @@ export async function renderToHTMLOrFlight(
/> />
)) ))
: null : null
const Comp = await interopDefault(getComponent()) const Comp = await interopDefault(await getComponent())
return [Comp, styles] return [Comp, styles]
} }
......
...@@ -22,6 +22,9 @@ describe('app-dir alias handling', () => { ...@@ -22,6 +22,9 @@ describe('app-dir alias handling', () => {
'@types/react': 'latest', '@types/react': 'latest',
'@types/node': 'latest', '@types/node': 'latest',
}, },
packageJson: {
type: 'module',
},
}) })
}) })
afterAll(() => next.destroy()) afterAll(() => next.destroy())
......
module.exports = { export default {
experimental: { experimental: {
appDir: true, appDir: true,
}, },
......
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