generated from elastic/next-eui-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnext.config.js
285 lines (245 loc) · 8.5 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* eslint-disable @typescript-eslint/no-var-requires,@typescript-eslint/no-use-before-define,@typescript-eslint/no-empty-function,prefer-template */
const crypto = require('crypto');
const fs = require('fs');
const glob = require('glob');
const path = require('path');
const iniparser = require('iniparser');
const withBundleAnalyzer = require('@next/bundle-analyzer');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const { IgnorePlugin } = require('webpack');
/**
* If you are deploying your site under a directory other than `/` e.g.
* GitHub pages, then you have to tell Next where the files will be served.
* We don't need this during local development, because everything is
* available under `/`.
*/
const usePathPrefix = process.env.PATH_PREFIX === 'true';
const pathPrefix = usePathPrefix ? derivePathPrefix() : '';
const themeConfig = buildThemeConfig();
const nextConfig = {
/** Disable the `X-Powered-By: Next.js` response header. */
poweredByHeader: false,
/**
* When set to something other than '', this field instructs Next to
* expect all paths to have a specific directory prefix. This fact is
* transparent to (almost all of) the rest of the application.
*/
basePath: pathPrefix,
/**
* Set custom `process.env.SOMETHING` values to use in the application.
* You can do this with Webpack's `DefinePlugin`, but this is more concise.
* It's also possible to provide values via `publicRuntimeConfig`, but
* this method is preferred as it can be done statically at build time.
*
* @see https://nextjs.org/docs/api-reference/next.config.js/environment-variables
*/
env: {
PATH_PREFIX: pathPrefix,
THEME_CONFIG: JSON.stringify(themeConfig),
},
/**
* Next.js reports TypeScript errors by default. If you don't want to
* leverage this behavior and prefer something else instead, like your
* editor's integration, you may want to disable it.
*/
// typescript: {
// ignoreDevErrors: true,
// },
/** Customises the build */
webpack(config, { isServer }) {
// EUI uses some libraries and features that don't work outside of a
// browser by default. We need to configure the build so that these
// features are either ignored or replaced with stub implementations.
if (isServer) {
config.externals = config.externals.map(eachExternal => {
if (typeof eachExternal !== 'function') {
return eachExternal;
}
return (context, callback) => {
if (context.request.indexOf('@elastic/eui') > -1) {
return callback();
}
return eachExternal(context, callback);
};
});
// Mock HTMLElement on the server-side
const definePluginId = config.plugins.findIndex(
p => p.constructor.name === 'DefinePlugin'
);
config.plugins[definePluginId].definitions = {
...config.plugins[definePluginId].definitions,
HTMLElement: function () {},
};
}
// Copy theme CSS files into `public`
config.plugins.push(
new CopyWebpackPlugin({ patterns: themeConfig.copyConfig }),
// Moment ships with a large number of locales. Exclude them, leaving
// just the default English locale. If you need other locales, see:
// https://create-react-app.dev/docs/troubleshooting/#momentjs-locales-are-missing
new IgnorePlugin({
resourceRegExp: /^\.\/locale$/,
contextRegExp: /moment$/,
})
);
config.resolve.mainFields = ['module', 'main'];
return config;
},
/**
* If you want to use dynamic routes with Static HTML Export, then you
* have to manually define (or in this case, extend) the path map.
* Otherwise, Next can't generate static pages for each path - everything
* would work on the client so long as an explicit path was loaded first,
* but a direct fetch on a dynamic route wouldn't work. If you don't use
* dynamic routes, then you don't need this function at all.
*
* If you can't generate all the possible paths at build time, for
* example by hard-coding or querying an API, you'll need some kind of
* server-side redirect, which is outside the scope of this project.
*
* @see https://nextjs.org/docs/routing/introduction
* @see https://nextjs.org/docs/advanced-features/static-html-export
*/
exportPathMap: async function (defaultPathMap) {
const dynamicPaths = [
'/my-dashboard',
'/workpad',
'/my-logs',
'/my-workpad',
'/my-logs',
'/apm',
'/metrics',
'/logs',
'/uptime',
'/maps',
'/siem',
'/canvas',
'/discover',
'/visualize',
'/dashboard',
'/machine-learning',
'/custom-plugin',
'/dev-tools',
'/stack-monitoring',
'/stack-management',
];
const pathMap = {
...defaultPathMap,
};
for (const path of dynamicPaths) {
pathMap[`/my-app${path}`] = { page: '/my-app/[slug]' };
}
return pathMap;
},
};
/**
* Enhances the Next config with the ability to:
* - Analyze the webpack bundle
* - Load images from JavaScript.
* - Load SCSS files from JavaScript.
*/
module.exports = withBundleAnalyzer({
enabled: process.env.ANALYZE === 'true',
})(nextConfig);
/**
* Find all EUI themes and construct a theme configuration object.
*
* The `copyConfig` key is used to configure CopyWebpackPlugin, which
* copies the default EUI themes into the `public` directory, injecting a
* hash into the filename so that when EUI is updated, new copies of the
* themes will be fetched.
*
* The `availableThemes` key is used in the app to includes the themes in
* the app's `<head>` element, and for theme switching.
*
* @return {ThemeConfig}
*/
function buildThemeConfig() {
const themeFiles = glob.sync(
path.join(
__dirname,
'node_modules',
'@elastic',
'eui',
'dist',
'eui_theme_*.min.css'
)
);
const themeConfig = {
availableThemes: [],
copyConfig: [],
};
for (const each of themeFiles) {
const basename = path.basename(each, '.min.css');
const themeId = basename.replace(/^eui_theme_/, '');
const themeName =
themeId[0].toUpperCase() + themeId.slice(1).replace(/_/g, ' ');
const publicPath = `themes/${basename}.${hashFile(each)}.min.css`;
const toPath = path.join(
__dirname,
`public`,
`themes`,
`${basename}.${hashFile(each)}.min.css`
);
themeConfig.availableThemes.push({
id: themeId,
name: themeName,
publicPath,
});
themeConfig.copyConfig.push({
from: each,
to: toPath,
});
}
return themeConfig;
}
/**
* Given a file, calculate a hash and return the first portion. The number
* of characters is truncated to match how Webpack generates hashes.
*
* @param {string} filePath the absolute path to the file to hash.
* @return string
*/
function hashFile(filePath) {
const hash = crypto.createHash(`sha256`);
const fileData = fs.readFileSync(filePath);
hash.update(fileData);
const fullHash = hash.digest(`hex`);
// Use a hash length that matches what Webpack does
return fullHash.substr(0, 20);
}
/**
* This starter assumes that if `usePathPrefix` is true, then you're serving the site
* on GitHub pages. If that isn't the case, then you can simply replace the call to
* this function with whatever is the correct path prefix.
*
* The implementation attempts to derive a path prefix for serving up a static site by
* looking at the following in order.
*
* 1. The git config for "origin"
* 2. The `name` field in `package.json`
*
* Really, the first should be sufficient and correct for a GitHub Pages site, because the
* repository name is what will be used to serve the site.
*/
function derivePathPrefix() {
const gitConfigPath = path.join(__dirname, '.git', 'config');
if (fs.existsSync(gitConfigPath)) {
const gitConfig = iniparser.parseSync(gitConfigPath);
if (gitConfig['remote "origin"'] != null) {
const originUrl = gitConfig['remote "origin"'].url;
// eslint-disable-next-line prettier/prettier
return '/' + originUrl.split('/').pop().replace(/\.git$/, '');
}
}
const packageJsonPath = path.join(__dirname, 'package.json');
if (fs.existsSync(packageJsonPath)) {
const { name: packageName } = require(packageJsonPath);
// Strip out any username / namespace part. This works even if there is
// no username in the package name.
return '/' + packageName.split('/').pop();
}
throw new Error(
"Can't derive path prefix, as neither .git/config nor package.json exists"
);
}