forked from belgattitude/nextjs-monorepo-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnext.config.js
234 lines (206 loc) · 6.38 KB
/
next.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
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
// @ts-check
const pc = require('picocolors');
const { i18n } = require('./next-i18next.config');
const packageJson = require('./package.json');
const trueEnv = ['true', '1', 'yes'];
const isProd = process.env.NODE_ENV === 'production';
const isCI = trueEnv.includes(process.env?.CI ?? 'false');
const NEXTJS_IGNORE_ESLINT = trueEnv.includes(
process.env?.NEXTJS_IGNORE_ESLINT ?? 'false'
);
const NEXTJS_IGNORE_TYPECHECK = trueEnv.includes(
process.env?.NEXTJS_IGNORE_TYPECHECK ?? 'false'
);
/**
* A way to allow CI optimization when the build done there is not used
* to deliver an image or deploy the files.
* @link https://nextjs.org/docs/advanced-features/source-maps
*/
const disableSourceMaps = trueEnv.includes(
process.env?.NEXT_DISABLE_SOURCEMAPS ?? 'false'
);
if (disableSourceMaps) {
console.info(
`${pc.green(
'notice'
)}- Sourcemaps generation have been disabled through NEXT_DISABLE_SOURCEMAPS`
);
}
// Tell webpack to compile those packages
// @link https://www.npmjs.com/package/next-transpile-modules
const tmModules = [
// for legacy browsers support (only in prod)
...(isProd
? [
// ie: '@react-google-maps/api'...
]
: []),
// ESM only packages are not yet supported by NextJs if you're not
// using experimental experimental esmExternals
// @link {https://nextjs.org/blog/next-11-1#es-modules-support|Blog 11.1.0}
// @link {https://github.com/vercel/next.js/discussions/27876|Discussion}
// @link https://github.com/vercel/next.js/issues/23725
// @link https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
...[
// ie: newer versions of https://github.com/sindresorhus packages
],
];
// Example of setting up secure headers
// @link https://github.com/jagaapple/next-secure-headers
const { createSecureHeaders } = require('next-secure-headers');
const secureHeaders = createSecureHeaders({
contentSecurityPolicy: {
directives: {
// defaultSrc: "'self'",
// styleSrc: ["'self'"],
},
},
...(isProd
? {
forceHTTPSRedirect: [
true,
{ maxAge: 60 * 60 * 24 * 4, includeSubDomains: true },
],
}
: {}),
referrerPolicy: 'same-origin',
});
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
reactStrictMode: true,
productionBrowserSourceMaps: !disableSourceMaps,
i18n,
optimizeFonts: true,
httpAgentOptions: {
// @link https://nextjs.org/blog/next-11-1#builds--data-fetching
keepAlive: true,
},
onDemandEntries: {
// period (in ms) where the server will keep pages in the buffer
maxInactiveAge: (isCI ? 3600 : 25) * 1000,
},
// @link https://nextjs.org/docs/advanced-features/compiler#minification
swcMinify: true,
experimental: {
// React 18
// @link https://nextjs.org/docs/advanced-features/react-18
reactRoot: true,
// React 18 streaming
// @link https://nextjs.org/docs/advanced-features/react-18/streaming
runtime: undefined,
// React 18 server components
// @link https://nextjs.org/docs/advanced-features/react-18/server-components
serverComponents: false,
// Standalone build
// @link https://nextjs.org/docs/advanced-features/output-file-tracing#automatically-copying-traced-files-experimental
outputStandalone: false,
// @link https://nextjs.org/docs/advanced-features/output-file-tracing#caveats
outputFileTracingRoot: undefined, // ,path.join(__dirname, '../../'),
// Prefer loading of ES Modules over CommonJS
// @link {https://nextjs.org/blog/next-11-1#es-modules-support|Blog 11.1.0}
// @link {https://github.com/vercel/next.js/discussions/27876|Discussion}
esmExternals: true,
// Experimental monorepo support
// @link {https://github.com/vercel/next.js/pull/22867|Original PR}
// @link {https://github.com/vercel/next.js/discussions/26420|Discussion}
externalDir: true,
},
// @link https://nextjs.org/docs/basic-features/image-optimization
images: {
loader: 'default',
deviceSizes: [640, 750, 828, 1080, 1200, 1920, 2048, 3840],
imageSizes: [16, 32, 48, 64, 96, 128, 256, 384],
disableStaticImages: false,
// https://nextjs.org/docs/api-reference/next/image#caching-behavior
minimumCacheTTL: 60,
// Allowed domains for next/image
domains: ['source.unsplash.com'],
},
typescript: {
ignoreBuildErrors: NEXTJS_IGNORE_TYPECHECK,
},
eslint: {
ignoreDuringBuilds: NEXTJS_IGNORE_ESLINT,
dirs: ['src'],
},
async headers() {
return [{ source: '/(.*)', headers: secureHeaders }];
},
/**
* @link https://nextjs.org/docs/api-reference/next.config.js/rewrites
async rewrites() {
return [
{
source: `/`,
destination: '/demo',
},
];
},
*/
webpack: (config, { isServer }) => {
if (!isServer) {
// Swap sentry/node by sentry/browser
config.resolve.alias['@sentry/node'] = '@sentry/browser';
}
if (isServer) {
// Till undici 4 haven't landed in prisma, we need this for docker/alpine
// @see https://github.com/prisma/prisma/issues/6925#issuecomment-905935585
config.externals.push('_http_common');
}
config.module.rules.push({
test: /\.svg$/,
issuer: /\.(js|ts)x?$/,
use: [
{
loader: '@svgr/webpack',
// https://react-svgr.com/docs/webpack/#passing-options
options: {
svgo: true,
// @link https://github.com/svg/svgo#configuration
svgoConfig: {
multipass: false,
datauri: 'base64',
js2svg: {
indent: 2,
pretty: false,
},
},
},
},
],
});
return config;
},
env: {
APP_NAME: packageJson.name,
APP_VERSION: packageJson.version,
BUILD_TIME: new Date().toISOString(),
},
serverRuntimeConfig: {
// to bypass https://github.com/zeit/next.js/issues/8251
PROJECT_ROOT: __dirname,
},
};
let config;
if (tmModules.length > 0) {
const withNextTranspileModules = require('next-transpile-modules')(
tmModules,
{
resolveSymlinks: true,
debug: false,
}
);
config = withNextTranspileModules(nextConfig);
} else {
config = nextConfig;
}
if (process.env.ANALYZE === 'true') {
// @ts-ignore
const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: true,
});
config = withBundleAnalyzer(config);
}
module.exports = config;