forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnext.config.js
170 lines (154 loc) · 5.32 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
const webpack = require('webpack');
const path = require('path');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const withTypescript = require('@zeit/next-typescript');
const pkg = require('../package.json');
const { findPages } = require('./src/modules/utils/find');
const { LANGUAGES_SSR } = require('./src/modules/constants');
const workspaceRoot = path.join(__dirname, '../');
/**
* @type {'legacy' | 'sync' | 'concurrent'}
*/
const reactMode = 'legacy';
module.exports = withTypescript({
webpack: (config, options) => {
const plugins = config.plugins.concat([
new webpack.DefinePlugin({
'process.env': {
LIB_VERSION: JSON.stringify(pkg.version),
ENABLE_AD: JSON.stringify(process.env.ENABLE_AD),
REACT_MODE: JSON.stringify(reactMode),
},
}),
]);
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,
// Will be available at `.next/stats.json`
statsFilename: 'stats.json',
}),
);
}
config.resolve.alias['react-dom$'] = 'react-dom/profiling';
config.resolve.alias['scheduler/tracing'] = 'scheduler/tracing-profiling';
if (reactMode !== 'legacy') {
config.resolve.alias['react-transition-group'] = '@material-ui/react-transition-group';
}
// 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) {
const [nextExternals, ...externals] = config.externals;
if (externals.length > 0) {
// currently not the case but other next plugins might introduce additional
// rules for externals. We would need to handle those in the callback
throw new Error('There are other externals in the webpack config.');
}
config.externals = [
(context, request, callback) => {
const hasDependencyOnRepoPackages = [
'notistack',
'material-table',
'@material-ui/pickers',
].includes(request);
if (hasDependencyOnRepoPackages) {
return callback(null);
}
return nextExternals(context, request, callback);
},
];
}
return Object.assign({}, config, {
plugins,
node: {
fs: 'empty',
},
module: Object.assign({}, config.module, {
rules: config.module.rules.concat([
{
test: /\.(css|md)$/,
loader: 'emit-file-loader',
options: {
name: 'dist/[path][name].[ext]',
},
},
{
test: /\.(css|md)$/,
loader: 'raw-loader',
},
// transpile 3rd party packages with dependencies in this repository
{
test: /\.(js|mjs|jsx)$/,
include: /node_modules(\/|\\)(material-table|notistack|@material-ui(\/|\\)pickers)/,
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: {
'@material-ui/core': '../packages/material-ui/src',
},
transformFunctions: ['require'],
},
],
],
},
},
},
// required to transpile ../packages/
{
test: /\.(js|mjs|jsx)$/,
include: [workspaceRoot],
exclude: /node_modules/,
use: options.defaultLoaders.babel,
},
]),
}),
});
},
exportTrailingSlash: true,
// 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 => {
if (!page.children) {
map[`${prefix}${page.pathname}`] = {
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') {
traverse(pages, 'en');
} else {
LANGUAGES_SSR.forEach(userLanguage => {
traverse(pages, userLanguage);
});
}
return map;
},
onDemandEntries: {
// Period (in ms) where the server will keep pages in the buffer
maxInactiveAge: 120 * 1e3, // default 25s
// Number of pages that should be kept simultaneously without being disposed
pagesBufferLength: 3, // default 2
},
});