-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
221 lines (189 loc) · 5.94 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
/* eslint-disable max-len */
const path = require('path');
const childProcess = require('child_process');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const CopyWebpackPlugin = require('copy-webpack-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const ESLintPlugin = require('eslint-webpack-plugin');
const hasha = require('hasha');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const StyleLintPlugin = require('stylelint-webpack-plugin');
const webpack = require('webpack');
const WorkboxWebpackPlugin = require('workbox-webpack-plugin');
const pkg = require('./package.json');
module.exports = (env, {
mode,
}) => {
const DEV = /development|dev/i.test(mode);
const PROD = /production|prod/i.test(mode);
const COMMIT_HASH = childProcess.execSync('git rev-parse HEAD').toString().trim();
const BUILD_DATE = new Date();
const config = {
devtool: DEV ? 'eval-source-map' : 'source-map',
entry: {
main: [
'./src/app/main.js',
'./src/app/main.scss',
],
},
output: {
filename: PROD ? '[name].[contenthash].js' : '[name].[hash].js',
path: path.resolve(__dirname, 'dist'),
publicPath: './',
hashFunction: 'xxhash64',
},
devServer: {
watchFiles: [
'src/**/*.scss',
'src/**/*.ejs',
],
static: {
directory: path.resolve(__dirname, 'static'),
},
devMiddleware: {
publicPath: '/',
// When sharing the site using ssh -R 80:localhost:8080 ssh.localhost.run
// disableHostCheck: true,
},
client: {
overlay: {
warnings: false,
errors: false,
},
},
},
module: {
rules: [{
test: /\.js$/,
exclude: /node_modules/,
use: [{
loader: 'babel-loader',
}],
}, {
test: /\.scss/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'postcss-loader',
'sass-loader',
],
}, {
test: /\.ejs$/,
exclude: /node_modules/,
use: {
loader: 'ejs-compiled-loader',
},
}],
},
plugins: [
new ESLintPlugin({ fix: true }),
new HtmlWebpackPlugin({
filename: path.resolve(__dirname, 'dist/index.html'),
template: path.resolve(__dirname, 'src/app/components/app/app.template.ejs'),
title: 'GMZcodes - Dani Gámez Franco',
description: 'Dani Gámez Franco - Lead Web3 Full-Stack Software Engineer - Summa Cum Laude Computer Science Engineer - Top 1.5% at StackOverflow - JS, TS, React, Next.js, Node.js, tRPC, DynamoDB, Clean Software Architecture',
favicon: path.resolve(__dirname, 'static/favicon.ico'),
inlineSource: '.(js|css)$', // Inline JS and CSS.
minify: PROD ? {
removeComments: false,
} : false,
meta: {
author: pkg.author.name,
description: pkg.description,
},
// inlineSource: '.css$', // Inline JS and CSS.
}),
new MiniCssExtractPlugin({
filename: PROD ? '[name].[contenthash].css' : '[name].[hash].css',
}),
new StyleLintPlugin({
fix: true,
}),
new CopyWebpackPlugin({
patterns: [{
from: 'static',
}],
}),
// Defines variables available globally that Webpack can evaluate in compilation time and remove dead code:
// new webpack.DefinePlugin({}),
// Same as before, but sets properties inside `process.env` specifically:
new webpack.EnvironmentPlugin({
DEV,
PROD,
BUILD_DATE,
COMMIT_HASH,
}),
// new BundleAnalyzerPlugin(),
],
optimization: {
minimize: true,
// Extract all styles in a single file:
splitChunks: {
cacheGroups: {
styles: {
name: 'styles',
test: /\.css$/,
chunks: 'all',
enforce: true,
},
},
},
minimizer: PROD ? [
'...',
new CssMinimizerPlugin(),
] : [],
},
};
if (PROD) {
config.plugins.push(
new WorkboxWebpackPlugin.GenerateSW({
cleanupOutdatedCaches: true,
// Do not precache images or fonts
exclude: [/\.(?:png|jpg|jpeg|gif|svg|woff2)$/],
additionalManifestEntries: [{
url: 'manifest.json',
revision: hasha.fromFileSync('./static/manifest.json'),
}, {
url: 'dani-gamez-franco-cv-2024.05.03.pdf',
revision: hasha.fromFileSync('./static/dani-gamez-franco-cv-2024.05.03.pdf'),
}],
// Define runtime caching rules:
runtimeCaching: [{
urlPattern: /\.(?:png|jpg|jpeg|gif|svg|woff2)$/,
handler: 'CacheFirst',
options: {
cacheName: 'images',
expiration: {
maxEntries: 16,
maxAgeSeconds: 30 * 24 * 60 * 60, // 30 Days
},
},
}, {
urlPattern: /^https:\/\/fonts\.googleapis\.com/,
handler: 'StaleWhileRevalidate',
options: {
cacheName: 'google-fonts-stylesheets',
expiration: {
maxEntries: 4,
},
},
}, {
urlPattern: /^https:\/\/fonts\.gstatic\.com/,
handler: 'CacheFirst',
options: {
cacheName: 'google-fonts-webfonts',
cacheableResponse: {
statuses: [0, 200],
},
expiration: {
maxEntries: 30,
maxAgeSeconds: 60 * 60 * 24 * 365, // 1 year
},
},
}],
}),
);
}
return config;
};