-
Notifications
You must be signed in to change notification settings - Fork 0
/
next.config.js
246 lines (232 loc) · 9.04 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
235
236
237
238
239
240
241
242
243
244
245
246
const path = require('path');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const pkg = require('./package.json');
const withDocsInfra = require('./nextConfigDocsInfra');
const { findPages } = require('./src/modules/utils/find');
const {
LANGUAGES,
LANGUAGES_SSR,
LANGUAGES_IGNORE_PAGES,
LANGUAGES_IN_PROGRESS,
} = require('./config');
const workspaceRoot = path.join(__dirname, '../');
const l10nPRInNetlify = /^l10n_/.test(process.env.HEAD) && process.env.NETLIFY === 'true';
const vercelDeploy = Boolean(process.env.VERCEL);
const isDeployPreview = Boolean(process.env.PULL_REQUEST_ID);
// For crowdin PRs we want to build all locales for testing.
const buildOnlyEnglishLocale = isDeployPreview && !l10nPRInNetlify && !vercelDeploy;
module.exports = withDocsInfra({
webpack: (config, options) => {
const plugins = config.plugins.slice();
if (process.env.DOCS_STATS_ENABLED) {
plugins.push(
// For all options see https://github.com/th0r/webpack-bundle-analyzer#as-plugin
new BundleAnalyzerPlugin({
analyzerMode: 'server',
generateStatsFile: true,
analyzerPort: options.isServer ? 8888 : 8889,
reportTitle: `${options.isServer ? 'server' : 'client'} docs bundle`,
// Will be available at `.next/${statsFilename}`
statsFilename: `stats-${options.isServer ? 'server' : 'client'}.json`,
}),
);
}
// next includes node_modules in webpack externals. Some of those have dependencies
// on the aliases defined above. If a module is an external those aliases won't be used.
// We need tell webpack to not consider those packages as externals.
if (
options.isServer &&
// Next executes this twice on the server with React 18 (once per runtime).
// We only care about Node runtime at this point.
(options.nextRuntime === undefined || options.nextRuntime === 'nodejs')
) {
const [nextExternals, ...externals] = config.externals;
config.externals = [
(ctx, callback) => {
const { request } = ctx;
const hasDependencyOnRepoPackages = [
'notistack',
'@mui/x-data-grid',
'@mui/x-data-grid-pro',
'@mui/x-date-pickers',
'@mui/x-date-pickers-pro',
'@mui/x-data-grid-generator',
'@mui/x-charts',
'@mui/x-tree-view',
'@mui/x-license-pro',
].some((dep) => request.startsWith(dep));
if (hasDependencyOnRepoPackages) {
return callback(null);
}
return nextExternals(ctx, callback);
},
...externals,
];
}
config.module.rules.forEach((r) => {
r.resourceQuery = { not: [/raw/] };
});
return {
...config,
plugins,
resolve: {
...config.resolve,
// resolve .tsx first
extensions: [
'.tsx',
...config.resolve.extensions.filter((extension) => extension !== '.tsx'),
],
},
module: {
...config.module,
rules: config.module.rules.concat([
{
test: /\.md$/,
oneOf: [
{
resourceQuery: /@mui\/markdown/,
use: [
options.defaultLoaders.babel,
{
loader: require.resolve('@mui/markdown/loader'),
options: {
ignoreLanguagePages: LANGUAGES_IGNORE_PAGES,
languagesInProgress: LANGUAGES_IN_PROGRESS,
env: {
SOURCE_CODE_REPO: options.config.env.SOURCE_CODE_REPO,
LIB_VERSION: options.config.env.LIB_VERSION,
},
},
},
],
},
{
// used in some /getting-started/templates
type: 'asset/source',
},
],
},
// transpile 3rd party packages with dependencies in this repository
{
test: /\.(js|mjs|jsx)$/,
resourceQuery: { not: [/raw/] },
include:
/node_modules(\/|\\)(notistack|@mui(\/|\\)x-data-grid|@mui(\/|\\)x-data-grid-pro|@mui(\/|\\)x-license-pro|@mui(\/|\\)x-data-grid-generator|@mui(\/|\\)x-date-pickers-pro|@mui(\/|\\)x-date-pickers|@mui(\/|\\)x-charts|@mui(\/|\\)x-tree-view)/,
use: {
loader: 'babel-loader',
options: {
// on the server we use the transpiled commonJS build, on client ES6 modules
// babel needs to figure out in what context to parse the file
sourceType: 'unambiguous',
plugins: [
[
'babel-plugin-module-resolver',
{
alias: {
// all packages in this monorepo
// '@mui/material': './packages/mui-material/src',
// '@mui/docs': './packages/mui-docs/src',
// '@mui/icons-material': './packages/mui-icons-material/lib',
// '@mui/lab': './packages/mui-lab/src',
// '@mui/styled-engine': './packages/mui-styled-engine/src',
// '@mui/styles': './packages/mui-styles/src',
// '@mui/system': './packages/mui-system/src',
// '@mui/private-theming': './packages/mui-private-theming/src',
// '@mui/utils': './packages/mui-utils/src',
// '@mui/base': './packages/mui-base/src',
// '@mui/material-next': './packages/mui-material-next/src',
// '@mui/joy': './packages/mui-joy/src',
},
// transformFunctions: ['require'],
},
],
],
},
},
},
// required to transpile ./packages/
{
test: /\.(js|mjs|tsx|ts)$/,
resourceQuery: { not: [/raw/] },
include: [workspaceRoot],
exclude: /(node_modules|mui-icons-material)/,
use: options.defaultLoaders.babel,
},
{
resourceQuery: /raw/,
type: 'asset/source',
},
]),
},
};
},
env: {
GITHUB_AUTH: process.env.GITHUB_AUTH
? `Basic ${Buffer.from(process.env.GITHUB_AUTH).toString('base64')}`
: null,
LIB_VERSION: pkg.version,
FEEDBACK_URL: process.env.FEEDBACK_URL,
SOURCE_GITHUB_BRANCH: 'master', // #default-branch-switch
SOURCE_CODE_REPO: 'https://github.com/mui/material-ui',
GITHUB_TEMPLATE_DOCS_FEEDBACK: '4.docs-feedback.yml',
BUILD_ONLY_ENGLISH_LOCALE: buildOnlyEnglishLocale,
},
// Next.js provides a `defaultPathMap` argument, we could simplify the logic.
// However, we don't in order to prevent any regression in the `findPages()` method.
exportPathMap: () => {
const pages = findPages();
const map = {};
function traverse(pages2, userLanguage) {
const prefix = userLanguage === 'en' ? '' : `/${userLanguage}`;
pages2.forEach((page) => {
// The experiments pages are only meant for experiments, they shouldn't leak to production.
if (
(page.pathname.startsWith('/experiments/') || page.pathname === '/experiments') &&
process.env.DEPLOY_ENV === 'production'
) {
return;
}
// The blog is not translated
if (userLanguage !== 'en' && LANGUAGES_IGNORE_PAGES(page.pathname)) {
return;
}
if (!page.children) {
// map api-docs to api
// i: /api-docs/* > /api/* (old structure)
// ii: /*/api-docs/* > /*/api/* (for new structure)
map[`${prefix}${page.pathname.replace(/^(\/[^/]+)?\/api-docs\/(.*)/, '$1/api/$2')}`] = {
page: page.pathname,
query: {
userLanguage,
},
};
return;
}
traverse(page.children, userLanguage);
});
}
// We want to speed-up the build of pull requests.
// For this, consider only English language on deploy previews, except for crowdin PRs.
if (buildOnlyEnglishLocale) {
// eslint-disable-next-line no-console
console.log('Considering only English for SSR');
traverse(pages, 'en');
} else {
// eslint-disable-next-line no-console
console.log('Considering various locales for SSR');
LANGUAGES_SSR.forEach((userLanguage) => {
traverse(pages, userLanguage);
});
}
return map;
},
// rewrites has no effect when run `next export` for production
rewrites: async () => {
return [
{ source: `/:lang(${LANGUAGES.join('|')})?/:rest*`, destination: '/:rest*' },
// Make sure to include the trailing slash if `trailingSlash` option is set
{ source: '/api/:rest*/', destination: '/api-docs/:rest*/' },
{ source: `/static/x/:rest*`, destination: 'http://0.0.0.0:3001/static/x/:rest*' },
];
},
});