forked from wevote/WebApp
-
Notifications
You must be signed in to change notification settings - Fork 3
/
webpack.config.js
183 lines (181 loc) · 5.58 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
/* 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 RemovePlugin = require('remove-files-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 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' || 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}`);
}
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,
},
devtool: false,
plugins: [
new CleanWebpackPlugin(),
new ESLintPlugin({ failOnError: false, failOnWarning: false }),
new HtmlWebpackPlugin({
title: 'We Vote Web App',
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*.*',
'/sass/',
'/robots.txt',
'srcDeprecated',
],
root: __dirname,
}),
new BundleAnalyzerPlugin(),
] : []),
new CopyPlugin({
patterns: [
{ from: `${source}/robots.txt`, to: '.' },
{ from: `${source}/css/`, to: 'css/' },
{
from: `${source}/img`,
to: 'img/',
globOptions: { ignore: ['DO-NOT-BUNDLE/**/*']},
},
],
}),
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/],
}),
]),
...(isWebApp ? [
new RemovePlugin({
before: {
include: [
'./srcCordova',
],
log: true,
logWarning: false,
// logError: true,
// logDebug: true,
},
}),
] : []),
],
devServer: {
static: {
directory: path.join(__dirname, './build'),
},
host: 'localhost',
port,
historyApiFallback: true,
// open: true,
...(isHTTPS ? {
server: {
type: 'https',
options: {
key: fs.readFileSync(`./${source}/cert/server.key`),
cert: fs.readFileSync(`./${source}/cert/server.crt`),
},
},
} : {}),
},
});