File size: 27,098 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
import path from 'path'
import * as React from 'react'
import {
  DOT_NEXT_ALIAS,
  PAGES_DIR_ALIAS,
  ROOT_DIR_ALIAS,
  APP_DIR_ALIAS,
  RSC_ACTION_PROXY_ALIAS,
  RSC_ACTION_CLIENT_WRAPPER_ALIAS,
  RSC_ACTION_VALIDATE_ALIAS,
  RSC_ACTION_ENCRYPTION_ALIAS,
  RSC_CACHE_WRAPPER_ALIAS,
  type WebpackLayerName,
  RSC_DYNAMIC_IMPORT_WRAPPER_ALIAS,
} from '../lib/constants'
import type { NextConfigComplete } from '../server/config-shared'
import { defaultOverrides } from '../server/require-hook'
import { hasExternalOtelApiPackage } from './webpack-config'
import { NEXT_PROJECT_ROOT } from './next-dir-paths'
import { shouldUseReactServerCondition } from './utils'

interface CompilerAliases {
  [alias: string]: string | string[]
}

const isReact19 = typeof React.use === 'function'

export function createWebpackAliases({
  distDir,
  isClient,
  isEdgeServer,
  dev,
  config,
  pagesDir,
  appDir,
  dir,
  reactProductionProfiling,
}: {
  distDir: string
  isClient: boolean
  isEdgeServer: boolean
  dev: boolean
  config: NextConfigComplete
  pagesDir: string | undefined
  appDir: string | undefined
  dir: string
  reactProductionProfiling: boolean
}): CompilerAliases {
  const pageExtensions = config.pageExtensions
  const customAppAliases: CompilerAliases = {}
  const customDocumentAliases: CompilerAliases = {}

  // tell webpack where to look for _app and _document
  // using aliases to allow falling back to the default
  // version when removed or not present
  if (dev) {
    const nextDistPath = 'next/dist/' + (isEdgeServer ? 'esm/' : '')
    customAppAliases[`${PAGES_DIR_ALIAS}/_app`] = [
      ...(pagesDir
        ? pageExtensions.reduce((prev, ext) => {
            prev.push(path.join(pagesDir, `_app.${ext}`))
            return prev
          }, [] as string[])
        : []),
      `${nextDistPath}pages/_app.js`,
    ]
    customAppAliases[`${PAGES_DIR_ALIAS}/_error`] = [
      ...(pagesDir
        ? pageExtensions.reduce((prev, ext) => {
            prev.push(path.join(pagesDir, `_error.${ext}`))
            return prev
          }, [] as string[])
        : []),
      `${nextDistPath}pages/_error.js`,
    ]
    customDocumentAliases[`${PAGES_DIR_ALIAS}/_document`] = [
      ...(pagesDir
        ? pageExtensions.reduce((prev, ext) => {
            prev.push(path.join(pagesDir, `_document.${ext}`))
            return prev
          }, [] as string[])
        : []),
      `${nextDistPath}pages/_document.js`,
    ]
  }

  return {
    '@vercel/og$': 'next/dist/server/og/image-response',

    // Avoid bundling both entrypoints in React 19 when we just need one.
    // Also avoids bundler warnings in React 18 where react-dom/server.edge doesn't exist.
    'next/dist/server/ReactDOMServerPages': isReact19
      ? 'react-dom/server.edge'
      : 'react-dom/server.browser',

    // Alias next/dist imports to next/dist/esm assets,
    // let this alias hit before `next` alias.
    ...(isEdgeServer
      ? {
          'next/dist/api': 'next/dist/esm/api',
          'next/dist/build': 'next/dist/esm/build',
          'next/dist/client': 'next/dist/esm/client',
          'next/dist/shared': 'next/dist/esm/shared',
          'next/dist/pages': 'next/dist/esm/pages',
          'next/dist/lib': 'next/dist/esm/lib',
          'next/dist/server': 'next/dist/esm/server',

          ...createNextApiEsmAliases(),
        }
      : undefined),

    // For RSC server bundle
    ...(!hasExternalOtelApiPackage() && {
      '@opentelemetry/api': 'next/dist/compiled/@opentelemetry/api',
    }),

    ...(config.images.loaderFile
      ? {
          'next/dist/shared/lib/image-loader': config.images.loaderFile,
          ...(isEdgeServer && {
            'next/dist/esm/shared/lib/image-loader': config.images.loaderFile,
          }),
        }
      : undefined),

    'styled-jsx/style$': defaultOverrides['styled-jsx/style'],
    'styled-jsx$': defaultOverrides['styled-jsx'],

    ...customAppAliases,
    ...customDocumentAliases,

    ...(pagesDir ? { [PAGES_DIR_ALIAS]: pagesDir } : {}),
    ...(appDir ? { [APP_DIR_ALIAS]: appDir } : {}),
    [ROOT_DIR_ALIAS]: dir,
    ...(isClient
      ? {
          'private-next-instrumentation-client': [
            path.join(dir, 'src', 'instrumentation-client'),
            path.join(dir, 'instrumentation-client'),
            'private-next-empty-module',
          ],

          // disable typechecker, webpack5 allows aliases to be set to false to create a no-op module
          'private-next-empty-module': false as any,
        }
      : {}),

    [DOT_NEXT_ALIAS]: distDir,
    ...(isClient || isEdgeServer ? getOptimizedModuleAliases() : {}),
    ...(reactProductionProfiling ? getReactProfilingInProduction() : {}),

    [RSC_ACTION_VALIDATE_ALIAS]:
      'next/dist/build/webpack/loaders/next-flight-loader/action-validate',

    [RSC_ACTION_CLIENT_WRAPPER_ALIAS]:
      'next/dist/build/webpack/loaders/next-flight-loader/action-client-wrapper',

    [RSC_ACTION_PROXY_ALIAS]:
      'next/dist/build/webpack/loaders/next-flight-loader/server-reference',

    [RSC_ACTION_ENCRYPTION_ALIAS]: 'next/dist/server/app-render/encryption',

    [RSC_CACHE_WRAPPER_ALIAS]:
      'next/dist/build/webpack/loaders/next-flight-loader/cache-wrapper',
    [RSC_DYNAMIC_IMPORT_WRAPPER_ALIAS]:
      'next/dist/build/webpack/loaders/next-flight-loader/track-dynamic-import',

    '@swc/helpers/_': path.join(
      path.dirname(require.resolve('@swc/helpers/package.json')),
      '_'
    ),

    setimmediate: 'next/dist/compiled/setimmediate',
  }
}

