-
Notifications
You must be signed in to change notification settings - Fork 187
/
webpack.config.js
155 lines (146 loc) · 5.41 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
// Copyright (C) 2012-present, The Authors. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License, version 3, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
var path = require("path");
var webpack = require("webpack");
var CompressionPlugin = require('compression-webpack-plugin');
var LodashModuleReplacementPlugin = require('lodash-webpack-plugin');
var HtmlWebPackPlugin = require('html-webpack-plugin');
var EventHooksPlugin = require('event-hooks-webpack-plugin');
var CopyPlugin = require("copy-webpack-plugin");
var TerserPlugin = require("terser-webpack-plugin");
var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
var mri = require('mri');
var glob = require('glob');
var fs = require('fs');
// CLI commands for deploying built artefact.
var argv = process.argv.slice(2)
var cliArgs = mri(argv)
var enableTwitterWidgets = process.env.ENABLE_TWITTER_WIDGETS === 'true';
var fbAppId = process.env.FB_APP_ID;
module.exports = (env, options) => {
var isDevBuild = options.mode === 'development';
var isDevServer = process.env.WEBPACK_SERVE;
var chunkHashFragment = (isDevBuild || isDevServer) ? '' : '.[chunkhash:8]';
return {
entry: ["./src/index"],
output: {
publicPath: '/',
filename: `static/js/admin_bundle${chunkHashFragment}.js`,
path: path.resolve(__dirname, "build"),
clean: true,
},
resolve: {
extensions: [".js", ".css", ".png", ".svg"],
},
devServer: {
historyApiFallback: true,
/**
// TODO: Set up API proxy later for server component.
// See: https://webpack.js.org/configuration/dev-server/#devserverproxy
proxy: {
'/api': {
target: 'https://pol.is',
secure: false,
},
},
**/
},
plugins: [
new CopyPlugin({
patterns: [
{ from: 'public', globOptions: { ignore: ['**/index.ejs']}},
],
}),
new HtmlWebPackPlugin({
template: path.resolve( __dirname, 'public/index.ejs' ),
filename: (isDevBuild || isDevServer) ? 'index.html' : 'index_admin.html',
inject: "body",
templateParameters: {
enableTwitterWidgets: enableTwitterWidgets,
fbAppId: fbAppId,
},
}),
new LodashModuleReplacementPlugin({
currying: true,
flattening: true,
paths: true,
placeholders: true,
shorthands: true
}),
new webpack.DefinePlugin({
'process.env.FB_APP_ID': JSON.stringify(fbAppId),
}),
// Only run analyzer when specified in flag.
...(cliArgs.analyze ? [new BundleAnalyzerPlugin({ defaultSizes: 'gzip' })] : []),
// Only compress and create headerJson files during production builds.
...((isDevBuild || isDevServer || cliArgs.analyze) ? [] : [
new CompressionPlugin({
test: /\.js$/,
// Leave unmodified without gz ext.
// See: https://webpack.js.org/plugins/compression-webpack-plugin/#options
filename: '[path][base]',
deleteOriginalAssets: true,
}),
new EventHooksPlugin({
afterEmit: () => {
console.log('Writing *.headersJson files...')
function writeHeadersJson(matchGlob, headersData = {}) {
const files = glob.sync(path.resolve(__dirname, "build", matchGlob))
files.forEach((f, i) => {
const headersFilePath = f + '.headersJson'
fs.writeFileSync(headersFilePath, JSON.stringify(headersData))
})
}
function writeHeadersJsonHtml() {
const headersData = {
'Content-Type': 'text/html; charset=UTF-8',
'Cache-Control': 'no-cache'
}
writeHeadersJson('*.html', headersData)
}
function writeHeadersJsonJs() {
const headersData = {
'Content-Encoding': 'gzip',
'Content-Type': 'application/javascript',
'Cache-Control':
'no-transform,public,max-age=31536000,s-maxage=31536000'
}
writeHeadersJson('static/js/*.js?(.map)', headersData)
}
function writeHeadersJsonMisc() {
writeHeadersJson('favicon.ico')
}
writeHeadersJsonHtml()
writeHeadersJsonJs()
writeHeadersJsonMisc()
}
}),
])
],
optimization: {
minimize: !isDevBuild,
minimizer: [new TerserPlugin()],
},
module: {
rules: [
{
test: /\.m?js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/preset-react'],
},
},
},
{
test: /\.(png|jpg|gif|svg)$/,
loader: 'file-loader',
},
{
test: /\.mdx?$/,
use: ['babel-loader', '@mdx-js/loader']
}
],
},
};
}