-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.mjs
403 lines (369 loc) · 11.2 KB
/
webpack.config.mjs
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import url from 'url';
import path from 'path';
import BrowserSyncPlugin from 'browser-sync-webpack-plugin';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import WebpackPwaManifest from 'webpack-pwa-manifest';
import InterpolateHtmlPlugin from 'interpolate-html-plugin';
import GA4WebpackPlugin from 'ga4-webpack-plugin';
import packageJson from './package.json' assert { type: 'json' };
import webpack from 'webpack';
import { WebpackManifestPlugin } from 'webpack-manifest-plugin';
import CopyPlugin from 'copy-webpack-plugin';
import HTMLMinifier from 'html-minifier';
import dotenv from 'dotenv';
import ENTRIES from './entries.mjs';
dotenv.config();
const __filename = url.fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
let devMode = process.env['NODE' + '_ENV'] !== 'production';
const CONFIG = {
output: {
name: '[name].[contenthash]',
chunk: '[id].[contenthash]',
dir: 'dist' // Do not include './' or '/'
},
input: {
entry: {}, // Do not fill
dir: 'src'
},
// Changing this during runtime will not going to parse it.
// Restart the webpack to load
env: {},
windowResizeable: false,
// Manifesting and information
// For site.webmanifest
appName: ENTRIES.appName,
shortAppName: ENTRIES.shortAppName,
description: packageJson.description,
colors: {
background: ENTRIES.PWA.background,
theme: ENTRIES.PWA.theme_color // also injected into html
},
icons: {
src: path.resolve('src/assets/icon.png'),
sizes: [96, 128, 256, 512]
},
appType: 'app'
};
/**
* Production Plugins
* */
const prodPlugins = [
new WebpackPwaManifest({
name: CONFIG.appName,
short_name: CONFIG.shortAppName,
description: CONFIG.description,
orientation: 'portrait',
start_url: '.',
display: 'standalone',
background_color: CONFIG.colors.background,
theme_color: CONFIG.colors.theme,
icons: [
{
src: CONFIG.icons.src,
sizes: CONFIG.icons.sizes,
purpose: 'maskable'
}
],
// Asset config
fingerprints: false, // Remove hashed in filename
publicPath: './', // Make sure the url starts with
inject: true, // Insert html tag <link rel="manifest" ... />
filename: 'site.webmanifest'
}),
new WebpackManifestPlugin({
basePath: '',
publicPath: ENTRIES.publicPath,
fileName: 'asset-manifest.json'
})
];
/**
* Handle App Repository Url
* */
const APP_REPOSITORY = packageJson.repository.url.replace(/^git\+/i, '');
export default function (env, config) {
// DANGER!!!
Object.assign(CONFIG.env, process.env);
if (process.env['NODE' + '_ENV'] === void 0) {
// From flag '--mode'
devMode = config.mode !== 'production';
CONFIG.env['NODE_ENV'] = config.mode;
} else {
CONFIG.env['NODE_ENV'] = process.env['NODE' + '_ENV'];
}
console.log('\nDEV MODE: ' + String(devMode) + '\n');
if (devMode) {
CONFIG.output.name = '[name]';
CONFIG.output.chunk = '[id]';
prodPlugins.splice(0, prodPlugins.length);
}
const DateToday = new Date().toISOString().substring(0, 10);
const current_year = new Date().getFullYear();
/**
* This one wasnt efficient since if the port is taken, BrowserSyncPlugin will
* use other port and this one will remain unchanged.
* */
const DYNAMIC_HOMEPAGE_URL = (
devMode
? `http://${ENTRIES.DEV_ADDR.host}:${ENTRIES.DEV_ADDR.port}`
: packageJson.homepage
).replace(/\/+$/gi, ''); // Remove / at the end
CONFIG.env.APP_NAME = CONFIG.appName;
CONFIG.env.APP_SHORT_NAME = CONFIG.shortAppName;
CONFIG.env.APP_DESCRIPTION = CONFIG.description;
CONFIG.env.APP_HOMEPAGE = DYNAMIC_HOMEPAGE_URL;
CONFIG.env.APP_REPOSITORY = APP_REPOSITORY;
CONFIG.env.AUTHOR = packageJson.author;
CONFIG.env.PROJECT_NAME = packageJson.name;
CONFIG.env.BUILD_DATE = DateToday;
CONFIG.env.APP_VERSION = packageJson.version;
const HTMLEntries = ENTRIES.pages.map(({ title, folder, output_folder }) => {
CONFIG.input.entry[title] = './' + path.join(CONFIG.input.dir, folder, 'index.ts');
return new HtmlWebpackPlugin({
title: title,
filename: output_folder === '' ? 'index.html' : `${output_folder}/index.html`,
template: './' + path.join(CONFIG.input.dir, folder, `index.html`),
// manifest: './src/site.webmanifest',
showErrors: devMode, // Include html error on emitted file
lang: 'en',
chunks: [title],
meta: {
'viewport':
'width=device-width, initial-scale=1, shrink-to-fit=no' +
(CONFIG.windowResizeable ? '' : ',user-scalable=no'),
'robots': 'index,follow',
'referrer': 'origin',
'charset': { charset: 'UTF-8' },
'http-equiv': {
'http-equiv': 'Content-Type',
'content': 'text/html; charset=UTF-8'
},
'http-equiv-IE': {
'http-equiv': 'X-UA-Compatible',
'content': 'IE=edge'
},
'color-scheme': 'light dark',
'description': packageJson.description,
// Extended
'version': packageJson.version,
'author': packageJson.author,
'dc.creator': packageJson.author,
'keywords': packageJson.keywords.join(','),
// Open Graph
'og:title': {
property: 'og:title',
content: CONFIG.appName
},
'og:description': {
property: 'og:description',
content: packageJson.description
},
'og:url': {
property: 'og:url',
content: packageJson.homepage
},
'og:type': {
property: 'og:type',
content: 'website'
},
'og:site_name': {
property: 'og:site_name',
content: ENTRIES.SITE_NAME
},
'og:image:url': {
property: 'og:image:url',
content: ENTRIES.PRODUCT_ICON.href
},
'og:image:width': {
property: 'og:image:width',
content: ENTRIES.PRODUCT_ICON.width
},
'og:image:height': {
property: 'og:image:height',
content: ENTRIES.PRODUCT_ICON.height
},
'og:image:alt': {
property: 'og:image:alt',
content: 'Logo'
},
'apple-meta-00': {
name: 'apple-touch-fullscreen',
content: 'no'
},
'apple-meta-01': {
name: 'apple-mobile-web-app-capable',
content: 'yes'
},
'apple-meta-02': {
name: 'apple-mobile-web-app-status-bar-style',
content: 'black-translucent'
},
'apple-meta-03': {
name: 'apple-touch-icon',
content: './favicon.ico'
},
'apple-meta-04': {
name: 'apple-mobile-web-app-title',
content: CONFIG.appName
},
'apple-meta-05': {
name: 'apple-mobile-web-app-capable',
content: 'yes'
},
'tel-meta': {
name: 'format-detection',
content: 'telephone=no'
},
'twitter:card': {
name: 'twitter:card',
content: 'app'
},
'twitter:title': {
name: 'twitter:title',
content: CONFIG.appName
},
'twitter:description': {
name: 'twitter:description',
content: packageJson.description
},
'twitter:image': {
name: 'twitter:image',
content: ENTRIES.PRODUCT_ICON.href
},
'geo.country': {
name: 'geo.country',
content: 'PH'
},
'date': {
name: 'date',
content: DateToday
},
'dcterms.created': {
name: 'dcterms.created',
content: DateToday
},
'dcterms.modified': {
name: 'dcterms.modified',
content: DateToday
}
}
});
});
return {
entry: CONFIG.input.entry,
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/
},
{
test: /\.(jpe?g|png|gif|svg|webp)$/,
type: 'asset/resource'
},
{
test: /\.(wav|mp3|mp4|avi|ogg)$/i,
loader: 'file-loader'
},
{
test: /\.((s[ca]|c)ss)$/,
exclude: /node_modules/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'postcss-loader',
'sass-loader'
]
}
]
},
resolve: {
extensions: ['.ts', '.tsx', '.scss', '.sass', '.css']
},
output: {
filename: CONFIG.output.name + '.js',
chunkFilename: CONFIG.output.chunk + '.js',
path: path.resolve(path.join(__dirname, CONFIG.output.dir)),
clean: true
},
optimization: {
runtimeChunk: 'single'
/* splitChunks: {
chunks: 'all'
} */
},
plugins: [
...HTMLEntries,
new webpack.DefinePlugin({
'process.env': Object.fromEntries(
Object.entries(CONFIG.env).map((x) => [x[0], JSON.stringify(x[1])])
)
}),
new GA4WebpackPlugin({
id: ENTRIES.GA4_MEASUREMENT_ID,
inject: ENTRIES.GA4_MEASUREMENT_ID === false ? false : !devMode, // Only inject in build mode
callPageView: true
}),
new MiniCssExtractPlugin({
filename: CONFIG.output.name + '.css',
chunkFilename: CONFIG.output.chunk + '.css'
}),
new BrowserSyncPlugin({
host: ENTRIES.DEV_ADDR.host,
port: ENTRIES.DEV_ADDR.port,
server: {
baseDir: [CONFIG.output.dir]
},
files: ['./' + CONFIG.output.dir + '/*'],
notify: false,
ui: false, // Web UI for BrowserSyncPlugin
open: false // Open browser after initiation
}),
new InterpolateHtmlPlugin({
CDN: '',
PUBLIC_URL: DYNAMIC_HOMEPAGE_URL,
TITLE: CONFIG.env.APP_NAME,
APP_MODE: devMode ? 'development' : 'production',
BASE_URL: DYNAMIC_HOMEPAGE_URL,
CURRENT_YEAR: current_year,
CURRENT_DATE: DateToday,
APP_TITLE_LENGTH: CONFIG.env.APP_NAME.length,
APP_NAME: CONFIG.env.APP_NAME,
APP_SHORT_NAME: CONFIG.env.APP_SHORT_NAME,
APP_DESCRIPTION: CONFIG.env.APP_DESCRIPTION,
APP_HOMEPAGE: DYNAMIC_HOMEPAGE_URL,
APP_REPOSITORY: CONFIG.env.APP_REPOSITORY,
AUTHOR: CONFIG.env.AUTHOR,
PROJECT_NAME: CONFIG.env.PROJECT_NAME,
BUILD_DATE: CONFIG.env.BUILD_DATE,
APP_VERSION: CONFIG.env.APP_VERSION
}),
new CopyPlugin({
patterns: [
{
from: 'public/',
transform: {
transformer(content, absoluteFrom) {
if (!absoluteFrom.endsWith('.html')) return content;
content = new Buffer(content).toString('utf8');
const minified = HTMLMinifier.minify(content, {
html5: true,
keepClosingSlash: true,
minifyCSS: true,
quoteCharacter: '"',
removeComments: true,
minifyJS: true,
removeTagWhitespace: true,
caseSensitive: true
});
return Buffer.from(minified);
}
}
}
]
})
].concat(prodPlugins)
};
}