export function createServerOnlyClientOnlyAliases(
  isServer: boolean
): CompilerAliases {
  return isServer
    ? {
        'server-only$': 'next/dist/compiled/server-only/empty',
        'client-only$': 'next/dist/compiled/client-only/error',
        'next/dist/compiled/server-only$':
          'next/dist/compiled/server-only/empty',
        'next/dist/compiled/client-only$':
          'next/dist/compiled/client-only/error',
      }
    : {
        'server-only$': 'next/dist/compiled/server-only/index',
        'client-only$': 'next/dist/compiled/client-only/index',
        'next/dist/compiled/client-only$':
          'next/dist/compiled/client-only/index',
        'next/dist/compiled/server-only':
          'next/dist/compiled/server-only/index',
      }
}

export function createNextApiEsmAliases() {
  const mapping = {
    head: 'next/dist/api/head',
    image: 'next/dist/api/image',
    constants: 'next/dist/api/constants',
    router: 'next/dist/api/router',
    dynamic: 'next/dist/api/dynamic',
    script: 'next/dist/api/script',
    link: 'next/dist/api/link',
    form: 'next/dist/api/form',
    navigation: 'next/dist/api/navigation',
    headers: 'next/dist/api/headers',
    og: 'next/dist/api/og',
    server: 'next/dist/api/server',
    // pages api
    document: 'next/dist/api/document',
    app: 'next/dist/api/app',
  }
  const aliasMap: Record<string, string> = {}
  // Handle fully specified imports like `next/image.js`
  for (const [key, value] of Object.entries(mapping)) {
    const nextApiFilePath = path.join(NEXT_PROJECT_ROOT, key)
    aliasMap[nextApiFilePath + '.js'] = value
  }

  return aliasMap
}

