weblate/client/webpack.config.js
Michal Čihař 8610c1724b
feat: use fonts from a separate package (#17705)
This moves not frequently updated content to a separate package heavily
reducing Weblate package size (we're hitting PyPI limits). This also
consolidates our bundling approach for fonts.

Issue #17395
Fixes #13255
2026-01-20 15:07:06 +00:00

193 lines
5.5 KiB
JavaScript

// Copyright © Michal Čihař <michal@weblate.org>
//
// SPDX-License-Identifier: GPL-3.0-or-later
const path = require("node:path");
const TerserPlugin = require("terser-webpack-plugin");
const LicensePlugin = require("webpack-license-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
// Regular expression to match copyright lines
const copyrightRegex = /Copyright.*\n/;
// REUSE-IgnoreStart
// Function to extract copyright information from a package
function extractCopyright(pkg) {
if (pkg.licenseText !== null) {
const copyrights = pkg.licenseText.match(copyrightRegex);
if (copyrights !== null) {
return copyrights.join("");
}
}
return `Copyright ${pkg.author}\n`;
}
// Generic function to transform package information
function genericTransform(packages, filter) {
const mainPackages = packages.filter(filter);
const licenses = [...new Set(mainPackages.map((pkg) => pkg.license))]
.sort()
.join(" AND ");
const copyrights = [...new Set(mainPackages.map(extractCopyright))]
.sort()
.join("");
return `${copyrights}
SPDX-License-Identifier: ${licenses}
`;
}
// REUSE-IgnoreEnd
// License transform function for global packages used in main.js
function mainLicenseTransform(packages) {
const excludePrefixes = [
"@sentry",
"tributejs",
"@tarekraafat/autocomplete.js",
"autosize",
"multi.js",
"mousetrap",
"prismjs",
"@altcha",
"altcha",
"source-",
"bootstrap",
];
return genericTransform(
packages,
(pkg) => !excludePrefixes.some((prefix) => pkg.name.startsWith(prefix)),
);
}
function sentryLicenseTransform(packages) {
return genericTransform(packages, (pkg) => pkg.name.startsWith("@sentry"));
}
function tributeLicenseTransform(packages) {
return genericTransform(packages, (pkg) => pkg.name.startsWith("tributejs"));
}
function autosizeLicenseTransform(packages) {
return genericTransform(packages, (pkg) => pkg.name.startsWith("autosize"));
}
function multiJsLicenseTransform(packages) {
return genericTransform(packages, (pkg) => pkg.name.startsWith("multi.js"));
}
function prismJsLicenseTransform(packages) {
return genericTransform(packages, (pkg) => pkg.name.startsWith("prismjs"));
}
function altchaLicenseTransform(packages) {
return genericTransform(
packages,
(pkg) => pkg.name.startsWith("altcha") || pkg.name.startsWith("@altcha"),
);
}
function bootstrapLicenseTransform(packages) {
return genericTransform(packages, (pkg) => pkg.name.startsWith("bootstrap"));
}
// REUSE-IgnoreStart
function mousetrapLicenseTransform(packages) {
const pkg = packages.find((pkg) => pkg.name.startsWith("mousetrap"));
if (pkg) {
const author =
typeof pkg.author === "string"
? pkg.author
: pkg.author?.email
? `${pkg.author.name} <${pkg.author.email}>`
: pkg.author?.name
? pkg.author.name
: "";
return `SPDX-FileCopyrightText: ${author}\n\nSPDX-License-Identifier: ${pkg.license}`;
}
return "";
}
function autoCompleteLicenseTransform(packages) {
const pkg = packages.find((pkgsItem) =>
pkgsItem.name.startsWith("@tarekraafat/autocomplete.js"),
);
if (pkg) {
const author =
typeof pkg.author === "string"
? pkg.author
: pkg.author?.email
? `${pkg.author.name} <${pkg.author.email}>`
: pkg.author?.name
? pkg.author.name
: "";
return `SPDX-FileCopyrightText: ${author}\n\nSPDX-License-Identifier: ${pkg.license}`;
}
return "";
}
// REUSE-IgnoreEnd
// Webpack configuration
module.exports = {
entry: {
main: "./src/main.js",
sentry: "./src/sentry.js",
tribute: "./src/tribute.js",
autoComplete: "./src/autoComplete.js",
autosize: "./src/autosize.js",
multi: "./src/multi.js",
mousetrap: "./src/mousetrap.js",
prismjs: "./src/prismjs.js",
altcha: "./src/altcha.js",
bootstrap5: "./src/bootstrap5.js",
bootstrap5_rtl: "./src/bootstrap5_rtl.css",
},
mode: "production",
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
extractComments: false,
terserOptions: {
format: {
comments: false,
},
},
}),
],
},
module: {
rules: [
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, "css-loader"],
},
],
},
plugins: [
new LicensePlugin({
additionalFiles: {
"main.js.license": mainLicenseTransform,
"sentry.js.license": sentryLicenseTransform,
"tribute.js.license": tributeLicenseTransform,
"autoComplete.js.license": autoCompleteLicenseTransform,
"autosize.js.license": autosizeLicenseTransform,
"multi.js.license": multiJsLicenseTransform,
"../../styles/vendor/multi.css.license": multiJsLicenseTransform,
"mousetrap.js.license": mousetrapLicenseTransform,
"prismjs.js.license": prismJsLicenseTransform,
"altcha.js.license": altchaLicenseTransform,
"bootstrap5.js.license": bootstrapLicenseTransform,
"bootstrap5_rtl.js.license": bootstrapLicenseTransform,
"../../styles/vendor/bootstrap5.css.license": bootstrapLicenseTransform,
"../../styles/vendor/bootstrap5_rtl.css.license":
bootstrapLicenseTransform,
},
}),
new MiniCssExtractPlugin({
filename: "../../styles/vendor/[name].css",
}),
],
output: {
filename: "[name].js",
path: path.resolve(__dirname, "../weblate/static/js/vendor"),
},
};