-
Notifications
You must be signed in to change notification settings - Fork 312
/
webpack.config.js
205 lines (203 loc) · 6.8 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
/* jshint esversion: 6 */
const CopyPlugin = require('copy-webpack-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const ESLintPlugin = require('eslint-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MomentLocalesPlugin = require('moment-locales-webpack-plugin');
const SourceMapDevToolPlugin = require('webpack/lib/SourceMapDevToolPlugin');
const TerserPlugin = require('terser-webpack-plugin');
const UnusedWebpackPlugin = require('unused-webpack-plugin');
const WebpackShellPluginNext = require('webpack-shell-plugin-next');
const fs = require('fs');
const path = require('path');
const webpack = require('webpack');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const port = process.env.PORT || 3000;
const isHTTPS = process.env.PROTOCOL && process.env.PROTOCOL === 'HTTPS';
const isWebApp = !process.env.npm_lifecycle_script.includes('CORDOVA=1');
const useRealCerts = process.env.npm_lifecycle_script.includes('USE_REAL_CERTS=1');
const isProduction = process.env.npm_lifecycle_script.includes('PRODUCTION=1');
const source = isWebApp ? 'src' : 'srcCordova';
const bundleAnalysis = process.env.ANALYSIS || false; // enable the interactive bundle analyser and the Unused component analyzer
const minimized = process.env.MINIMIZED === '1' || false; // enable the Terser plugin that strips comments and shrinks long variable names
const verBits = process.version.split('.');
const major = parseInt(verBits[0].replace('v', ''));
if (major < 13) {
console.error(`The minimum Node version is: v14.0.0, but you are running ${process.version}\n`);
} else {
console.log(`Node version is: ${process.version}`);
}
if (useRealCerts) console.log('useRealCerts in webpack.config.js ', useRealCerts);
// console.log('key: ', fs.readFileSync(`./${source}/cert/wevotedeveloper.com.crt`).toString());
module.exports = (env, argv) => ({
entry: path.resolve(__dirname, `./${source}/index.jsx`),
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules|srcDeprecated/,
use: ['babel-loader'],
},
{
test: /\.(png|jp(e*)g|svg|eot|woff|ttf)$/,
use: [
{
loader: 'file-loader',
options: {
publicPath: '/',
exclude: /srcDeprecated/,
name: '[path][name].[ext]',
},
},
],
},
],
},
optimization: {
minimize: minimized,
minimizer: [
...(minimized ? [
new TerserPlugin({
parallel: true,
terserOptions: {
// https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions
format: {
comments: false,
},
},
}),
] : []),
new CssMinimizerPlugin({
test: /\.css$/i,
minimizerOptions: {
preset: [
'default',
{
discardComments: { removeAll: true },
},
],
},
}),
],
},
resolve: {
modules: [path.resolve(__dirname, source), 'node_modules'],
extensions: ['*', '.js', '.jsx'],
alias: {
'@mui/styled-engine': '@mui/styled-engine-sc',
},
},
output: {
path: path.resolve(__dirname, './build'),
filename: isWebApp ? '[name].[contenthash].js' : 'bundle.js',
publicPath: isWebApp ? '/' : undefined,
},
// source-map is for OpenReplay
devtool: 'source-map',
plugins: [
new CleanWebpackPlugin(),
new ESLintPlugin({ failOnError: false, failOnWarning: false }),
new HtmlWebpackPlugin({
title: 'WeVote Sample Ballot',
template: path.resolve(__dirname, `./${source}/index.html`),
}),
...(bundleAnalysis ? [
new UnusedWebpackPlugin({ // Set ANALYSIS to true to list (likely) unused files
directories: [path.join(__dirname, source)],
exclude: [
'/**/cert/',
'/**/global/svg-icons/',
'/*.test.js',
'/**/config*.*',
'extension.html',
'/sass/',
'/robots.txt',
'/app-ads.txt',
'srcDeprecated',
],
root: __dirname,
}),
new BundleAnalyzerPlugin(),
] : []),
new CopyPlugin({
patterns: [
{ from: `${source}/robots.txt`, to: '.' },
{ from: `${source}/app-ads.txt`, to: '.' },
{
from: `${source}/css/`,
to: 'css/',
globOptions: { ignore: ['**/mainPreBeautified.css', '**/mainSubtract.css']},
},
{ from: `${source}/javascript`, to: 'javascript/' },
{ from: `${source}/extension.html`, to: '.' },
{
from: `${source}/img`,
to: 'img/',
globOptions: { ignore: ['**/DO-NOT-BUNDLE/**']},
},
...(isProduction ? [
{ from: 'node/STORYBOOK-README.TXT', to: './storybook-static/STORYBOOK-README.TXT' },
{ from: 'storybook-static', to: './storybook-static' },
] : []),
],
}),
new MomentLocalesPlugin(),
new WebpackShellPluginNext({
onBuildEnd: {
scripts: ['node ./src/js/common/node/webPackPostBuild.js'],
blocking: false,
parallel: true,
},
}),
...(argv.mode === 'production' ? [
new webpack.DefinePlugin({
// We need to get webpack into production mode, to make it include the much smaller minimized libraries
// especially for React itself.
// PRODUCTION: JSON.stringify(true),
'process.env.NODE_ENV': JSON.stringify('production'),
}),
] : [
new SourceMapDevToolPlugin({
filename: isWebApp ? null : '[file].map', // if no value is provided the sourcemap is inlined
exclude: [/node_modules/, /css/],
}),
]),
],
devServer: {
allowedHosts: ['wevotedeveloper.com', 'localhost'],
static: {
directory: path.join(__dirname, './build/index.html'),
},
host: (useRealCerts ? 'wevotedeveloper.com' : 'localhost'),
port,
historyApiFallback: true,
...(isHTTPS ? {
server: {
type: 'https',
options: {
...(useRealCerts ? {
// For testing with Cordova and real authoritative certs
key: fs.readFileSync(`./${source}/cert/wevotedeveloper.com_key.txt`),
cert: fs.readFileSync(`./${source}/cert/wevotedeveloper.com.crt`),
// requestCert: true,
// passphrase: 'webpack-dev-server',
} : {
key: fs.readFileSync(`./${source}/cert/server.key`),
cert: fs.readFileSync(`./${source}/cert/server.crt`),
}),
},
},
} : {}),
client: {
overlay: {
runtimeErrors: (error) => {
if (error.message.includes('ResizeObserver loop')) {
return false;
}
return true;
},
},
},
},
});