From a8181152b7f9594bfdb4d9fdce83f1b27841f498 Mon Sep 17 00:00:00 2001 From: Alex Vandiver Date: Fri, 3 Mar 2023 21:38:01 +0000 Subject: [PATCH] webpack: Build a ZULIP_VERSION global constant into the built product. --- .eslintrc.json | 3 +++ tools/webpack | 12 +++++++++++- web/src/global.d.ts | 2 ++ web/webpack.config.ts | 9 ++++++++- 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index b493cd93fd..a2753f03ab 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -237,6 +237,9 @@ "browser": true, "node": false }, + "globals": { + "ZULIP_VERSION": false + }, "rules": { "no-console": "error" }, diff --git a/tools/webpack b/tools/webpack index 8e8beb6516..ca9704dceb 100755 --- a/tools/webpack +++ b/tools/webpack @@ -3,11 +3,17 @@ import argparse import json import os import subprocess +import sys from typing import NoReturn +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) + os.chdir(os.path.join(os.path.dirname(__file__), "../web")) +from version import ZULIP_VERSION + + def build_for_prod_or_puppeteer(quiet: bool) -> NoReturn: """Builds for production, writing the output to disk""" @@ -15,7 +21,11 @@ def build_for_prod_or_puppeteer(quiet: bool) -> NoReturn: with open("/proc/meminfo") as meminfo: if int(next(meminfo).split()[1]) < 3 * 1024 * 1024: webpack_args += ["--max-old-space-size=1536"] - webpack_args += ["../node_modules/.bin/webpack-cli", "--mode=production"] + webpack_args += [ + "../node_modules/.bin/webpack-cli", + "--mode=production", + f"--env=ZULIP_VERSION={ZULIP_VERSION}", + ] if quiet: webpack_args += ["--stats=errors-only"] os.execvp(webpack_args[0], webpack_args) diff --git a/web/src/global.d.ts b/web/src/global.d.ts index b1fb6e68a3..5bdd7711fb 100644 --- a/web/src/global.d.ts +++ b/web/src/global.d.ts @@ -9,3 +9,5 @@ interface JQuery { expectOne(): JQuery; tab(action?: string): this; // From web/third/bootstrap } + +declare const ZULIP_VERSION: string; diff --git a/web/webpack.config.ts b/web/webpack.config.ts index 1f9a657fbd..1577a53662 100644 --- a/web/webpack.config.ts +++ b/web/webpack.config.ts @@ -5,6 +5,7 @@ import path from "path"; import CssMinimizerPlugin from "css-minimizer-webpack-plugin"; import HtmlWebpackPlugin from "html-webpack-plugin"; import MiniCssExtractPlugin from "mini-css-extract-plugin"; +import {DefinePlugin} from "webpack"; import type webpack from "webpack"; import BundleTracker from "webpack-bundle-tracker"; @@ -12,7 +13,10 @@ import DebugRequirePlugin from "./debug-require-webpack-plugin"; import assets from "./webpack.assets.json"; import dev_assets from "./webpack.dev-assets.json"; -export default (env: {minimize?: boolean} = {}, argv: {mode?: string}): webpack.Configuration[] => { +export default ( + env: {minimize?: boolean; ZULIP_VERSION?: string} = {}, + argv: {mode?: string}, +): webpack.Configuration[] => { const production: boolean = argv.mode === "production"; const baseConfig: webpack.Configuration = { @@ -194,6 +198,9 @@ export default (env: {minimize?: boolean} = {}, argv: {mode?: string}): webpack. }, }, plugins: [ + new DefinePlugin({ + ZULIP_VERSION: JSON.stringify(env.ZULIP_VERSION || "development"), + }), new DebugRequirePlugin(), new BundleTracker({ filename: production