export function createAppRouterApiAliases(isServerOnlyLayer: boolean) {
  const mapping: Record<string, string> = {
    head: 'next/dist/client/components/noop-head',
    dynamic: 'next/dist/api/app-dynamic',
    link: 'next/dist/client/app-dir/link',
    form: 'next/dist/client/app-dir/form',
  }

  if (isServerOnlyLayer) {
    mapping['navigation'] = 'next/dist/api/navigation.react-server'
  }

  const aliasMap: Record<string, string> = {}
  for (const [key, value] of Object.entries(mapping)) {
    const nextApiFilePath = path.join(NEXT_PROJECT_ROOT, key)
    aliasMap[nextApiFilePath + '.js'] = value
  }
  return aliasMap
}

// file:///./../compiled/react/package.json
type ReactEntrypoint = 'jsx-runtime' | 'jsx-dev-runtime' | 'compiler-runtime'
// file:///./../compiled/react-dom/package.json
type ReactDOMEntrypoint =
  | 'client'
  | 'server'
  | 'server.edge'
  | 'server.browser'
  // TODO: server.node
  | 'static'
  | 'static.browser'
  | 'static.edge'
// TODO: static.node

// file:///./../compiled/react-server-dom-webpack/package.json
type ReactServerDOMWebpackEntrypoint =
  | 'client'
  // TODO: client.browser
  // TODO: client.edge
  // TODO: client.node
  | 'server'
  // TODO: server.browser
  // TODO: server.edge
  | 'server.node'
  | 'static'
// TODO: static.browser
// TODO: static.edge
// TODO: static.node

type ReactPackagesEntryPoint =
  | 'react'
  | `react/${ReactEntrypoint}`
  | 'react-dom'
  | `react-dom/${ReactDOMEntrypoint}`
  | `react-server-dom-webpack/${ReactServerDOMWebpackEntrypoint}`

type BundledReactChannel = '' | '-experimental'

type ReactAliases = {
  [K in `${ReactPackagesEntryPoint}$`]: string
} & {
  // Edge Runtime does not use next-server runtime.
  // This means we rely on rewritten import sources in compiled React.
  // We need to alias those rewritten import sources.
  [K in
    | `next/dist/compiled/react${BundledReactChannel}$`
    | `next/dist/compiled/react${BundledReactChannel}/${ReactEntrypoint}$`
    | `next/dist/compiled/react-dom${BundledReactChannel}$`]?: string
}

