Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(turbopack): support basic next/dynamic (#56389)
Closes WEB-1702 This PR implements initial support for the `next/dynamic` in Turbopack, more specifically resolving some hydration errors and other components boot up cases. Previously, turbopack had partial next/dynamic support via its own mode (https://github.com/vercel/next.js/pull/56389/files#diff-e1af4f79cb88a73f819a25443d15ed4b1ffabcbb879256caa59b751fad46d7c4L68), which does a transform against `next/dynamic` wrapped import to embed dynamically resolvable chunk ids like (https://github.com/vercel/next.js/blob/ad42b610c25b72561ad367b82b1c7383fd2a5dd2/packages/next-swc/crates/next-transform-dynamic/tests/fixture/wrapped-import/output-turbo-dev-server.js). However, since next.js relies on static path to the chunks to the dynamic import and passing those ids in between client-server to ensure component load (and avoid hydration errors), it doesn't work out of the box. This PR changes turbopack's behavior to closely mimic what current next.js's webpack plugin does, by 1. Traverse the module graph, find out `dynamic(import())` 2. Generate chunks for those imports, creates a partial LoadableManifest per each imports 3. Merge partial manifest into a single `react-loadable-manifest.json` 4. For the id, use static (Webpack mode) instead of dynamic so we can embed it in `react-loadable-manifest` as well as next.js can use it to pass it between server-client context. I left a small comment to the implementation (https://github.com/vercel/next.js/pull/56389/files#diff-bf12ed2c69d0bc89a06884779da4ae44967eb8becada031dea12bedef28e2622R155) for the lifecycle of this feature in case to fix further. This makes to pass most of the basic next-dynamic related integration tests, except if the import have webpack specific features like https://github.com/vercel/next.js/blob/ad42b610c25b72561ad367b82b1c7383fd2a5dd2/test/development/basic/next-dynamic/pages/dynamic/multiple-modules.js#L5. --------- Co-authored-by: Tim Neutkens <tim@timneutkens.nl>
- Loading branch information