Make hash docker agnostic

This commit is contained in:
SleeplessOne1917 2023-06-29 09:25:08 -04:00
parent c7ea1b7c56
commit 753953ad3e
10 changed files with 75 additions and 72 deletions

View file

@ -24,7 +24,7 @@ COPY src src
COPY .git .git COPY .git .git
# Set UI version # Set UI version
RUN echo "export const VERSION = '$(git describe --tag)'; export const COMMIT_HASH = '$(git rev-parse HEAD)';" > "src/shared/version.ts" RUN echo "export const VERSION = '$(git describe --tag)';" > "src/shared/version.ts"
RUN yarn --production --prefer-offline RUN yarn --production --prefer-offline
RUN yarn build:prod RUN yarn build:prod

View file

@ -18,13 +18,12 @@ COPY generate_translations.js \
.babelrc \ .babelrc \
./ ./
COPY lemmy-translations lemmy-translations COPY lemmy-translations lemmy-translations
COPY src src COPY src src
COPY .git .git COPY .git .git
# Set UI version # Set UI version
RUN echo "export const VERSION = 'dev'; export const COMMIT_HASH = '$(git rev-parse HEAD)';" > "src/shared/version.ts" RUN echo "export const VERSION = 'dev';" > "src/shared/version.ts"
RUN yarn --prefer-offline RUN yarn --prefer-offline
RUN yarn build:dev RUN yarn build:dev

View file