export function createVendoredReactAliases(
  bundledReactChannel: BundledReactChannel,
  {
    layer,
    isBrowser,
    isEdgeServer,
    reactProductionProfiling,
  }: {
    layer: WebpackLayerName
    isBrowser: boolean
    isEdgeServer: boolean
    reactProductionProfiling: boolean
  }
): CompilerAliases {
  const environmentCondition = isBrowser
    ? 'browser'
    : isEdgeServer
      ? 'edge'
      : 'nodejs'
  const reactCondition = shouldUseReactServerCondition(layer)
    ? 'server'
    : 'client'

  // βœ… Correct alias
  // ❌ Incorrect alias i.e. importing this entrypoint should throw an error.
  // ❔ Alias that may produce correct code in certain conditions.Keep until react-markup is available.

  let reactAlias: ReactAliases
  if (environmentCondition === 'browser' && reactCondition === 'client') {
    // prettier-ignore
    reactAlias = {
      // file:///./../compiled/react/package.json
      react$:                                  /* βœ… */ `next/dist/compiled/react${bundledReactChannel}`,
      'react/compiler-runtime$':               /* βœ… */ `next/dist/compiled/react${bundledReactChannel}/compiler-runtime`,
      'react/jsx-dev-runtime$':                /* βœ… */ `next/dist/compiled/react${bundledReactChannel}/jsx-dev-runtime`,
      'react/jsx-runtime$':                    /* βœ… */ `next/dist/compiled/react${bundledReactChannel}/jsx-runtime`,
      // file:///./../compiled/react-dom/package.json
      'react-dom$':                            /* βœ… */ `next/dist/compiled/react-dom${bundledReactChannel}`,
      'react-dom/client$':                     /* βœ… */ `next/dist/compiled/react-dom${bundledReactChannel}/client`,
      'react-dom/server$':                     /* βœ… */ `next/dist/compiled/react-dom${bundledReactChannel}/server.browser`,
      'react-dom/server.browser$':             /* βœ… */ `next/dist/compiled/react-dom${bundledReactChannel}/server.browser`,
      // optimizations to ignore the legacy build of react-dom/server in `server.edge` build
      'react-dom/server.edge$':                /* ❌ */ `next/dist/build/webpack/alias/react-dom-server${bundledReactChannel}.js`,
      'react-dom/static$':                     /* βœ… */ `next/dist/compiled/react-dom${bundledReactChannel}/static.browser`,
      'react-dom/static.browser$':             /* βœ… */ `next/dist/compiled/react-dom${bundledReactChannel}/static.browser`,
      'react-dom/static.edge$':                /* βœ… */ `next/dist/compiled/react-dom${bundledReactChannel}/static.edge`,
      // file:///./../compiled/react-server-dom-webpack/package.json
      'react-server-dom-webpack/client$':      /* βœ… */ `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/client.browser`,
      'react-server-dom-webpack/server$':      /* ❌ */ `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/server.browser`,
      'react-server-dom-webpack/server.node$': /* ❌ */ `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/server.node`,
      'react-server-dom-webpack/static$':      /* ❌ */ `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/static.browser`,
    }
  } else if (
    environmentCondition === 'browser' &&
    reactCondition === 'server'
  ) {
    // prettier-ignore
    reactAlias = {
      // file:///./../compiled/react/package.json
      react$:                                  /* ❌ */ `next/dist/compiled/react${bundledReactChannel}`,
      'react/compiler-runtime$':               /* ❌ */ `next/dist/compiled/react${bundledReactChannel}/compiler-runtime`,
      'react/jsx-dev-runtime$':                /* ❌ */ `next/dist/compiled/react${bundledReactChannel}/jsx-dev-runtime`,
      'react/jsx-runtime$':                    /* ❌ */ `next/dist/compiled/react${bundledReactChannel}/jsx-runtime`,
      // file:///./../compiled/react-dom/package.json
      'react-dom$':                            /* ❌ */ `next/dist/compiled/react-dom${bundledReactChannel}`,
      'react-dom/client$':                     /* ❌ */ `next/dist/compiled/react-dom${bundledReactChannel}/client`,
      'react-dom/server$':                     /* ❌ */ `next/dist/compiled/react-dom${bundledReactChannel}/server.browser`,
      'react-dom/server.browser$':             /* ❌ */ `next/dist/compiled/react-dom${bundledReactChannel}/server.browser`,
      // optimizations to ignore the legacy build of react-dom/server in `server.edge` build
      'react-dom/server.edge$':                /* ❌ */ `next/dist/build/webpack/alias/react-dom-server${bundledReactChannel}.js`,
      'react-dom/static$':                     /* ❌ */ `next/dist/compiled/react-dom${bundledReactChannel}/static.browser`,
      'react-dom/static.browser$':             /* ❌ */ `next/dist/compiled/react-dom${bundledReactChannel}/static.browser`,
      'react-dom/static.edge$':                /* ❌ */ `next/dist/compiled/react-dom${bundledReactChannel}/static.edge`,
      // file:///./../compiled/react-server-dom-webpack/package.json
      'react-server-dom-webpack/client$':      /* βœ… */ `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/client.browser`,
      'react-server-dom-webpack/server$':      /* βœ… */ `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/server.browser`,
      'react-server-dom-webpack/server.node$': /* ❌ */ `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/server.node`,
      'react-server-dom-webpack/static$':      /* βœ… */ `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/static.browser`,
    }
  } else if (environmentCondition === 'nodejs' && reactCondition === 'client') {
    // prettier-ignore
    reactAlias = {
      // file:///./../compiled/react/package.json
      react$:                                 /* βœ… */ `next/dist/server/route-modules/app-page/vendored/ssr/react`,
      'react/compiler-runtime$':              /* βœ… */ `next/dist/server/route-modules/app-page/vendored/ssr/react-compiler-runtime`,
      'react/jsx-dev-runtime$':               /* βœ… */ `next/dist/server/route-modules/app-page/vendored/ssr/react-jsx-dev-runtime`,
      'react/jsx-runtime$':                   /* βœ… */ `next/dist/server/route-modules/app-page/vendored/ssr/react-jsx-runtime`,
      // file:///./../compiled/react-dom/package.json
      'react-dom$':                           /* βœ… */ `next/dist/server/route-modules/app-page/vendored/ssr/react-dom`,
      'react-dom/client$':                    /* ❔ */ `next/dist/compiled/react-dom${bundledReactChannel}/client`,
      'react-dom/server$':                    /* ❔ */ `next/dist/compiled/react-dom${bundledReactChannel}/server.node`,
      'react-dom/server.browser$':            /* ❔ */ `next/dist/compiled/react-dom${bundledReactChannel}/server.browser`,
      // optimizations to ignore the legacy build of react-dom/server in `server.edge` build
      'react-dom/server.edge$':               /* βœ… */ `next/dist/build/webpack/alias/react-dom-server${bundledReactChannel}.js`,
      'react-dom/static$':                    /* ❔ */ `next/dist/compiled/react-dom${bundledReactChannel}/static.node`,
      'react-dom/static.browser$':            /* ❔ */ `next/dist/compiled/react-dom${bundledReactChannel}/static.browser`,
      'react-dom/static.edge$':               /* ❔ */ `next/dist/compiled/react-dom${bundledReactChannel}/static.edge`,
      // file:///./../compiled/react-server-dom-webpack/package.json
      'react-server-dom-webpack/client$':     /* βœ… */ `next/dist/server/route-modules/app-page/vendored/ssr/react-server-dom-webpack-client`,
      'react-server-dom-webpack/server$':     /* ❌ */ `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/server.node`,
      'react-server-dom-webpack/server.node$':/* ❌ */ `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/server.node`,
      'react-server-dom-webpack/static$':     /* ❌ */ `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/static.node`,
    }
  } else if (environmentCondition === 'nodejs' && reactCondition === 'server') {
    // prettier-ignore
    reactAlias = {
      // file:///./../compiled/react/package.json
      react$:                                  /* βœ… */ `next/dist/server/route-modules/app-page/vendored/rsc/react`,
      'react/compiler-runtime$':               /* βœ… */ `next/dist/server/route-modules/app-page/vendored/rsc/react-compiler-runtime`,
      'react/jsx-dev-runtime$':                /* βœ… */ `next/dist/server/route-modules/app-page/vendored/rsc/react-jsx-dev-runtime`,
      'react/jsx-runtime$':                    /* βœ… */ `next/dist/server/route-modules/app-page/vendored/rsc/react-jsx-runtime`,
      // file:///./../compiled/react-dom/package.json
      'react-dom$':                            /* βœ… */ `next/dist/server/route-modules/app-page/vendored/rsc/react-dom`,
      'react-dom/client$':                     /* ❌ */ `next/dist/compiled/react-dom${bundledReactChannel}/client`,
      'react-dom/server$':                     /* ❌ */ `next/dist/compiled/react-dom${bundledReactChannel}/server.node`,
      'react-dom/server.browser$':             /* ❌ */ `next/dist/compiled/react-dom${bundledReactChannel}/server.browser`,
      // optimizations to ignore the legacy build of react-dom/server in `server.edge` build
      'react-dom/server.edge$':                /* ❌ */ `next/dist/build/webpack/alias/react-dom-server${bundledReactChannel}.js`,
      'react-dom/static$':                     /* ❌ */ `next/dist/compiled/react-dom${bundledReactChannel}/static.node`,
      'react-dom/static.browser$':             /* ❌ */ `next/dist/compiled/react-dom${bundledReactChannel}/static.browser`,
      'react-dom/static.edge$':                /* ❌ */ `next/dist/compiled/react-dom${bundledReactChannel}/static.edge`,
      // file:///./../compiled/react-server-dom-webpack/package.json
      'react-server-dom-webpack/client$':      /* ❔ */ `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/client.node`,
      'react-server-dom-webpack/server$':      /* βœ… */ `next/dist/server/route-modules/app-page/vendored/rsc/react-server-dom-webpack-server`,
      'react-server-dom-webpack/server.node$': /* βœ… */ `next/dist/server/route-modules/app-page/vendored/rsc/react-server-dom-webpack-server`,
      'react-server-dom-webpack/static$':      /* βœ… */ `next/dist/server/route-modules/app-page/vendored/rsc/react-server-dom-webpack-static`,
    }
  } else if (environmentCondition === 'edge' && reactCondition === 'client') {
    // prettier-ignore
    reactAlias = {
      // file:///./../compiled/react/package.json
      react$:                                  /* βœ… */ `next/dist/compiled/react${bundledReactChannel}`,
      'react/compiler-runtime$':               /* βœ… */ `next/dist/compiled/react${bundledReactChannel}/compiler-runtime`,
      'react/jsx-dev-runtime$':                /* βœ… */ `next/dist/compiled/react${bundledReactChannel}/jsx-dev-runtime`,
      'react/jsx-runtime$':                    /* βœ… */ `next/dist/compiled/react${bundledReactChannel}/jsx-runtime`,
      // file:///./../compiled/react-dom/package.json
      'react-dom$':                            /* βœ… */ `next/dist/compiled/react-dom${bundledReactChannel}`,
      'react-dom/client$':                     /* βœ… */ `next/dist/compiled/react-dom${bundledReactChannel}/client`,
      'react-dom/server$':                     /* βœ… */ `next/dist/build/webpack/alias/react-dom-server${bundledReactChannel}.js`,
      'react-dom/server.browser$':             /* βœ… */ `next/dist/compiled/react-dom${bundledReactChannel}/server.browser`,
      // optimizations to ignore the legacy build of react-dom/server in `server.edge` build
      'react-dom/server.edge$':                /* βœ… */ `next/dist/build/webpack/alias/react-dom-server${bundledReactChannel}.js`,
      'react-dom/static$':                     /* βœ… */ `next/dist/compiled/react-dom${bundledReactChannel}/static.edge`,
      'react-dom/static.browser$':             /* βœ… */ `next/dist/compiled/react-dom${bundledReactChannel}/static.browser`,
      'react-dom/static.edge$':                /* βœ… */ `next/dist/compiled/react-dom${bundledReactChannel}/static.edge`,
      // file:///./../compiled/react-server-dom-webpack/package.json
      'react-server-dom-webpack/client$':      /* βœ… */ `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/client.edge`,
      'react-server-dom-webpack/server$':      /* ❌ */ `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/server.edge`,
      'react-server-dom-webpack/server.node$': /* ❌ */ `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/server.node`,
      'react-server-dom-webpack/static$':      /* ❌ */ `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/static.edge`,
    }
  } else if (environmentCondition === 'edge' && reactCondition === 'server') {
    // prettier-ignore
    reactAlias = {
      // file:///./../compiled/react/package.json
      react$:                                  /* βœ… */ `next/dist/compiled/react${bundledReactChannel}/react.react-server`,
      'react/compiler-runtime$':               /* ❌ */ `next/dist/compiled/react${bundledReactChannel}/compiler-runtime`,
      'react/jsx-dev-runtime$':                /* βœ… */ `next/dist/compiled/react${bundledReactChannel}/jsx-dev-runtime.react-server`,
      'react/jsx-runtime$':                    /* βœ… */ `next/dist/compiled/react${bundledReactChannel}/jsx-runtime.react-server`,
      // file:///./../compiled/react-dom/package.json
      'react-dom$':                            /* βœ… */ `next/dist/compiled/react-dom${bundledReactChannel}/react-dom.react-server`,
      'react-dom/client$':                     /* ❌ */ `next/dist/compiled/react-dom${bundledReactChannel}/client`,
      'react-dom/server$':                     /* ❌ */ `next/dist/build/webpack/alias/react-dom-server${bundledReactChannel}.js`,
      'react-dom/server.browser$':             /* ❌ */ `next/dist/compiled/react-dom${bundledReactChannel}/server.browser`,
      // optimizations to ignore the legacy build of react-dom/server in `server.edge` build
      'react-dom/server.edge$':                /* ❌ */ `next/dist/build/webpack/alias/react-dom-server${bundledReactChannel}.js`,
      'react-dom/static$':                     /* ❌ */ `next/dist/compiled/react-dom${bundledReactChannel}/static.edge`,
      'react-dom/static.browser$':             /* ❌ */ `next/dist/compiled/react-dom${bundledReactChannel}/static.browser`,
      'react-dom/static.edge$':                /* ❌ */ `next/dist/compiled/react-dom${bundledReactChannel}/static.edge`,
      // file:///./../compiled/react-server-dom-webpack/package.json
      'react-server-dom-webpack/client$':      /* ❔ */ `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/client.edge`,
      'react-server-dom-webpack/server$':      /* βœ… */ `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/server.edge`,
      'react-server-dom-webpack/server.node$': /* βœ… */ `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/server.node`,
      'react-server-dom-webpack/static$':      /* βœ… */ `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/static.edge`,
    }

    // prettier-ignore
    reactAlias[`next/dist/compiled/react${bundledReactChannel}$`                 ] = reactAlias[`react$`]
    // prettier-ignore
    reactAlias[`next/dist/compiled/react${bundledReactChannel}/compiler-runtime$`] = reactAlias[`react/compiler-runtime$`]
    // prettier-ignore
    reactAlias[`next/dist/compiled/react${bundledReactChannel}/jsx-dev-runtime$` ] = reactAlias[`react/jsx-dev-runtime$`]
    // prettier-ignore
    reactAlias[`next/dist/compiled/react${bundledReactChannel}/jsx-runtime$`     ] = reactAlias[`react/jsx-runtime$`]
    // prettier-ignore
    reactAlias[`next/dist/compiled/react-dom${bundledReactChannel}$`             ] = reactAlias[`react-dom$`]
  } else {
    throw new Error(
      `Unsupported environment condition "${environmentCondition}" and react condition "${reactCondition}". This is a bug in Next.js.`
    )
  }

  if (reactProductionProfiling) {
    reactAlias['react-dom/client$'] =
      `next/dist/compiled/react-dom${bundledReactChannel}/profiling`
  }

  const alias: CompilerAliases = reactAlias

  alias[
    '@vercel/turbopack-ecmascript-runtime/browser/dev/hmr-client/hmr-client.ts'
  ] = `next/dist/client/dev/noop-turbopack-hmr`

  return alias
}

