2022-06-20 18:43:27 +08:00
|
|
|
const { resolve } = require("path");
|
2022-03-17 03:57:30 +08:00
|
|
|
const { merge } = require("webpack-merge");
|
2022-06-22 18:11:14 +08:00
|
|
|
const RemovePlugin = require("remove-files-webpack-plugin");
|
2022-03-18 22:40:40 +08:00
|
|
|
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
|
2022-03-18 04:00:20 +08:00
|
|
|
const HtmlMinimizerPlugin = require("html-minimizer-webpack-plugin");
|
2022-03-18 22:40:40 +08:00
|
|
|
const JsonMinimizerPlugin = require("json-minimizer-webpack-plugin");
|
2022-10-13 20:26:49 +08:00
|
|
|
const WorkboxPlugin = require("workbox-webpack-plugin");
|
2022-03-18 04:00:20 +08:00
|
|
|
|
2022-03-22 04:22:47 +08:00
|
|
|
const BASE_CONFIG = require("./config.base");
|
2022-03-17 03:57:30 +08:00
|
|
|
|
2022-03-17 07:18:29 +08:00
|
|
|
function pad(numbers, maxLength, fillString) {
|
|
|
|
return numbers.map((number) =>
|
|
|
|
number.toString().padStart(maxLength, fillString)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-03 00:49:13 +08:00
|
|
|
const { COMMIT_HASH = "NO_HASH" } = process.env;
|
|
|
|
|
2022-03-22 04:22:47 +08:00
|
|
|
/** @type { import('webpack').Configuration } */
|
|
|
|
const PRODUCTION_CONFIG = {
|
2022-03-17 03:57:30 +08:00
|
|
|
mode: "production",
|
|
|
|
module: {
|
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
test: /version\.ts$/,
|
|
|
|
loader: "string-replace-loader",
|
|
|
|
options: {
|
|
|
|
search: /^export const CLIENT_VERSION =.*/,
|
2022-03-19 19:33:25 +08:00
|
|
|
replace() {
|
2022-03-17 03:57:30 +08:00
|
|
|
const date = new Date();
|
2022-03-17 07:18:29 +08:00
|
|
|
|
|
|
|
const versionPrefix = pad(
|
|
|
|
[date.getFullYear(), date.getMonth() + 1, date.getDate()],
|
|
|
|
2,
|
|
|
|
"0"
|
|
|
|
).join(".");
|
|
|
|
const versionSuffix = pad(
|
|
|
|
[date.getHours(), date.getMinutes()],
|
|
|
|
2,
|
|
|
|
"0"
|
|
|
|
).join(".");
|
|
|
|
const version = [versionPrefix, versionSuffix].join("_");
|
|
|
|
|
2022-07-03 00:49:13 +08:00
|
|
|
return `export const CLIENT_VERSION = "${version}.${COMMIT_HASH}";`;
|
2022-03-17 03:57:30 +08:00
|
|
|
},
|
|
|
|
flags: "g",
|
|
|
|
},
|
|
|
|
},
|
2022-03-19 19:33:25 +08:00
|
|
|
{
|
|
|
|
test: /firebase\.ts$/,
|
|
|
|
loader: "string-replace-loader",
|
|
|
|
options: {
|
|
|
|
search: /\.\/constants\/firebase-config/,
|
|
|
|
replace() {
|
|
|
|
return "./constants/firebase-config-live";
|
|
|
|
},
|
|
|
|
flags: "g",
|
|
|
|
},
|
|
|
|
},
|
2022-03-17 03:57:30 +08:00
|
|
|
],
|
|
|
|
},
|
2022-03-18 04:00:20 +08:00
|
|
|
optimization: {
|
|
|
|
minimize: true,
|
2022-03-18 22:40:40 +08:00
|
|
|
minimizer: [
|
|
|
|
`...`,
|
|
|
|
new HtmlMinimizerPlugin(),
|
|
|
|
new JsonMinimizerPlugin(),
|
|
|
|
new CssMinimizerPlugin(),
|
|
|
|
],
|
2022-03-18 04:00:20 +08:00
|
|
|
},
|
2022-06-22 18:11:14 +08:00
|
|
|
plugins: [
|
|
|
|
new RemovePlugin({
|
|
|
|
after: {
|
|
|
|
include: [resolve(__dirname, "../public/html")],
|
|
|
|
},
|
|
|
|
}),
|
2022-10-13 20:26:49 +08:00
|
|
|
new WorkboxPlugin.GenerateSW({
|
|
|
|
// these options encourage the ServiceWorkers to get in there fast
|
|
|
|
// and not allow any straggling "old" SWs to hang around
|
|
|
|
clientsClaim: true,
|
|
|
|
skipWaiting: false,
|
|
|
|
//include the generated css and js files
|
|
|
|
maximumFileSizeToCacheInBytes: 11000000,
|
2022-10-14 16:59:07 +08:00
|
|
|
exclude: [
|
2022-10-14 17:01:15 +08:00
|
|
|
/html\/.*\.html/,
|
2022-10-14 16:59:07 +08:00
|
|
|
/LICENSE\.txt/,
|
|
|
|
/\.DS_Store/,
|
|
|
|
/\.map$/,
|
|
|
|
/^manifest.*\.js$/,
|
2022-11-03 22:31:37 +08:00
|
|
|
/languages\/.*\.json/,
|
|
|
|
/quotes\/.*\.json/,
|
|
|
|
/themes\/.*\.css/,
|
|
|
|
/challenges\/.*\.txt/,
|
|
|
|
/sound\/.*\.wav/,
|
|
|
|
/images\/.*\.(png|jpg)/,
|
|
|
|
/webfonts\/.+/,
|
|
|
|
],
|
|
|
|
runtimeCaching: [
|
|
|
|
{
|
|
|
|
urlPattern: /languages\/.*\.json/,
|
|
|
|
handler: "StaleWhileRevalidate",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
urlPattern: /quotes\/.*\.json/,
|
|
|
|
handler: "StaleWhileRevalidate",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
urlPattern: /themes\/.*\.css/,
|
|
|
|
handler: "StaleWhileRevalidate",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
urlPattern: /challenges\/.*\.txt/,
|
|
|
|
handler: "StaleWhileRevalidate",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
urlPattern: /sound\/.*\.wav/,
|
|
|
|
handler: "StaleWhileRevalidate",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
urlPattern: /images\/.*\.(png|jpg)/,
|
|
|
|
handler: "StaleWhileRevalidate",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
urlPattern: /webfonts\/.+/,
|
|
|
|
handler: "CacheFirst",
|
|
|
|
},
|
2022-10-14 16:59:07 +08:00
|
|
|
],
|
2022-10-13 20:26:49 +08:00
|
|
|
}),
|
2022-06-22 18:11:14 +08:00
|
|
|
],
|
2022-03-17 03:57:30 +08:00
|
|
|
};
|
|
|
|
|
2022-03-22 04:22:47 +08:00
|
|
|
module.exports = merge(BASE_CONFIG, PRODUCTION_CONFIG);
|