File size: 3,118 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
import curry from 'next/dist/compiled/lodash.curry'
import type { webpack } from 'next/dist/compiled/webpack/webpack'
import { COMPILER_NAMES } from '../../../../shared/lib/constants'
import type { ConfigurationContext } from '../utils'
import DevToolsIgnorePlugin from '../../plugins/devtools-ignore-list-plugin'
import EvalSourceMapDevToolPlugin from '../../plugins/eval-source-map-dev-tool-plugin'
import { getRspackCore } from '../../../../shared/lib/get-rspack'

function shouldIgnorePath(modulePath: string): boolean {
  return (
    modulePath.includes('node_modules') ||
    modulePath.endsWith('__nextjs-internal-proxy.cjs') ||
    modulePath.endsWith('__nextjs-internal-proxy.mjs') ||
    // Only relevant for when Next.js is symlinked e.g. in the Next.js monorepo
    modulePath.includes('next/dist')
  )
}

export const base = curry(function base(
  ctx: ConfigurationContext,
  config: webpack.Configuration
) {
  config.mode = ctx.isDevelopment ? 'development' : 'production'
  config.name = ctx.isServer
    ? ctx.isEdgeRuntime
      ? COMPILER_NAMES.edgeServer
      : COMPILER_NAMES.server
    : COMPILER_NAMES.client

  config.target = !ctx.targetWeb
    ? 'node18.17' // Same version defined in packages/next/package.json#engines
    : ctx.isEdgeRuntime
      ? ['web', 'es6']
      : ['web', 'es6']

  // https://webpack.js.org/configuration/devtool/#development
  if (ctx.isDevelopment) {
    if (process.env.__NEXT_TEST_MODE && !process.env.__NEXT_TEST_WITH_DEVTOOL) {
      config.devtool = false
    } else {
      // `eval-source-map` provides full-fidelity source maps for the
      // original source, including columns and original variable names.
      // This is desirable so the in-browser debugger can correctly pause
      // and show scoped variables with their original names.
      config.devtool = 'eval-source-map'
    }
  } else {
    if (
      ctx.isEdgeRuntime ||
      (ctx.isServer && ctx.serverSourceMaps) ||
      // Enable browser sourcemaps:
      (ctx.productionBrowserSourceMaps && ctx.isClient)
    ) {
      config.devtool = 'source-map'
    } else {
      config.devtool = false
    }
  }

  if (!config.module) {
    config.module = { rules: [] }
  }

  config.plugins ??= []
  if (config.devtool === 'source-map' && !process.env.NEXT_RSPACK) {
    config.plugins.push(
      new DevToolsIgnorePlugin({
        shouldIgnorePath,
      })
    )
  } else if (config.devtool === 'eval-source-map') {
    // We're using a fork of `eval-source-map`
    config.devtool = false
    if (process.env.NEXT_RSPACK) {
      config.plugins.push(
        new (getRspackCore().EvalSourceMapDevToolPlugin)({
          moduleFilenameTemplate: config.output?.devtoolModuleFilenameTemplate,
        })
      )
    } else {
      config.plugins.push(
        new EvalSourceMapDevToolPlugin({
          moduleFilenameTemplate: config.output?.devtoolModuleFilenameTemplate,
          shouldIgnorePath,
        })
      )
    }
  }

  // TODO: add codemod for "Should not import the named export" with JSON files
  // config.module.strictExportPresence = !isWebpack5

  return config
})