-
Notifications
You must be signed in to change notification settings - Fork 27.2k
/
webpack.config.js
146 lines (141 loc) · 4.28 KB
/
webpack.config.js
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
const webpack = require('webpack')
const path = require('path')
const TerserPlugin = require('terser-webpack-plugin')
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer')
const minimalExternals = [
'react',
'react/package.json',
'react/jsx-runtime',
'react/jsx-dev-runtime',
'react-dom',
'react-dom/package.json',
'react-dom/client',
'react-dom/server',
'react-dom/server.browser',
'react-dom/server.edge',
'react-server-dom-webpack/client',
'react-server-dom-webpack/client.edge',
'react-server-dom-webpack/server.edge',
'react-server-dom-webpack/server.node',
'styled-jsx',
'styled-jsx/style',
'@opentelemetry/api',
'next/dist/compiled/@next/react-dev-overlay/dist/middleware',
'next/dist/compiled/@ampproject/toolbox-optimizer',
'next/dist/compiled/edge-runtime',
'next/dist/compiled/@edge-runtime/ponyfill',
'next/dist/compiled/undici',
'next/dist/compiled/raw-body',
'next/dist/server/capsize-font-metrics.json',
'critters',
'next/dist/compiled/node-html-parser',
'next/dist/compiled/compression',
'next/dist/compiled/jsonwebtoken',
'next/dist/compiled/@opentelemetry/api',
'next/dist/compiled/@mswjs/interceptors/ClientRequest',
]
const externalsMap = {
'./web/sandbox': 'next/dist/server/web/sandbox',
}
const externalsRegexMap = {
'(.*)trace/tracer$': 'next/dist/server/lib/trace/tracer',
}
module.exports = ({ dev, turbo }) => {
const externalHandler = ({ context, request, getResolve }, callback) => {
;(async () => {
if (
((dev || turbo) && request.endsWith('.shared-runtime')) ||
request.endsWith('.external')
) {
const resolve = getResolve()
const resolved = await resolve(context, request)
const relative = path.relative(
path.join(__dirname, '..'),
resolved.replace('esm' + path.sep, '')
)
callback(null, `commonjs ${relative}`)
} else {
const regexMatch = Object.keys(externalsRegexMap).find((regex) =>
new RegExp(regex).test(request)
)
if (regexMatch) {
return callback(null, 'commonjs ' + externalsRegexMap[regexMatch])
}
callback()
}
})()
}
/** @type {webpack.Configuration} */
return {
entry: {
server: path.join(__dirname, 'dist/esm/server/next-server.js'),
'app-page': path.join(
__dirname,
'dist/esm/server/future/route-modules/app-page/module.js'
),
'app-route': path.join(
__dirname,
'dist/esm/server/future/route-modules/app-route/module.js'
),
pages: path.join(
__dirname,
'dist/esm/server/future/route-modules/pages/module.js'
),
'pages-api': path.join(
__dirname,
'dist/esm/server/future/route-modules/pages-api/module.js'
),
},
target: 'node',
mode: 'production',
output: {
path: path.join(__dirname, 'dist/compiled/next-server'),
filename: `[name]${turbo ? '-turbo' : ''}.runtime.${
dev ? 'dev' : 'prod'
}.js`,
libraryTarget: 'commonjs2',
},
optimization: {
moduleIds: 'named',
minimize: true,
// splitChunks: {
// chunks: 'all',
// },
concatenateModules: true,
minimizer: [
new TerserPlugin({
extractComments: false,
terserOptions: {
format: {
comments: false,
},
compress: {
passes: 2,
},
},
}),
],
},
plugins: [
new webpack.DefinePlugin({
'process.env.NEXT_MINIMAL': JSON.stringify('true'),
'this.serverOptions.experimentalTestProxy': JSON.stringify(false),
'this.minimalMode': JSON.stringify(true),
'this.renderOpts.dev': JSON.stringify(dev),
'process.env.NODE_ENV': JSON.stringify(
dev ? 'development' : 'production'
),
'process.env.NEXT_RUNTIME': JSON.stringify('nodejs'),
...(!dev ? { 'process.env.TURBOPACK': JSON.stringify(turbo) } : {}),
}),
!!process.env.ANALYZE &&
new BundleAnalyzerPlugin({
analyzerPort: 8888 + (dev ? 0 : 1) + (turbo ? 1 : 0),
}),
].filter(Boolean),
stats: {
optimizationBailout: true,
},
externals: [...minimalExternals, externalsMap, externalHandler],
}
}