-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathnext.config.mjs
244 lines (229 loc) · 8.15 KB
/
next.config.mjs
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
// @ts-check
import * as path from 'path';
import * as url from 'url';
import * as fs from 'fs';
import { createRequire } from 'module';
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
// const withTM from 'next-transpile-modules')(['@mui/monorepo'];
// @ts-expect-error This expected error should be gone once we update the monorepo
import withDocsInfra from '@mui/monorepo/docs/nextConfigDocsInfra.js';
import { findPages } from './src/modules/utils/find.mjs';
import {
LANGUAGES,
LANGUAGES_SSR,
LANGUAGES_IGNORE_PAGES,
LANGUAGES_IN_PROGRESS,
} from './config.js';
import constants from './constants.js';
const currentDirectory = url.fileURLToPath(new URL('.', import.meta.url));
const require = createRequire(import.meta.url);
const WORKSPACE_ROOT = path.resolve(currentDirectory, '../');
const MONOREPO_PATH = path.resolve(WORKSPACE_ROOT, './node_modules/@mui/monorepo');
const MONOREPO_ALIASES = {
'@mui/docs': path.resolve(MONOREPO_PATH, './packages/mui-docs/src'),
};
const WORKSPACE_ALIASES = {
'@mui/x-data-grid': path.resolve(WORKSPACE_ROOT, './packages/x-data-grid/src'),
'@mui/x-data-grid-generator': path.resolve(
WORKSPACE_ROOT,
'./packages/x-data-grid-generator/src',
),
'@mui/x-data-grid-pro': path.resolve(WORKSPACE_ROOT, './packages/x-data-grid-pro/src'),
'@mui/x-data-grid-premium': path.resolve(WORKSPACE_ROOT, './packages/x-data-grid-premium/src'),
'@mui/x-date-pickers': path.resolve(WORKSPACE_ROOT, './packages/x-date-pickers/src'),
'@mui/x-date-pickers-pro': path.resolve(WORKSPACE_ROOT, './packages/x-date-pickers-pro/src'),
'@mui/x-charts': path.resolve(WORKSPACE_ROOT, './packages/x-charts/src'),
'@mui/x-tree-view': path.resolve(WORKSPACE_ROOT, './packages/x-tree-view/src'),
'@mui/x-tree-view-pro': path.resolve(WORKSPACE_ROOT, './packages/x-tree-view-pro/src'),
'@mui/x-license': path.resolve(WORKSPACE_ROOT, './packages/x-license/src'),
};
/**
* @param {string} pkgPath
* @returns {{version: string}}
*/
function loadPkg(pkgPath) {
const pkgContent = fs.readFileSync(path.resolve(WORKSPACE_ROOT, pkgPath, 'package.json'), 'utf8');
return JSON.parse(pkgContent);
}
const pkg = loadPkg('.');
const dataGridPkg = loadPkg('./packages/x-data-grid');
const datePickersPkg = loadPkg('./packages/x-date-pickers');
const chartsPkg = loadPkg('./packages/x-charts');
const treeViewPkg = loadPkg('./packages/x-tree-view');
let localSettings = {};
try {
// eslint-disable-next-line import/no-unresolved
localSettings = require('./next.config.local.js');
} catch (_) {
// Ignore
}
export default withDocsInfra({
transpilePackages: [
// TODO, those shouldn't be needed in the first place
'@mui/monorepo', // Migrate everything to @mui/docs until the @mui/monorepo dependency becomes obsolete
'@mui/docs',
],
// Avoid conflicts with the other Next.js apps hosted under https://mui.com/
assetPrefix: process.env.DEPLOY_ENV === 'development' ? undefined : '/x',
env: {
// docs-infra
LIB_VERSION: pkg.version,
SOURCE_CODE_REPO: constants.SOURCE_CODE_REPO,
SOURCE_GITHUB_BRANCH: constants.SOURCE_GITHUB_BRANCH,
GITHUB_TEMPLATE_DOCS_FEEDBACK: '6.docs-feedback.yml',
// MUI X related
DATA_GRID_VERSION: dataGridPkg.version,
DATE_PICKERS_VERSION: datePickersPkg.version,
CHARTS_VERSION: chartsPkg.version,
TREE_VIEW_VERSION: treeViewPkg.version,
},
// @ts-ignore
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,
// Will be available at `.next/stats.json`
statsFilename: 'stats.json',
}),
);
}
return {
...config,
plugins,
// TODO, this shouldn't be needed in the first place
// Migrate everything from @mui/monorepo to @mui/docs and embed @mui/internal-markdown in @mui/docs
resolveLoader: {
...config.resolveLoader,
alias: {
...config.resolveLoader.alias,
'@mui/internal-markdown/loader': path.resolve(
MONOREPO_PATH,
'./packages/markdown/loader',
),
},
},
resolve: {
...config.resolve,
alias: {
...config.resolve.alias,
...MONOREPO_ALIASES,
...WORKSPACE_ALIASES,
// TODO: get rid of this, replace with @mui/docs
docs: path.resolve(MONOREPO_PATH, './docs'),
docsx: path.resolve(currentDirectory, '../docs'),
},
},
module: {
...config.module,
rules: config.module.rules.concat([
// used in some /getting-started/templates
{
test: /\.md$/,
oneOf: [
{
resourceQuery: /muiMarkdown/,
use: [
options.defaultLoaders.babel,
{
loader: '@mui/internal-markdown/loader',
options: {
workspaceRoot: WORKSPACE_ROOT,
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,
},
},
},
],
},
],
},
{
test: /\.+(js|jsx|mjs|ts|tsx)$/,
include: [/(@mui[\\/]monorepo)$/, /(@mui[\\/]monorepo)[\\/](?!.*node_modules)/],
use: options.defaultLoaders.babel,
},
{
test: /\.(ts|tsx)$/,
loader: 'string-replace-loader',
options: {
search: '__RELEASE_INFO__',
replace: 'MTU5NjMxOTIwMDAwMA==', // 2020-08-02
},
},
]),
},
};
},
distDir: 'export',
// 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 = {};
// @ts-ignore
function traverse(pages2, userLanguage) {
const prefix = userLanguage === 'en' ? '' : `/${userLanguage}`;
// @ts-ignore
pages2.forEach((page) => {
// The experiments pages are only meant for experiments, they shouldn't leak to production.
if (page.pathname.includes('/experiments/') && process.env.DEPLOY_ENV === 'production') {
return;
}
if (!page.children) {
// @ts-ignore
map[`${prefix}${page.pathname.replace(/^\/api-docs\/(.*)/, '/api/$1')}`] = {
page: page.pathname,
query: {
userLanguage,
},
};
return;
}
traverse(page.children, userLanguage);
});
}
// We want to speed-up the build of pull requests.
if (process.env.PULL_REQUEST === 'true') {
// 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;
},
// Used to signal we run build
...(process.env.NODE_ENV === 'production'
? {
output: 'export',
}
: {
rewrites: async () => {
return [
{ source: `/:lang(${LANGUAGES.join('|')})?/:rest*`, destination: '/:rest*' },
{ source: '/api/:rest*', destination: '/api-docs/:rest*' },
];
},
// redirects only take effect in the development, not production (because of `next export`).
redirects: async () => [
{
source: '/',
destination: '/x/introduction/',
permanent: false,
},
],
}),
...localSettings,
});