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 { ServiceWorkerPlugin } = require("service-worker-webpack");
9 hash:[contentHash], chunkhash:[chunkhash], name:[name], filebase:[base], query:[query], file:[file]
10 Source code: https://github.com/LemmyNet/lemmy-ui
12 @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL v3.0
15 module.exports = (env, argv) => {
16 const mode = argv.mode;
20 hashFunction: "xxhash64",
23 extensions: [".js", ".jsx", ".ts", ".tsx"],
25 "@": resolve(__dirname, "src/"),
26 "@utils": resolve(__dirname, "src/shared/utils/"),
35 test: /\.(scss|css)$/i,
36 use: [MiniCssExtractPlugin.loader, "css-loader", "sass-loader"],
39 test: /\.(js|jsx|tsx|ts)$/, // All ts and tsx files will be process by
40 exclude: /node_modules/, // ignore node_modules
41 loader: "babel-loader",
43 // Due to some weird babel issue: https://github.com/webpack/webpack/issues/11467
47 fullySpecified: false,
53 new webpack.DefinePlugin({
54 "process.env.COMMIT_HASH": `"${env.COMMIT_HASH}"`,
55 "process.env.NODE_ENV": `"${mode}"`,
57 new MiniCssExtractPlugin({
58 filename: "styles/styles.css",
61 patterns: [{ from: "./src/assets", to: "./assets" }],
63 new webpack.BannerPlugin({
69 const serverConfig = {
71 entry: "./src/server/index.tsx",
74 filename: "js/server.js",
78 externals: [nodeExternals(), "inferno-helmet"],
81 const clientConfig = {
83 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 const RunNodeWebpackPlugin = require("run-node-webpack-plugin");
150 serverConfig.plugins.push(
151 new RunNodeWebpackPlugin({ runOnlyInWatchMode: true })
153 } else if (mode === "none") {
154 const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
155 serverConfig.plugins.push(new BundleAnalyzerPlugin());
158 return [serverConfig, clientConfig];