// Insert aliases for Next.js stubs of fetch, object-assign, and url
// Keep in sync with insert_optimized_module_aliases in import_map.rs
export function getOptimizedModuleAliases(): CompilerAliases {
  return {
    unfetch: require.resolve('next/dist/build/polyfills/fetch/index.js'),
    'isomorphic-unfetch': require.resolve(
      'next/dist/build/polyfills/fetch/index.js'
    ),
    'whatwg-fetch': require.resolve(
      'next/dist/build/polyfills/fetch/whatwg-fetch.js'
    ),
    'object-assign': require.resolve(
      'next/dist/build/polyfills/object-assign.js'
    ),
    'object.assign/auto': require.resolve(
      'next/dist/build/polyfills/object.assign/auto.js'
    ),
    'object.assign/implementation': require.resolve(
      'next/dist/build/polyfills/object.assign/implementation.js'
    ),
    'object.assign/polyfill': require.resolve(
      'next/dist/build/polyfills/object.assign/polyfill.js'
    ),
    'object.assign/shim': require.resolve(
      'next/dist/build/polyfills/object.assign/shim.js'
    ),
    url: require.resolve('next/dist/compiled/native-url'),
  }
}

function getReactProfilingInProduction(): CompilerAliases {
  return {
    'react-dom/client$': 'react-dom/profiling',
  }
}