-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
283 lines (249 loc) · 8.45 KB
/
webpack.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
const glob = require('glob');
const path = require('path');
// webpack config reference:
// https://webpack.js.org/configuration/
const CopyWebpackPlugin = require('copy-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin'); // eslint-disable-line
const StaticSiteGeneratorPlugin = require('./scripts/libs/StaticSiteGeneratorWebpack4Plugin.js'); // eslint-disable-line
// Utils needed for building the static webpages, instead of requiring them in each
// individual script, and thus bundling them in the final app.min.js, we're much better
// off just passing them through webpack's server if they are only needed for the
// markup rendering.
const fs = require('fs');
const { JSDOM } = require('jsdom');
const parser = require('parse5');
const parserUtils = require('parse5-utils');
const { renderToString } = require('react-dom/server');
// configurations
require('toml-require').install();
const netlifyToml = require('./netlify.toml');
const packageJson = require('./package.json');
const allExports = {
mode: process.env.NODE_ENV,
devtool: process.env.NODE_ENV === 'development' ? 'source-map' : false,
performance: {
// maxEntrypointSize: 1048576,
// maxAssetSize: 1048576,
hints: false,
assetFilter: (assetFilename) => {
const check = new RegExp(`.(${packageJson['sharp-config'].types.join('|')})$`, 'gi');
return !check.test(assetFilename);
},
},
};
const allPlugins = [];
if (process.env.NODE_ENV !== 'development') {
allPlugins.push(new TerserPlugin());
}
// A small custom plugin to instruct webpack to (recursively) watch for changes within a directory.
class WatchDirectoriesPlugin {
constructor(directories) {
this.directories = (Array.isArray(directories)) ? directories : [ directories ];
}
apply(compiler) {
const plugin = { name: 'WatchDirectoriesPlugin' };
compiler.hooks.afterCompile.tapAsync(plugin, (compilation, callback) => {
this.directories.forEach((dir) => {
compilation.contextDependencies.add(path.resolve(__dirname, dir));
});
callback();
});
}
}
// Returns a simulated window for use in Node during SSR.
const buildGlobals = () => {
const globals = new JSDOM('...', {
beforeParse(window) {
// Some package somewhere tries calling window.location.reload, which
// triggers a notice in the console by jsdom that we don't care about.
// (Haven't figured out what causes this, started after doing some
// updates to dependencies, maybe React?)
Object.defineProperty(window, 'location', {
enumerable: true,
configurable: true,
value: Object.getOwnPropertyNames(window.location)
.reduce((res, key) => {
switch (key) {
case 'reload':
res[key] = () => {};
break;
default: {
// Defining properties from descriptors triggers illegal invocations at
// /node_modules/jsdom/lib/jsdom/living/generated/Location.js:242
// For our use-case, we can live without the rest of the functionality
// from the location object.
res[key] = window.location[key];
break;
}
}
return res;
}, {}),
});
// We don't want any console output at this stage.
window.console = {
log() {},
warn() {},
error() {},
};
// Avoid throwing compile errors because these are not available in
// this context.
delete window.localStorage;
delete window.sessionStorage;
},
});
return { ...(globals.window) };
};
module.exports = [
{
...allExports,
// Here the application starts executing and webpack starts bundling
entry: {
js: [
// './scripts/webpack/polyfills.js',
// './scripts/webpack/identity.js',
'./scripts/webpack/site.js',
],
admin: [
// './scripts/webpack/polyfills.js',
// './scripts/webpack/identity.js',
'./scripts/webpack/admin.js',
],
},
// options related to how webpack emits results
output: {
// the target directory for all output files
// must be an absolute path (use the Node.js path module)
path: path.resolve(__dirname, netlifyToml.build.publish),
// the filename template for entry chunks
filename: '[name]/main.min.js',
},
// configuration regarding modules
module: {
// rules for modules (configure loaders, parser options, etc.)
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
babelrc: false,
presets: [
'@babel/preset-env',
],
},
},
},
],
},
// additional plugins
plugins: allPlugins.concat([
// Make sure newly added files (even within subfolders) trigger a recompile.
new WatchDirectoriesPlugin([
'./js/site',
'./js/admin',
]),
]),
// This is not specific to building the js files,
// it's just that webpack-dev-server fetches these configs from the first exports entry.
// Note that currently (as specified in the package.json scripts) webpack is run twice when
// serving locally, once for webpack itself to build the files into the public directory,
// and again for webpack-server itself to serve the files from memory.
devServer: {
contentBase: path.resolve(__dirname, netlifyToml.build.publish),
compress: true,
historyApiFallback: {
rewrites: [
{
// Without this rule, if we typed "/admin" we would end up in "/admin#/"
// instead of "/admin/#/", making it impossible to use the CMS interface
// in localhost.
from: /\/admin$/,
to: '/admin/',
},
],
},
port: packageJson.config.port,
watchContentBase: true,
},
},
{
...allExports,
entry: [
'./scripts/webpack/polyfills.js',
'./components/App.jsx',
],
output: {
path: path.resolve(__dirname, netlifyToml.build.publish),
filename: 'js/app.min.js',
// Required for static-site-generator-webpack-plugin,
libraryTarget: 'umd',
},
// configuration regarding modules
module: {
// rules for modules (configure loaders, parser options, etc.)
rules: [
{
test: /\.jsx$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
babelrc: false,
presets: [
'@babel/preset-env',
'@babel/preset-react',
],
plugins: [
'@babel/plugin-proposal-class-properties',
],
},
},
},
{
test: /\.css$/,
use: {
loader: 'ignore-loader',
},
},
],
},
resolve: {
modules: [ 'node_modules', path.resolve(__dirname, 'components') ],
extensions: [ '.js', '.json', '.jsx' ],
},
// additional plugins
plugins: allPlugins.concat([
// Serve all hugo-generated files, including static files, except for html files (see below).
new CopyWebpackPlugin([
{
from: 'temp/hugo',
to: path.resolve(__dirname, netlifyToml.build.publish),
ignore: [ '*.html' ],
},
]),
// Hugo-generated html files go through a react server-side rendering process,
// so that we serve full markup pages. This way we're obeying a JAM stack architecture,
// while still allowing for any needed extra JS/React dynamics that should fail gracefully,
// that is fail without compromising access to the basic/critical information on the pages.
new StaticSiteGeneratorPlugin({
paths: glob.sync('./temp/hugo/**/*.html'),
locals: {
fs,
parser,
parserUtils,
renderToString,
pathsReplace: [
{ from: 'temp/hugo/', to: '' },
],
},
// Simulate a window object inside our generator environment;
// many different dependencies can expect different global properties,
// even webpack itself, in particular its hot reload module, requires a
// minimally "real" environment.
// Ref: https://github.com/tmpvar/jsdom
globals: buildGlobals(),
}),
]),
},
];