1 const webpack = require("webpack");
2 const path = 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 merge = require("lodash.merge");
8 const { ServiceWorkerPlugin } = require("service-worker-webpack");
11 hash:[contentHash], chunkhash:[chunkhash], name:[name], filebase:[base], query:[query], file:[file]
12 Source code: https://github.com/LemmyNet/lemmy-ui
14 @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL v3.0
17 function getBase(env, mode) {
20 filename: "js/server.js",
22 hashFunction: "xxhash64",
25 extensions: [".js", ".jsx", ".ts", ".tsx"],
27 "@": path.resolve(__dirname, "src/"),
28 "@utils": path.resolve(__dirname, "src/shared/utils/"),
37 test: /\.(scss|css)$/i,
38 use: [MiniCssExtractPlugin.loader, "css-loader", "sass-loader"],
41 test: /\.(js|jsx|tsx|ts)$/, // All ts and tsx files will be process by
42 exclude: /node_modules/, // ignore node_modules
43 loader: "babel-loader",
45 // Due to some weird babel issue: https://github.com/webpack/webpack/issues/11467
49 fullySpecified: false,
55 new webpack.DefinePlugin({
56 "process.env.COMMIT_HASH": `"${env.COMMIT_HASH}"`,
57 "process.env.NODE_ENV": `"${mode}"`,
59 new MiniCssExtractPlugin({
60 filename: "styles/styles.css",
63 patterns: [{ from: "./src/assets", to: "./assets" }],
65 new webpack.BannerPlugin({
72 const createServerConfig = (env, mode) => {
73 const base = getBase(env, mode);
74 const config = merge({}, base, {
76 entry: "./src/server/index.tsx",
78 filename: "js/server.js",
81 externals: [nodeExternals(), "inferno-helmet"],
84 if (mode === "development") {
86 // type: "filesystem",
91 new RunNodeWebpackPlugin({
92 runOnlyInWatchMode: true,
100 const createClientConfig = (env, mode) => {
101 const base = getBase(env, mode);
102 const config = merge({}, base, {
104 entry: "./src/client/index.tsx",
106 filename: "js/client.js",
107 publicPath: `/static/${env.COMMIT_HASH}/`,
111 new ServiceWorkerPlugin({
112 enableInDevelopment: mode !== "development", // this may seem counterintuitive, but it is correct
115 include: [/(assets|styles|js)\/.+\..+$/g],
116 inlineWorkboxRuntime: true,
121 url: { pathname, host },
124 (sameOrigin || host.includes("localhost")) &&
126 pathname.includes("pictrs") || pathname.includes("static")
129 handler: "NetworkFirst",
131 cacheName: "instance-cache",
135 urlPattern: ({ url: { pathname, host }, sameOrigin }) =>
136 (sameOrigin || host.includes("localhost")) &&
137 pathname.includes("static"),
138 handler: mode === "development" ? "NetworkFirst" : "CacheFirst",
140 cacheName: "static-cache",
142 maxAgeSeconds: 60 * 60 * 24,
147 urlPattern: ({ url: { pathname }, request: { method } }) =>
148 pathname.includes("pictrs") && method === "GET",
149 handler: "StaleWhileRevalidate",
151 cacheName: "image-cache",
153 maxAgeSeconds: 60 * 60 * 24,
163 if (mode === "none") {
164 const BundleAnalyzerPlugin =
165 require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
166 config.plugins.push(new BundleAnalyzerPlugin());
172 module.exports = (env, properties) => [
173 createServerConfig(env, properties.mode || "development"),
174 createClientConfig(env, properties.mode || "development"),