forked from belgattitude/nextjs-monorepo-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
next.config.js
178 lines (156 loc) · 5.25 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
// @ts-check
const { withSentryConfig } = require('@sentry/nextjs');
const { i18n } = require('./next-i18next.config');
const packageJson = require('./package.json');
const NEXTJS_BUILD_TARGET = process.env.NEXTJS_BUILD_TARGET || 'server';
const NEXTJS_IGNORE_ESLINT = process.env.NEXTJS_IGNORE_ESLINT === '1' || false;
const NEXTJS_IGNORE_TYPECHECK =
process.env.NEXTJS_IGNORE_TYPECHECK === '1' || false;
const isProd = process.env.NODE_ENV === 'production';
// 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
],
];
const withNextTranspileModules = require('next-transpile-modules')(tmModules, {
resolveSymlinks: true,
debug: 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 = process.env.NEXT_DISABLE_SOURCEMAPS === 'true';
if (disableSourceMaps) {
console.log(
'[INFO]: Sourcemaps generation have been disabled through NEXT_DISABLE_SOURCEMAPS'
);
}
// 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 = {
target: NEXTJS_BUILD_TARGET,
reactStrictMode: true,
productionBrowserSourceMaps: !disableSourceMaps,
i18n,
optimizeFonts: true,
httpAgentOptions: {
// @link https://nextjs.org/blog/next-11-1#builds--data-fetching
keepAlive: true,
},
experimental: {
// 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
// @ts-ignore
images: {
deviceSizes: [640, 750, 828, 1080, 1200, 1920, 2048, 3840],
imageSizes: [16, 32, 48, 64, 96, 128, 256, 384],
disableStaticImages: false,
loader: 'default',
// 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 }];
},
webpack: (config, { isServer }) => {
if (isServer) {
// Add specific config for server mode
}
config.module.rules.push({
test: /\.svg$/,
issuer: /\.(js|ts)x?$/,
use: ['@svgr/webpack'],
});
return config;
},
env: {
APP_NAME: packageJson.name,
APP_VERSION: packageJson.version,
BUILD_TIME: new Date().getTime().toString(10),
SENTRY_RELEASE: process.env.SENTRY_RELEASE
? process.env.SENTRY_RELEASE
: `${packageJson.name}@${packageJson.version}`,
NEXT_PUBLIC_SENTRY_DSN: process.env.SENTRY_DSN ?? '',
},
serverRuntimeConfig: {
// to bypass https://github.com/zeit/next.js/issues/8251
PROJECT_ROOT: __dirname,
},
};
let config = withNextTranspileModules(nextConfig);
if (process.env.NEXT_DISABLE_SENTRY !== '1') {
/** @type {Partial<import('@sentry/nextjs/dist/config/types').SentryWebpackPluginOptions>} */
const sentryWebpackPluginOptions = {
// Additional config options for the Sentry Webpack plugin. Keep in mind that
// the following options are set automatically, and overriding them is not
// recommended:
// release, url, org, project, authToken, configFile, stripPrefix,
// urlPrefix, include, ignore
// For all available options, see:
// https://github.com/getsentry/sentry-webpack-plugin#options.
dryRun:
process.env.NODE_ENV !== 'production' ||
process.env.NEXT_SENTRY_DRY_RUN === '1',
};
config = withSentryConfig(config, sentryWebpackPluginOptions);
}
if (process.env.ANALYZE === 'true') {
// @ts-ignore
const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: true,
});
config = withBundleAnalyzer(config);
}
module.exports = config;