Unverified Commit bc19c2a5 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Fix code splitting and build target for the server-web build (#30972)

- Code splitting should be disabled for the server-web build. Done via `ServerlessPlugin`.
- ~Target can't be `web`, `webworker` is better.~ Using `web` and `es6` for now, still not ideal.
- https://github.com/acornjs/acorn/issues/970

## Bug

- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
parent 4b6cea2e
...@@ -1450,7 +1450,8 @@ export default async function getBaseWebpackConfig( ...@@ -1450,7 +1450,8 @@ export default async function getBaseWebpackConfig(
resourceRegExp: /react-is/, resourceRegExp: /react-is/,
contextRegExp: /next[\\/]dist[\\/]/, contextRegExp: /next[\\/]dist[\\/]/,
}), }),
isServerless && isServer && !webServerRuntime && new ServerlessPlugin(), ((isServerless && isServer) || webServerRuntime) &&
new ServerlessPlugin(),
isServer && isServer &&
!webServerRuntime && !webServerRuntime &&
new PagesManifestPlugin({ serverless: isLikeServerless, dev }), new PagesManifestPlugin({ serverless: isLikeServerless, dev }),
......
...@@ -14,7 +14,11 @@ export const base = curry(function base( ...@@ -14,7 +14,11 @@ export const base = curry(function base(
: 'client' : 'client'
// @ts-ignore TODO webpack 5 typings // @ts-ignore TODO webpack 5 typings
config.target = !ctx.targetWeb ? 'node12.22' : ['web', 'es5'] config.target = !ctx.targetWeb
? 'node12.22'
: ctx.webServerRuntime
? ['web', 'es6']
: ['web', 'es5']
// https://webpack.js.org/configuration/devtool/#development // https://webpack.js.org/configuration/devtool/#development
if (ctx.isDevelopment) { if (ctx.isDevelopment) {
......
...@@ -81,7 +81,7 @@ async function parseExportNamesInto( ...@@ -81,7 +81,7 @@ async function parseExportNamesInto(
loadModule: TransformSourceFunction loadModule: TransformSourceFunction
): Promise<void> { ): Promise<void> {
const { body } = acorn.parse(transformedSource, { const { body } = acorn.parse(transformedSource, {
ecmaVersion: 2019, ecmaVersion: 11,
sourceType: 'module', sourceType: 'module',
}) as any }) as any
for (let i = 0; i < body.length; i++) { for (let i = 0; i < body.length; i++) {
......
...@@ -28,7 +28,7 @@ async function parseImportsInfo( ...@@ -28,7 +28,7 @@ async function parseImportsInfo(
pageExtensions: string[] pageExtensions: string[]
): Promise<string> { ): Promise<string> {
const { body } = acorn.parse(source, { const { body } = acorn.parse(source, {
ecmaVersion: 2019, ecmaVersion: 11,
sourceType: 'module', sourceType: 'module',
}) as any }) as any
......
...@@ -51,7 +51,7 @@ export default async function middlewareRSCLoader(this: any) { ...@@ -51,7 +51,7 @@ export default async function middlewareRSCLoader(this: any) {
const buildManifest = self.__BUILD_MANIFEST const buildManifest = self.__BUILD_MANIFEST
const reactLoadableManifest = self.__REACT_LOADABLE_MANIFEST const reactLoadableManifest = self.__REACT_LOADABLE_MANIFEST
const rscManifest = self._middleware_rsc_manifest const rscManifest = self.__RSC_MANIFEST
if (typeof Page !== 'function') { if (typeof Page !== 'function') {
throw new Error('Your page must export a \`default\` component') throw new Error('Your page must export a \`default\` component')
......
...@@ -120,9 +120,7 @@ export class FlightManifestPlugin { ...@@ -120,9 +120,7 @@ export class FlightManifestPlugin {
}) })
}) })
const output = const output = `self.__RSC_MANIFEST=` + JSON.stringify(json)
`self._middleware_rsc_manifest=(typeof _ENTRIES === "undefined"?{}:_ENTRIES)._middleware_rsc_manifest=` +
JSON.stringify(json)
assets[`server/${MIDDLEWARE_FLIGHT_MANIFEST}.js`] = new sources.RawSource( assets[`server/${MIDDLEWARE_FLIGHT_MANIFEST}.js`] = new sources.RawSource(
output output
) )
......
...@@ -130,13 +130,10 @@ export async function run(params: { ...@@ -130,13 +130,10 @@ export async function run(params: {
const entryPoint = cache.context._ENTRIES[`middleware_${params.name}`] const entryPoint = cache.context._ENTRIES[`middleware_${params.name}`]
if (params.ssr) { if (params.ssr) {
const rscManifest = cache.context._ENTRIES._middleware_rsc_manifest
cache = undefined cache = undefined
if (entryPoint) {
if (rscManifest && entryPoint) {
return entryPoint.default({ return entryPoint.default({
request: params.request, request: params.request,
rscManifest,
}) })
} }
} }
......
import { lazy, Suspense } from 'react'
const Foo = lazy(() => import('../components/foo.client'))
export default function Page() {
return (
<div>
<Suspense fallback="loading...">
<Foo />
</Suspense>
</div>
)
}
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
import cheerio from 'cheerio' import cheerio from 'cheerio'
import { join } from 'path' import { join } from 'path'
import fs from 'fs-extra' import fs from 'fs-extra'
import webdriver from 'next-webdriver'
import { import {
File, File,
...@@ -140,6 +141,12 @@ describe('concurrentFeatures - prod', () => { ...@@ -140,6 +141,12 @@ describe('concurrentFeatures - prod', () => {
expect(content.clientInfo).toContainEqual(item) expect(content.clientInfo).toContainEqual(item)
} }
}) })
it('should support React.lazy and dynamic imports', async () => {
const html = await renderViaHTTP(context.appPort, '/dynamic-imports')
expect(html).toContain('foo.client')
})
runBasicTests(context) runBasicTests(context)
}) })
...@@ -153,6 +160,16 @@ describe('concurrentFeatures - dev', () => { ...@@ -153,6 +160,16 @@ describe('concurrentFeatures - dev', () => {
afterAll(async () => { afterAll(async () => {
await killApp(context.server) await killApp(context.server)
}) })
it('should support React.lazy and dynamic imports', async () => {
const html = await renderViaHTTP(context.appPort, '/dynamic-imports')
expect(html).toContain('loading...')
const browser = await webdriver(context.appPort, '/dynamic-imports')
const content = await browser.eval(`window.document.body.innerText`)
expect(content).toMatchInlineSnapshot('"foo.client"')
})
runBasicTests(context) runBasicTests(context)
}) })
......
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