1 const webpack = require("webpack");
2 const { resolve } = require("path");
3 const MiniCssExtractPlugin = require("mini-css-extract-plugin");
4 const nodeExternals = require("webpack-node-externals");
5 const CopyPlugin = require("copy-webpack-plugin");
6 const RunNodeWebpackPlugin = require("run-node-webpack-plugin");
7 const { ServiceWorkerPlugin } = require("service-worker-webpack");
10 hash:[contentHash], chunkhash:[chunkhash], name:[name], filebase:[base], query:[query], file:[file]
11 Source code: https://github.com/LemmyNet/lemmy-ui
13 @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL v3.0
16 module.exports = (env, argv) => {
17 const mode = argv.mode;
21 filename: "js/server.js",
23 hashFunction: "xxhash64",
26 extensions: [".js", ".jsx", ".ts", ".tsx"],
28 "@": resolve(__dirname, "src/"),
29 "@utils": resolve(__dirname, "src/shared/utils/"),
38 test: /\.(scss|css)$/i,
39 use: [MiniCssExtractPlugin.loader, "css-loader", "sass-loader"],
42 test: /\.(js|jsx|tsx|ts)$/, // All ts and tsx files will be process by
43 exclude: /node_modules/, // ignore node_modules
44 loader: "babel-loader",
46 // Due to some weird babel issue: https://github.com/webpack/webpack/issues/11467
50 fullySpecified: false,
56 new webpack.DefinePlugin({
57 "process.env.COMMIT_HASH": `"${env.COMMIT_HASH}"`,
58 "process.env.NODE_ENV": `"${mode}"`,
60 new MiniCssExtractPlugin({
61 filename: "styles/styles.css",
64 patterns: [{ from: "./src/assets", to: "./assets" }],
66 new webpack.BannerPlugin({
72 const serverConfig = {
74 entry: "./src/server/index.tsx",
76 filename: "js/server.js",
79 externals: [nodeExternals(), "inferno-helmet"],
82 const clientConfig = {
84 entry: "./src/client/index.tsx",
86 filename: "js/client.js",
87 publicPath: `/static/${env.COMMIT_HASH}/`,
91 new ServiceWorkerPlugin({
92 enableInDevelopment: mode !== "development", // this may seem counterintuitive, but it is correct
95 include: [/(assets|styles|js)\/.+\..+$/g],
96 inlineWorkboxRuntime: true,
101 url: { pathname, host },
104 (sameOrigin || host.includes("localhost")) &&
106 pathname.includes("pictrs") || pathname.includes("static")
109 handler: "NetworkFirst",
111 cacheName: "instance-cache",
115 urlPattern: ({ url: { pathname, host }, sameOrigin }) =>
116 (sameOrigin || host.includes("localhost")) &&
117 pathname.includes("static"),
118 handler: mode === "development" ? "NetworkFirst" : "CacheFirst",
120 cacheName: "static-cache",
122 maxAgeSeconds: 60 * 60 * 24,
127 urlPattern: ({ url: { pathname }, request: { method } }) =>
128 pathname.includes("pictrs") && method === "GET",
129 handler: "StaleWhileRevalidate",
131 cacheName: "image-cache",
133 maxAgeSeconds: 60 * 60 * 24,
143 if (mode === "development") {
144 // serverConfig.cache = {
145 // type: "filesystem",
149 serverConfig.plugins.push(
150 new RunNodeWebpackPlugin({ runOnlyInWatchMode: true })
152 } else if (mode === "none") {
153 const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
154 serverConfig.plugins.push(new BundleAnalyzerPlugin());
157 return [serverConfig, clientConfig];