forked from coralproject/talk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
453 lines (419 loc) · 12.7 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
const path = require('path');
const fs = require('fs');
const CompressionPlugin = require('compression-webpack-plugin');
const BrotliPlugin = require('brotli-webpack-plugin');
const autoprefixer = require('autoprefixer');
const precss = require('precss');
const _ = require('lodash');
const Copy = require('copy-webpack-plugin');
const webpack = require('webpack');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const debug = require('debug')('talk:webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const ManifestPlugin = require('webpack-manifest-plugin');
// Needed to enforce stable asset hashes.
// https://medium.com/webpack/predictable-long-term-caching-with-webpack-d3eee1d3fa31
const NameAllModulesPlugin = require('name-all-modules-plugin');
// Possibly load the config from the .env file (if there is one).
require('dotenv').config();
const { plugins, pluginsPath, PluginManager } = require('./plugins');
const manager = new PluginManager(plugins);
const targetPlugins = manager.section('targets').plugins;
const clientPlugins = manager.section('client').plugins;
debug(`Using ${pluginsPath} as the plugin configuration path`);
const buildTargets = [
'coral-admin',
'coral-login',
{ name: 'coral-auth-callback', disablePolyfill: true },
];
const buildEmbeds = ['stream'];
// In production, default turn off source maps. In development, default use
// 'cheap-module-source-map'.
const DEFAULT_WEBPACK_SOURCE_MAP =
process.env.NODE_ENV === 'production' ? 'none' : 'cheap-module-source-map';
// TALK_WEBPACK_SOURCE_MAP is sourced from the environment, defaulting based on
// the environment.
const TALK_WEBPACK_SOURCE_MAP = _.get(
process.env,
'TALK_WEBPACK_SOURCE_MAP',
DEFAULT_WEBPACK_SOURCE_MAP
);
// Set the devtool based on the source map selection, 'none' just means turn off
// source maps.
const devtool =
TALK_WEBPACK_SOURCE_MAP === 'none' ? false : TALK_WEBPACK_SOURCE_MAP;
// Exclude everything from node_modules except for those that may be in a
// plugin folder inside the node_modules folder.
const exclude = {
and: [
// Exclude everything inside node_modules..
/node_modules/,
// But not those that contain plugins.
{
not: clientPlugins.map(({ name }) => new RegExp(`node_modules\/${name}`)),
},
],
};
//==============================================================================
// Base Webpack Config
//==============================================================================
const config = {
devtool,
target: 'web',
output: {
path: path.join(__dirname, 'dist'),
publicPath: '',
filename: '[name].[chunkhash].js',
chunkFilename: '[name].[chunkhash].chunk.js',
},
module: {
rules: [
{
loader: 'plugins-loader',
test: /\.(json|js)$/,
include: pluginsPath,
},
{
loader: 'babel-loader',
exclude,
test: /\.js$/,
query: {
cacheDirectory: true,
},
},
{
loader: 'hjson-loader',
test: /\.(json|yml)$/,
exclude,
},
{
loader: 'yaml-loader',
exclude,
test: /\.yml$/,
},
{
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
options: {
minimize: true,
modules: true,
importLoaders: 1,
localIdentName: '[name]__[local]___[hash:base64:5]',
},
},
'postcss-loader',
],
}),
test: /.css$/,
},
{
loader: 'file-loader',
test: /\.(jpg|png|gif|svg)$/,
},
{
loader: 'url-loader',
options: { limit: 100000 },
test: /\.woff$/,
},
{
test: /\.(graphql|gql)$/,
exclude,
loader: 'graphql-tag/loader',
},
],
},
plugins: [
new ExtractTextPlugin({
// Use contenthash instead of chunk hash see
// https://survivejs.com/webpack/optimizing/adding-hashes-to-filenames/#setting-up-hashing
filename: getPath => getPath('[name].[contenthash].css'),
}),
new Copy([
...buildEmbeds.map(embed => ({
from: path.join(__dirname, 'client', `coral-embed-${embed}`, 'style'),
to: path.join(__dirname, 'dist', 'embed', embed, '[name].[hash].[ext]'),
})),
]),
autoprefixer,
precss,
new webpack.ProvidePlugin({
fetch:
'imports-loader?this=>global!exports-loader?global.fetch!whatwg-fetch',
}),
new webpack.DefinePlugin({
'process.env': {
VERSION: `"${require('./package.json').version}"`,
NODE_ENV: `${JSON.stringify(process.env.NODE_ENV)}`,
},
}),
new webpack.EnvironmentPlugin({
TALK_PLUGINS_JSON: '{}',
TALK_THREADING_LEVEL: '3',
TALK_ADDTL_COMMENTS_ON_LOAD_MORE: '10',
TALK_ASSET_COMMENTS_LOAD_DEPTH: '10',
TALK_REPLY_COMMENTS_LOAD_DEPTH: '3',
TALK_DEFAULT_STREAM_TAB: 'all',
TALK_DEFAULT_LANG: 'en',
TALK_WHITELISTED_LANGUAGES: '',
}),
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
// We follow this article for stable hashes.
// https://medium.com/webpack/predictable-long-term-caching-with-webpack-d3eee1d3fa31
//
// Chunks without names do not seem to work, so
// we have to make sure they are always named.
// https://github.com/webpack/webpack/tree/master/examples/code-splitting-specify-chunk-name
new webpack.NamedModulesPlugin(),
new webpack.NamedChunksPlugin(),
new NameAllModulesPlugin(),
],
resolveLoader: {
modules: [
'node_modules',
path.resolve(__dirname, 'client/coral-framework/loaders'),
],
},
resolve: {
alias: {
'graphql-anywhere': path.resolve(
__dirname,
'client/coral-framework/graphql/anywhere'
),
'plugin-api': path.resolve(__dirname, 'plugin-api/'),
plugins: path.resolve(__dirname, 'plugins/'),
pluginsConfig: pluginsPath,
lodash: path.resolve(
__dirname,
path.resolve(__dirname, 'node_modules/lodash-es')
),
'lodash.isequal': path.resolve(
__dirname,
'node_modules/lodash-es/isEqual'
),
},
modules: [
path.resolve(__dirname, 'plugins'),
path.resolve(__dirname, 'client'),
...buildTargets.map(target => {
if (typeof target !== 'string') {
target = target.name;
}
return path.join(__dirname, 'client', target, 'src');
}),
...buildEmbeds.map(embed =>
path.join(__dirname, 'client', `coral-embed-${embed}`, 'src')
),
'node_modules',
],
},
};
//==============================================================================
// Production configuration overrides
//==============================================================================
if (process.env.NODE_ENV === 'production') {
config.plugins.push(
// Pulled from https://slack.engineering/keep-webpack-fast-a-field-guide-for-better-build-performance-f56a5995e8f1
new UglifyJsPlugin({
uglifyOptions: {
compress: {
arrows: false,
booleans: false,
collapse_vars: false,
comparisons: false,
computed_props: false,
hoist_funs: false,
hoist_props: false,
hoist_vars: false,
if_return: false,
inline: false,
join_vars: false,
keep_infinity: true,
loops: false,
negate_iife: false,
properties: false,
reduce_funcs: false,
reduce_vars: false,
sequences: false,
side_effects: false,
switches: false,
top_retain: false,
toplevel: false,
typeofs: false,
unused: false,
// Switch off all types of compression except those needed to convince
// react-devtools that we're using a production build
conditionals: true,
dead_code: true,
evaluate: true,
// Remove warnings + discard any console.* functions
warnings: false,
drop_console: true,
},
mangle: true,
},
}),
new CompressionPlugin({
algorithm: 'gzip',
asset: '[path].gz[query]',
test: /\.(js|css)$/,
}),
new CompressionPlugin({
algorithm: 'deflate',
asset: '[path].zz[query]',
test: /\.(js|css)$/,
}),
new BrotliPlugin({
asset: '[path].br[query]',
threshold: 10240,
test: /\.(js|css)$/,
})
);
}
//==============================================================================
// Entries
//==============================================================================
function customizeConcatArrays(objValue, srcValue) {
if (_.isArray(objValue)) {
return objValue.concat(srcValue);
}
}
// Applies the base configuration to the following entries.
const applyConfig = (entries, root = {}) =>
_.mergeWith(
{},
config,
{
entry: entries.reduce(
(entry, { name, path: modulePath, disablePolyfill = false }) => {
const entries = [
path.join(
__dirname,
'client/coral-framework/helpers/webpackGlobals'
),
];
if (disablePolyfill) {
entries.push(modulePath);
} else {
entries.unshift('babel-polyfill');
entries.push(modulePath);
}
entry[name] = entries;
return entry;
},
{}
),
},
root,
customizeConcatArrays
);
// Hack until this issue is resolved https://github.com/webpack-contrib/copy-webpack-plugin/issues/104
const copyWebpackPluginManifestHack = file => {
// Remove hash in manifest key
file.name = file.name.replace(/(\.[a-f0-9]{32})(\..*)$/, '$2');
return file;
};
module.exports = [
// Coral Embed
applyConfig(
[
// Load in the root embed.
{
name: 'embed',
path: path.join(__dirname, 'client/coral-embed/src/index'),
disablePolyfill: process.env.TALK_DISABLE_EMBED_POLYFILL === 'TRUE',
},
],
{
output: {
library: 'Coral',
// don't hash the embed, cache-busting must be completed by the requester
// as this lives in a static template on the embed site.
filename: '[name].js',
},
plugins: [
new ManifestPlugin({
fileName: 'manifest.embed.json',
map: copyWebpackPluginManifestHack,
}),
],
}
),
// All framework targets/embeds/plugins.
applyConfig(
[
// Load in all the targets.
...buildTargets.map(target => {
let disablePolyfill = false;
if (typeof target !== 'string') {
disablePolyfill = target.disablePolyfill;
target = target.name;
}
return {
name: `${target}/bundle`,
path: path.join(__dirname, 'client/', target, '/src/index'),
disablePolyfill,
};
}),
// Load in all the embeds.
...buildEmbeds.map(embed => ({
name: `embed/${embed}/bundle`,
path: path.join(
__dirname,
'client/',
`coral-embed-${embed}`,
'/src/index'
),
})),
// Load in all the plugin entries.
...targetPlugins.reduce((entries, plugin) => {
// Introspect the path to find a targets folder.
let folder = path.dirname(plugin.path);
let files = fs.readdirSync(folder);
// While the folder does not contain the targets folder...
while (!files.includes('targets')) {
// Try to go up a folder.
folder = path.normalize(path.join(folder, '..'));
// And as long as we haven't gone too high
if (
!(
folder.includes(path.join(__dirname, 'node_modules')) ||
!folder.includes(path.join(__dirname, 'plugins'))
)
) {
throw new Error(
`target plugin ${plugin.name} does not have a 'targets' folder`
);
}
files = fs.readdirSync(folder);
}
// List all targets available in that folder.
folder = path.join(folder, 'targets');
let targets = fs.readdirSync(folder);
if (targets.length === 0) {
throw new Error(
`target plugin ${
plugin.name
} has no targets in it's target folder ${folder}`
);
}
return entries.concat(
targets.map(target => ({
name: `plugin/${plugin.name}/${target}/bundle`,
path: path.join(folder, target, 'index'),
}))
);
}, []),
],
{
plugins: [
new ManifestPlugin({
fileName: 'manifest.json',
map: copyWebpackPluginManifestHack,
}),
],
}
),
];