Newer
Older
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
//------------------------------------------------------------------------------
// This app is configured to use typescript.
// See https://webpack.js.org/concepts/ on how to configure this file.
//------------------------------------------------------------------------------
"use-strict";
//------------------------------------------------------------------------------
const path = require("path");
const webpack = require("webpack");
// Configure envirement (NOTE: This must be done as early as pissible)
require("dotenv").config();
//------------------------------------------------------------------------------
const TerserPlugin = require("terser-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const { WebpackManifestPlugin } = require("webpack-manifest-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const ESLintPlugin = require("eslint-webpack-plugin");
const CheckPortPlugin = require("./plugins/check-port");
const BuildFolderWiper = require("./plugins/build-wiper");
const paths = require("./paths");
//------------------------------------------------------------------------------
const checkRequiredFiles = require("react-dev-utils/checkRequiredFiles");
const eslintFormatter = require("react-dev-utils/eslintFormatter");
//------------------------------------------------------------------------------
const requiredFiles = [paths.index_html, paths.index];
if (!checkRequiredFiles(requiredFiles)) {
process.exit(1);
}
//------------------------------------------------------------------------------
const mainTsRejex = /\.(ts|tsx)$/;
const mainCssRejex = /\.(css)$/;
const assetsRejex = /\.(jpg|jpeg|png|gif|mp3|mp4|svg)$/;
//------------------------------------------------------------------------------
module.exports = function (_, webpackEnv) {
const isDevelopment = webpackEnv.mode === "development";
const isProduction = webpackEnv.mode === "production";
const isProfile = process.argv.includes("--profile");
const isProductionProfile = isProduction && isProfile;
const port = process.env.PORT || 8080;
const getStyleLoaders = () => [
isDevelopment ? "style-loader" : MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: {
sourceMap: isDevelopment,
},
},
];
return {
target: "web",
entry: paths.index,
output: {
path: paths.build,
filename: isDevelopment
? "static/js/[name].js"
: isProduction && "static/js/[name].[contenthash:8].js",
chunkFilename: isDevelopment
? "static/js/[name].chunk.js"
: isProduction && "static/js/[name].[contenthash:8].chunk.js",
publicPath: "/",
assetModuleFilename: "assets/[hash][ext]",
},
devtool: isDevelopment ? "source-map" : false,
devServer: {
client: {
logging: "none",
overlay: {
// Show only errors on the browser
errors: true,
warnings: false,
},
},
headers: [
// Security headers
{
key: "Strict-Transport-Security",
value: "max-age=63072000; preload",
},
{
key: "X-XSS-Protection",
value: "1; mode=block",
},
{
key: "X-Frame-Options",
value: "deny",
},
{
key: "X-Content-Type-Options",
value: "nosniff",
},
{
key: "Referrer-Policy",
value: "origin-when-cross-origin",
},
],
historyApiFallback: true, // Nessessary for react router to work
host: "0.0.0.0", // Allow local network access to the server
hot: true,
port,
},
module: {
rules: [
{
test: mainTsRejex,
exclude: /node_modules/,
use: ["ts-loader"],
},
{
test: mainCssRejex,
exclude: /node_modules/,
use: getStyleLoaders(),
},
{
test: assetsRejex,
exclude: [
/node_modules/,
/\.(js|mjs|jsx|ts|tsx)$/,
/\.html$/,
/\.json$/,
],
use: [
{
loader: "file-loader",
options: {
outputPath: "static/media",
name: "[name].[contenthash:8].[ext]",
},
},
],
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/i,
type: "asset/resource",
},
],
},
resolve: {
extensions: ["*", ".js", ".ts", ".tsx"],
modules: [paths.src, paths.modules],
fallback: {
contentBase: paths.build,
events: false,
url: false,
},
},
optimization: {
minimize: isProduction,
minimizer: [
new TerserPlugin({
terserOptions: {
parse: {
ecma: 8,
},
compress: {
ecma: 5,
warnings: false,
comparisons: false,
inline: 2,
},
mangle: {
safari10: true,
},
keep_classnames: isProductionProfile,
keep_fnames: isProductionProfile,
output: {
ecma: 5,
comments: false,
ascii_only: true,
},
sourceMap: false,
},
}),
],
},
plugins: [
new ESLintPlugin({ formatter: eslintFormatter }),
new HtmlWebpackPlugin({
template: paths.index_html,
minify: {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
},
}),
isProduction &&
new MiniCssExtractPlugin({
filename: "static/css/[name].[contenthash:8].css",
chunkFilename: "static/css/[name].[contenthash:8].chunk.css",
}),
new WebpackManifestPlugin({
fileName: "asset-manifest.json",
publicPath: "/",
generate: (seed, files, entrypoints) => {
const manifestFiles = files.reduce((manifest, file) => {
manifest[file.name] = file.path;
return manifest;
}, seed);
const entrypointFiles = entrypoints.main.filter((fileName) => {
return !fileName.endsWith(".map");
});
return {
files: manifestFiles,
entrypoints: entrypointFiles,
};
},
}),
new CopyWebpackPlugin({
patterns: [
{
from: paths.public,
to: ".",
},
],
}),
isDevelopment && new CheckPortPlugin(port),
isProduction && new BuildFolderWiper(paths.build),
new webpack.DefinePlugin({
"process.env.NODE_ENV": JSON.stringify(webpackEnv.mode),
}),
new webpack.EnvironmentPlugin([
// Pass all public env variables here
]),
].filter(Boolean),
};
};