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) {
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}"`,
58 new MiniCssExtractPlugin({
59 filename: "styles/styles.css",
62 patterns: [{ from: "./src/assets", to: "./assets" }],
64 new webpack.BannerPlugin({
71 const createServerConfig = (env, mode) => {
72 const base = getBase(env);
73 const config = merge({}, base, {
75 entry: "./src/server/index.tsx",
77 filename: "js/server.js",
80 externals: [nodeExternals(), "inferno-helmet"],
83 if (mode === "development") {
85 // type: "filesystem",
90 new RunNodeWebpackPlugin({
91 runOnlyInWatchMode: true,
99 const createClientConfig = (env, mode) => {
100 const base = getBase(env);
101 const config = merge({}, base, {
103 entry: "./src/client/index.tsx",
105 filename: "js/client.js",
106 publicPath: `/static/${env.COMMIT_HASH}/`,
110 new ServiceWorkerPlugin({
111 enableInDevelopment: mode !== "development", // this may seem counterintuitive, but it is correct
114 "/": `/static/${env.COMMIT_HASH}/`,
117 include: [/(assets|styles|js)\/.+\..+$/g],
118 inlineWorkboxRuntime: true,
123 url: { pathname, host },
126 (sameOrigin || host.includes("localhost")) &&
128 pathname.includes("pictrs") || pathname.includes("static")
131 handler: "NetworkFirst",
133 cacheName: "instance-cache",
137 urlPattern: ({ url: { pathname, host }, sameOrigin }) =>
138 (sameOrigin || host.includes("localhost")) &&
139 pathname.includes("static"),
140 handler: mode === "development" ? "NetworkFirst" : "CacheFirst",
142 cacheName: "static-cache",
144 maxAgeSeconds: 60 * 60 * 24,
149 urlPattern: ({ url: { pathname }, request: { method } }) =>
150 pathname.includes("pictrs") && method === "GET",
151 handler: "StaleWhileRevalidate",
153 cacheName: "image-cache",
155 maxAgeSeconds: 60 * 60 * 24,
165 if (mode === "none") {
166 const BundleAnalyzerPlugin =
167 require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
168 config.plugins.push(new BundleAnalyzerPlugin());
174 module.exports = (env, properties) => [
175 createServerConfig(env, properties.mode || "development"),
176 createClientConfig(env, properties.mode || "development"),