@ -8,9 +8,9 @@
"scripts": { "scripts": {
"analyze": "webpack --mode=none", "analyze": "webpack --mode=none",
"prebuild:dev": "yarn clean && node generate_translations.js", "prebuild:dev": "yarn clean && node generate_translations.js",
"build:dev": "webpack --mode=development", "build:dev": "webpack --env COMMIT_HASH=$(git rev-parse --short HEAD) --mode=development",
"prebuild:prod": "yarn clean && node generate_translations.js", "prebuild:prod": "yarn clean && node generate_translations.js",
"build:prod": "webpack --mode=production", "build:prod": "webpack --env COMMIT_HASH=$(git rev-parse --short HEAD) --mode=production",
"clean": "yarn run rimraf dist", "clean": "yarn run rimraf dist",
"dev": "yarn build:dev --watch", "dev": "yarn build:dev --watch",
"lint": "yarn translations:generate && tsc --noEmit && eslint --report-unused-disable-directives --ext .js,.ts,.tsx \"src/**\" && prettier --check \"src/**/*.{ts,tsx,js,css,scss}\"", "lint": "yarn translations:generate && tsc --noEmit && eslint --report-unused-disable-directives --ext .js,.ts,.tsx \"src/**\" && prettier --check \"src/**/*.{ts,tsx,js,css,scss}\"",

View file

@ -2,7 +2,6 @@ import { setupDateFns } from "@utils/app";
import express from "express"; import express from "express";
import path from "path"; import path from "path";
import process from "process"; import process from "process";
import { COMMIT_HASH } from "../shared/version";
import CatchAllHandler from "./handlers/catch-all-handler"; import CatchAllHandler from "./handlers/catch-all-handler";
import ManifestHandler from "./handlers/manifest-handler"; import ManifestHandler from "./handlers/manifest-handler";
import RobotsHandler from "./handlers/robots-handler"; import RobotsHandler from "./handlers/robots-handler";
@ -19,7 +18,10 @@ const [hostname, port] = process.env["LEMMY_UI_HOST"]
server.use(express.json()); server.use(express.json());
server.use(express.urlencoded({ extended: false })); server.use(express.urlencoded({ extended: false }));
server.use(`/static/${COMMIT_HASH}`, express.static(path.resolve("./dist"))); server.use(
`/static/${process.env.COMMIT_HASH}`,
express.static(path.resolve("./dist"))
);
server.use(setCacheControl); server.use(setCacheControl);
if (!process.env["LEMMY_UI_DISABLE_CSP"] && !process.env["LEMMY_UI_DEBUG"]) { if (!process.env["LEMMY_UI_DISABLE_CSP"] && !process.env["LEMMY_UI_DEBUG"]) {
@ -34,6 +36,7 @@ server.get("/css/themelist", ThemesListHandler);
server.get("/*", CatchAllHandler); server.get("/*", CatchAllHandler);
server.listen(Number(port), hostname, () => { server.listen(Number(port), hostname, () => {
console.log(`HASH: ${process.env.COMMIT_HASH}`);
setupDateFns(); setupDateFns();
console.log(`http://${hostname}:${port}`); console.log(`http://${hostname}:${port}`);
}); });

View file

@ -4,7 +4,6 @@ import serialize from "serialize-javascript";
import sharp from "sharp"; import sharp from "sharp";
import { favIconPngUrl, favIconUrl } from "../../shared/config"; import { favIconPngUrl, favIconUrl } from "../../shared/config";
import { ILemmyConfig, IsoDataOptionalSite } from "../../shared/interfaces"; import { ILemmyConfig, IsoDataOptionalSite } from "../../shared/interfaces";
import { COMMIT_HASH } from "../../shared/version";
import { buildThemeList } from "./build-themes-list"; import { buildThemeList } from "./build-themes-list";
import { fetchIconPng } from "./fetch-icon-png"; import { fetchIconPng } from "./fetch-icon-png";
@ -88,7 +87,9 @@ export async function createSsrHtml(
<link rel="apple-touch-startup-image" href=${appleTouchIcon} /> <link rel="apple-touch-startup-image" href=${appleTouchIcon} />
<!-- Styles --> <!-- Styles -->
<link rel="stylesheet" type="text/css" href="/static/${COMMIT_HASH}/styles/styles.css" /> <link rel="stylesheet" type="text/css" href="/static/${
process.env.COMMIT_HASH
}/styles/styles.css" />
<!-- Current theme and more --> <!-- Current theme and more -->
${helmet.link.toString() || fallbackTheme} ${helmet.link.toString() || fallbackTheme}
@ -103,7 +104,9 @@ export async function createSsrHtml(
</noscript> </noscript>
<div id='root'>${root}</div> <div id='root'>${root}</div>
<script defer src='/static/${COMMIT_HASH}/js/client.js'></script> <script defer src='/static/${
process.env.COMMIT_HASH
}/js/client.js'></script>
</body> </body>
</html> </html>
`; `;

View file

@ -1,6 +1,5 @@
import classNames from "classnames"; import classNames from "classnames";
import { Component } from "inferno"; import { Component } from "inferno";
import { COMMIT_HASH } from "../../../shared/version";
import { I18NextService } from "../../services"; import { I18NextService } from "../../services";
interface IconProps { interface IconProps {
@ -24,7 +23,7 @@ export class Icon extends Component<IconProps, any> {
})} })}
> >
<use <use
xlinkHref={`/static/${COMMIT_HASH}/assets/symbols.svg#icon-${this.props.icon}`} xlinkHref={`/static/${process.env.COMMIT_HASH}/assets/symbols.svg#icon-${this.props.icon}`}
></use> ></use>
<div className="visually-hidden"> <div className="visually-hidden">
<title>{this.props.icon}</title> <title>{this.props.icon}</title>

View file

@ -4,7 +4,6 @@ import classNames from "classnames";
import { Component } from "inferno"; import { Component } from "inferno";
import { Link } from "inferno-router"; import { Link } from "inferno-router";
import { Person } from "lemmy-js-client"; import { Person } from "lemmy-js-client";
import { COMMIT_HASH } from "../../../shared/version";
import { relTags } from "../../config"; import { relTags } from "../../config";
import { PictrsImage } from "../common/pictrs-image"; import { PictrsImage } from "../common/pictrs-image";
import { CakeDay } from "./cake-day"; import { CakeDay } from "./cake-day";
@ -90,7 +89,8 @@ export class PersonListing extends Component<PersonListingProps, any> {
showAvatars() && ( showAvatars() && (
<PictrsImage <PictrsImage
src={ src={
avatar ?? `/static/${COMMIT_HASH}/assets/icons/icon-96x96.png` avatar ??
`/static/${process.env.COMMIT_HASH}/assets/icons/icon-96x96.png`
} }
icon icon
/> />

View file

@ -1,7 +1,5 @@
import { COMMIT_HASH } from "./version"; export const favIconUrl = `/static/${process.env.COMMIT_HASH}/assets/icons/favicon.svg`;
export const favIconPngUrl = `/static/${process.env.COMMIT_HASH}/assets/icons/apple-touch-icon.png`;
export const favIconUrl = `/static/${COMMIT_HASH}/assets/icons/favicon.svg`;
export const favIconPngUrl = `/static/${COMMIT_HASH}/assets/icons/apple-touch-icon.png`;
export const repoUrl = "https://github.com/LemmyNet"; export const repoUrl = "https://github.com/LemmyNet";
export const joinLemmyUrl = "https://join-lemmy.org"; export const joinLemmyUrl = "https://join-lemmy.org";

View file

@ -1,2 +1 @@
export const VERSION = "unknown version"; export const VERSION = "unknown version";
export const COMMIT_HASH = "unknown hash";

View file

@ -14,61 +14,62 @@ const banner = `
@license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL v3.0 @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL v3.0
`; `;
const commitHash = require("child_process") function getBase(env) {
.execSync("git rev-parse HEAD") return {
.toString() output: {
.trim(); filename: "js/server.js",
publicPath: "/",
const base = { hashFunction: "xxhash64",
output: {
filename: "js/server.js",
publicPath: "/",
hashFunction: "xxhash64",
},
resolve: {
extensions: [".js", ".jsx", ".ts", ".tsx"],
alias: {
"@": path.resolve(__dirname, "src/"),
"@utils": path.resolve(__dirname, "src/shared/utils/"),
}, },
}, resolve: {
performance: { extensions: [".js", ".jsx", ".ts", ".tsx"],
hints: false, alias: {
}, "@": path.resolve(__dirname, "src/"),
module: { "@utils": path.resolve(__dirname, "src/shared/utils/"),
rules: [
{
test: /\.(scss|css)$/i,
use: [MiniCssExtractPlugin.loader, "css-loader", "sass-loader"],
}, },
{ },
test: /\.(js|jsx|tsx|ts)$/, // All ts and tsx files will be process by performance: {
exclude: /node_modules/, // ignore node_modules hints: false,
loader: "babel-loader", },
}, module: {
// Due to some weird babel issue: https://github.com/webpack/webpack/issues/11467 rules: [
{ {
test: /\.m?js/, test: /\.(scss|css)$/i,
resolve: { use: [MiniCssExtractPlugin.loader, "css-loader", "sass-loader"],
fullySpecified: false,
}, },
}, {
test: /\.(js|jsx|tsx|ts)$/, // All ts and tsx files will be process by
exclude: /node_modules/, // ignore node_modules
loader: "babel-loader",
},
// Due to some weird babel issue: https://github.com/webpack/webpack/issues/11467
{
test: /\.m?js/,
resolve: {
fullySpecified: false,
},
},
],
},
plugins: [
new webpack.DefinePlugin({
"process.env.COMMIT_HASH": `"${env.COMMIT_HASH}"`,
}),
new MiniCssExtractPlugin({
filename: "styles/styles.css",
}),
new CopyPlugin({
patterns: [{ from: "./src/assets", to: "./assets" }],
}),
new webpack.BannerPlugin({
banner,
}),
], ],
}, };
plugins: [ }
new MiniCssExtractPlugin({
filename: "styles/styles.css",
}),
new CopyPlugin({
patterns: [{ from: "./src/assets", to: "./assets" }],
}),
new webpack.BannerPlugin({
banner,
}),
],
};
const createServerConfig = (_env, mode) => { const createServerConfig = (env, mode) => {
const base = getBase(env);
const config = merge({}, base, { const config = merge({}, base, {
mode, mode,
entry: "./src/server/index.tsx", entry: "./src/server/index.tsx",
@ -95,13 +96,14 @@ const createServerConfig = (_env, mode) => {
return config; return config;
}; };
const createClientConfig = (_env, mode) => { const createClientConfig = (env, mode) => {
const base = getBase(env);
const config = merge({}, base, { const config = merge({}, base, {
mode, mode,
entry: "./src/client/index.tsx", entry: "./src/client/index.tsx",
output: { output: {
filename: "js/client.js", filename: "js/client.js",
publicPath: `/static/${commitHash}`, publicPath: `/static/${env.COMMIT_HASH}`,
}, },
plugins: [ plugins: [
...base.plugins, ...base.plugins,
@ -109,7 +111,7 @@ const createClientConfig = (_env, mode) => {
enableInDevelopment: mode !== "development", // this may seem counterintuitive, but it is correct enableInDevelopment: mode !== "development", // this may seem counterintuitive, but it is correct
workbox: { workbox: {
modifyURLPrefix: { modifyURLPrefix: {
"/": `/static/${commitHash}/`, "/": `/static/${env.COMMIT_HASH}/`,
}, },
cacheId: "lemmy", cacheId: "lemmy",
include: [/(assets|styles)\/.+\..+|client\.js$/g], include: [/(assets|styles)\/.+\..+|client\.js$/g],