Repository: vercel/next.js
Pull request: Turbopack: Change eager import to lazy import for loader_tree

## What?

Switch to matching webpack behavior where module in the loader_tree are initialized later.

Closes PACK-5365

Changed files:
filename: crates/next-core/src/base_loader_tree.rs
patch: @@ -99,7 +99,7 @@ impl BaseLoaderTreeBuilder {
         self.imports.push(
             formatdoc!(
                 r#"
-                import * as {} from "MODULE_{}";
+                const {} = () => require("MODULE_{}");
                 "#,
                 identifier,
                 i
@@ -118,7 +118,7 @@ impl BaseLoaderTreeBuilder {
         let module_path = module.ident().path().to_string().await?;
 
         Ok(format!(
-            "[() => {identifier}, {path}]",
+            "[{identifier}, {path}]",
             path = StringifyJs(&module_path),
         ))
     }

filename: packages/next/src/server/next-server.ts
patch: @@ -387,13 +387,6 @@ export default class NextNodeServer extends BaseServer<
         // otherwise if the fetch is patched by user code, we will be patching it
         // too late and there won't be any caching behaviors
         ComponentMod.patchFetch()
-
-        const webpackRequire = ComponentMod.__next_app__.require
-        if (webpackRequire?.m) {
-          for (const id of Object.keys(webpackRequire.m)) {
-            await webpackRequire(id)
-          }
-        }
       } catch (_err) {
         // Intentionally ignored because this is a preload step.
       }

filename: test/development/acceptance-app/hydration-error.test.ts
patch: @@ -228,7 +228,6 @@ describe('Error overlay for hydration errors in App router', () => {
                              <SegmentTrieNode>
                              <script>
                              <script>
-                             <script>
                              <ClientSegmentRoot Component={function Root} slots={{...}} params={{}}>
                                <Root params={Promise}>
                                  <html

filename: test/e2e/app-dir/cache-components-errors/cache-components-errors.test.ts
patch: @@ -2698,7 +2698,6 @@ describe('Cache Components Errors', () => {
                  "stack": [
                    "{module evaluation} app/use-cache-private-in-unstable-cache/page.tsx (21:38)",
                    "<FIXME-next-dist-dir>",
-                   "<FIXME-next-dist-dir>",
                  ],
                }
               `)
@@ -2754,7 +2753,6 @@ describe('Cache Components Errors', () => {
                  "Error: "use cache: private" must not be used within \`unstable_cache()\`.
                      at __TURBOPACK__module__evaluation__ (bundler:///app/use-cache-private-in-unstable-cache/page.tsx:21:38)
                      at a (<next-dist-dir>)
-                     at b (<next-dist-dir>)
                    19 | }
                    20 |
                  > 21 | const getCachedData = unstable_cache(async () => {
@@ -2772,7 +2770,6 @@ describe('Cache Components Errors', () => {
                 expect(output).toMatchInlineSnapshot(`
                  "Error: "use cache: private" must not be used within \`unstable_cache()\`.
                      at __TURBOPACK__module__evaluation__ (bundler:///app/use-cache-private-in-unstable-cache/page.tsx:21:38)
-                     at a (<next-dist-dir>)
                    19 | }
                    20 |
                  > 21 | const getCachedData = unstable_cache(async () => {
@@ -2841,7 +2838,6 @@ describe('Cache Components Errors', () => {
                  "stack": [
                    "{module evaluation} app/use-cache-private-in-use-cache/page.tsx (15:1)",
                    "<FIXME-next-dist-dir>",
-                   "<FIXME-next-dist-dir>",
                  ],
                }
               `)
@@ -2898,7 +2894,6 @@ describe('Cache Components Errors', () => {
                  "Error: "use cache: private" must not be used within "use cache". It can only be nested inside of another "use cache: private".
                      at __TURBOPACK__module__evaluation__ (bundler:///app/use-cache-private-in-use-cache/page.tsx:15:1)
                      at a (<next-dist-dir>)
-                     at b (<next-dist-dir>)
                    13 | }
                    14 |
                  > 15 | async function Private() {
@@ -2908,8 +2903,7 @@ describe('Cache Components Errors', () => {
                    18 |   return <p>Private</p>
                  Error: "use cache: private" must not be used within "use cache". It can only be nested inside of another "use cache: private".
                      at __TURBOPACK__module__evaluation__ (bundler:///app/use-cache-private-in-use-cache/page.tsx:15:1)
-                     at c (<next-dist-dir>)
-                     at d (<next-dist-dir>)
+                     at b (<next-dist-dir>)
                    13 | }
                    14 |
                  > 15 | async function Private() {
@@ -2927,7 +2921,6 @@ describe('Cache Components Errors', () => {
                 expect(output).toMatchInlineSnapshot(`
                  "Error: "use cache: private" must not be used within "use cache". It can only be nested inside of another "use cache: private".
                      at __TURBOPACK__module__evaluation__ (bundler:///app/use-cache-private-in-use-cache/page.tsx:15:1)
-                     at a (<next-dist-dir>)
                    13 | }
                    14 |
                  > 15 | async function Private() {
@@ -2937,7 +2930,6 @@ describe('Cache Components Errors', () => {
                    18 |   return <p>Private</p>
                  Error: "use cache: private" must not be used within "use cache". It can only be nested inside of another "use cache: private".
                      at __TURBOPACK__module__evaluation__ (bundler:///app/use-cache-private-in-use-cache/page.tsx:15:1)
-                     at b (<next-dist-dir>)
                    13 | }
                    14 |
                  > 15 | async function Private() {

filename: test/e2e/app-dir/server-source-maps/server-source-maps.test.ts
patch: @@ -484,7 +484,8 @@ describe('app-dir - server source maps', () => {
              "{module evaluation} app/module-evaluation/module.js (1:22)",
              "{module evaluation} app/module-evaluation/page.js (1:1)",
              "{module evaluation} app/module-evaluation/page.js (6:1)",
-             "<FIXME-next-dist-dir>",
+             "Array.map <anonymous>",
+             "Function.all <anonymous>",
              "Page <anonymous>",
            ],
          }
@@ -515,9 +516,8 @@ describe('app-dir - server source maps', () => {
         expect(normalizeCliOutput(next.cliOutput)).toContain(
           '' +
             '\nError: module-evaluation' +
-            '\n    at __TURBOPACK__module__evaluation__ (bundler:///app/module-evaluation/module.js:1:22)' +
             // TODO(veil): Turbopack internals. Feel free to update. Tracked in https://linear.app/vercel/issue/NEXT-4362
-            '\n    at Object.<anonymous>'
+            '\n    at __TURBOPACK__module__evaluation__ (bundler:///app/module-evaluation/module.js:1:22)'
         )
         expect(normalizeCliOutput(next.cliOutput)).toContain(
           '' +