2020-07-29 05:00:23 +02:00
|
|
|
/// <reference types="webpack-dev-server" />
|
|
|
|
|
2024-10-09 00:25:41 +02:00
|
|
|
import path from "node:path";
|
2024-11-13 00:36:33 +01:00
|
|
|
import * as url from "node:url";
|
2020-07-24 06:02:07 +02:00
|
|
|
|
2024-08-01 00:10:40 +02:00
|
|
|
import type {ZopfliOptions} from "@gfx/zopfli";
|
|
|
|
import {gzip} from "@gfx/zopfli";
|
|
|
|
import CompressionPlugin from "compression-webpack-plugin";
|
2021-03-17 23:36:20 +01:00
|
|
|
import CssMinimizerPlugin from "css-minimizer-webpack-plugin";
|
2020-07-15 01:29:15 +02:00
|
|
|
import HtmlWebpackPlugin from "html-webpack-plugin";
|
|
|
|
import MiniCssExtractPlugin from "mini-css-extract-plugin";
|
2024-11-13 00:36:33 +01:00
|
|
|
import webpack from "webpack";
|
2021-03-17 03:11:41 +01:00
|
|
|
import BundleTracker from "webpack-bundle-tracker";
|
2020-07-24 06:02:07 +02:00
|
|
|
|
2024-11-12 03:59:37 +01:00
|
|
|
import DebugRequirePlugin from "./debug-require-webpack-plugin.ts";
|
2024-11-13 00:36:33 +01:00
|
|
|
import assets from "./webpack.assets.json" with {type: "json"};
|
|
|
|
import dev_assets from "./webpack.dev-assets.json" with {type: "json"};
|
2017-05-22 22:49:20 +02:00
|
|
|
|
2024-04-26 01:00:10 +02:00
|
|
|
const config = (
|
2024-11-07 00:51:03 +01:00
|
|
|
env: {minimize?: true; puppeteer_tests?: true; ZULIP_VERSION?: string} = {},
|
2023-03-03 22:38:01 +01:00
|
|
|
argv: {mode?: string},
|
|
|
|
): webpack.Configuration[] => {
|
2020-09-26 06:32:14 +02:00
|
|
|
const production: boolean = argv.mode === "production";
|
|
|
|
|
2021-03-18 01:32:29 +01:00
|
|
|
const baseConfig: webpack.Configuration = {
|
2018-04-17 21:59:17 +02:00
|
|
|
mode: production ? "production" : "development",
|
2024-11-13 00:36:33 +01:00
|
|
|
context: import.meta.dirname,
|
2021-03-18 01:32:29 +01:00
|
|
|
cache: {
|
|
|
|
type: "filesystem",
|
|
|
|
buildDependencies: {
|
2024-11-13 00:36:33 +01:00
|
|
|
config: [import.meta.filename],
|
2021-03-18 01:32:29 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2024-08-02 05:06:04 +02:00
|
|
|
const plugins: webpack.WebpackPluginInstance[] = [
|
2024-11-13 00:36:33 +01:00
|
|
|
new webpack.DefinePlugin({
|
2024-08-02 05:06:04 +02:00
|
|
|
DEVELOPMENT: JSON.stringify(!production),
|
|
|
|
ZULIP_VERSION: JSON.stringify(env.ZULIP_VERSION ?? "development"),
|
|
|
|
}),
|
|
|
|
new DebugRequirePlugin(),
|
|
|
|
new BundleTracker({
|
2024-11-13 00:36:33 +01:00
|
|
|
path: path.join(import.meta.dirname, production ? ".." : "../var"),
|
2024-08-02 05:06:04 +02:00
|
|
|
filename: production ? "webpack-stats-production.json" : "webpack-stats-dev.json",
|
|
|
|
}),
|
|
|
|
// Extract CSS from files
|
|
|
|
new MiniCssExtractPlugin({
|
|
|
|
filename: production ? "[name].[contenthash].css" : "[name].css",
|
|
|
|
chunkFilename: production ? "[contenthash].css" : "[id].css",
|
|
|
|
}),
|
|
|
|
new HtmlWebpackPlugin({
|
|
|
|
filename: "5xx.html",
|
|
|
|
template: "html/5xx.html",
|
|
|
|
chunks: ["error-styles"],
|
|
|
|
publicPath: production ? "/static/webpack-bundles/" : "/webpack/",
|
|
|
|
}),
|
|
|
|
];
|
2024-11-07 00:51:03 +01:00
|
|
|
if (production && !env.puppeteer_tests) {
|
2024-08-02 05:06:04 +02:00
|
|
|
plugins.push(
|
|
|
|
new CompressionPlugin<ZopfliOptions>({
|
|
|
|
// Use zopfli to write pre-compressed versions of text files
|
|
|
|
test: /\.(js|css|html)$/,
|
|
|
|
algorithm: gzip,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-18 01:32:29 +01:00
|
|
|
const frontendConfig: webpack.Configuration = {
|
|
|
|
...baseConfig,
|
|
|
|
name: "frontend",
|
2020-09-26 04:44:32 +02:00
|
|
|
entry: production
|
|
|
|
? assets
|
|
|
|
: Object.fromEntries(
|
2021-04-15 22:57:23 +02:00
|
|
|
Object.entries({...assets, ...dev_assets}).map(([name, paths]) => [
|
2020-09-26 04:44:32 +02:00
|
|
|
name,
|
2024-11-12 22:56:47 +01:00
|
|
|
[...paths, "./src/debug.ts"],
|
2020-09-26 04:44:32 +02:00
|
|
|
]),
|
|
|
|
),
|
2017-07-28 22:33:40 +02:00
|
|
|
module: {
|
|
|
|
rules: [
|
2021-03-01 23:33:52 +01:00
|
|
|
{
|
2024-11-13 00:36:33 +01:00
|
|
|
test: path.resolve(import.meta.dirname, "src/zulip_test.ts"),
|
2021-03-01 23:33:52 +01:00
|
|
|
loader: "expose-loader",
|
|
|
|
options: {exposes: "zulip_test"},
|
|
|
|
},
|
2020-07-29 03:03:53 +02:00
|
|
|
{
|
2024-11-13 07:13:26 +01:00
|
|
|
test: path.resolve(import.meta.dirname, "debug-require.cjs"),
|
2020-07-29 03:03:53 +02:00
|
|
|
loader: "expose-loader",
|
|
|
|
options: {exposes: "require"},
|
|
|
|
},
|
|
|
|
{
|
2024-11-13 00:36:33 +01:00
|
|
|
test: url.fileURLToPath(import.meta.resolve("jquery")),
|
2020-07-29 03:03:53 +02:00
|
|
|
loader: "expose-loader",
|
|
|
|
options: {exposes: ["$", "jQuery"]},
|
|
|
|
},
|
2019-07-18 06:47:06 +02:00
|
|
|
// Generate webfont
|
|
|
|
{
|
|
|
|
test: /\.font\.js$/,
|
|
|
|
use: [
|
|
|
|
MiniCssExtractPlugin.loader,
|
2020-04-04 01:47:18 +02:00
|
|
|
{
|
2020-07-15 01:29:15 +02:00
|
|
|
loader: "css-loader",
|
2020-04-04 01:47:18 +02:00
|
|
|
options: {
|
2020-07-16 23:29:01 +02:00
|
|
|
url: false, // webfonts-loader generates public relative URLs
|
2020-04-04 01:47:18 +02:00
|
|
|
},
|
|
|
|
},
|
2019-07-18 06:47:06 +02:00
|
|
|
{
|
2020-07-15 01:29:15 +02:00
|
|
|
loader: "webfonts-loader",
|
2019-07-18 06:47:06 +02:00
|
|
|
options: {
|
2020-07-15 00:34:28 +02:00
|
|
|
fileName: production
|
|
|
|
? "files/[fontname].[chunkhash].[ext]"
|
|
|
|
: "files/[fontname].[ext]",
|
2020-07-15 01:29:15 +02:00
|
|
|
publicPath: "",
|
2019-07-18 06:47:06 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2019-07-10 00:05:46 +02:00
|
|
|
// Transpile .js and .ts files with Babel
|
2017-07-28 22:33:40 +02:00
|
|
|
{
|
2024-11-10 07:02:11 +01:00
|
|
|
test: /\.[cm]?[jt]s$/,
|
2019-10-04 23:08:11 +02:00
|
|
|
include: [
|
2024-11-13 00:36:33 +01:00
|
|
|
path.resolve(import.meta.dirname, "shared/src"),
|
|
|
|
path.resolve(import.meta.dirname, "src"),
|
2019-10-04 23:08:11 +02:00
|
|
|
],
|
2021-03-18 01:32:29 +01:00
|
|
|
loader: "babel-loader",
|
2017-07-28 22:33:40 +02:00
|
|
|
},
|
2018-04-25 22:09:48 +02:00
|
|
|
// regular css files
|
|
|
|
{
|
|
|
|
test: /\.css$/,
|
2024-11-13 00:36:33 +01:00
|
|
|
exclude: path.resolve(import.meta.dirname, "styles"),
|
2019-06-06 11:01:44 +02:00
|
|
|
use: [
|
2020-12-09 23:43:09 +01:00
|
|
|
MiniCssExtractPlugin.loader,
|
2018-04-25 22:09:48 +02:00
|
|
|
{
|
2020-07-15 01:29:15 +02:00
|
|
|
loader: "css-loader",
|
2018-04-25 22:09:48 +02:00
|
|
|
options: {
|
2019-03-24 11:54:17 +01:00
|
|
|
sourceMap: true,
|
2018-05-28 08:09:49 +02:00
|
|
|
},
|
2018-04-25 22:09:48 +02:00
|
|
|
},
|
2019-06-06 11:01:44 +02:00
|
|
|
],
|
2018-04-25 22:09:48 +02:00
|
|
|
},
|
2020-09-15 22:23:01 +02:00
|
|
|
// PostCSS loader
|
2018-04-25 22:09:48 +02:00
|
|
|
{
|
2020-09-15 22:23:01 +02:00
|
|
|
test: /\.css$/,
|
2024-11-13 00:36:33 +01:00
|
|
|
include: path.resolve(import.meta.dirname, "styles"),
|
2019-06-06 11:01:44 +02:00
|
|
|
use: [
|
2020-12-09 23:43:09 +01:00
|
|
|
MiniCssExtractPlugin.loader,
|
2018-04-25 22:09:48 +02:00
|
|
|
{
|
2020-07-15 01:29:15 +02:00
|
|
|
loader: "css-loader",
|
2018-04-25 22:09:48 +02:00
|
|
|
options: {
|
2019-08-27 01:23:48 +02:00
|
|
|
importLoaders: 1,
|
2019-03-24 11:54:17 +01:00
|
|
|
sourceMap: true,
|
|
|
|
},
|
2018-04-25 22:09:48 +02:00
|
|
|
},
|
|
|
|
{
|
2020-07-15 01:29:15 +02:00
|
|
|
loader: "postcss-loader",
|
2018-04-25 22:09:48 +02:00
|
|
|
options: {
|
2019-03-24 11:54:17 +01:00
|
|
|
sourceMap: true,
|
|
|
|
},
|
|
|
|
},
|
2019-06-06 11:01:44 +02:00
|
|
|
],
|
2018-04-25 22:09:48 +02:00
|
|
|
},
|
2019-06-25 11:39:03 +02:00
|
|
|
{
|
2019-07-12 00:52:56 +02:00
|
|
|
test: /\.hbs$/,
|
2021-03-18 01:32:29 +01:00
|
|
|
loader: "handlebars-loader",
|
|
|
|
options: {
|
|
|
|
ignoreHelpers: true,
|
|
|
|
// Tell webpack not to explicitly require these.
|
|
|
|
knownHelpers: [
|
|
|
|
"if",
|
|
|
|
"unless",
|
|
|
|
"each",
|
|
|
|
"with",
|
2023-02-22 23:03:47 +01:00
|
|
|
// The ones below are defined in web/src/templates.js
|
2021-03-18 01:32:29 +01:00
|
|
|
"plural",
|
|
|
|
"eq",
|
|
|
|
"and",
|
|
|
|
"or",
|
|
|
|
"not",
|
|
|
|
"t",
|
|
|
|
"tr",
|
|
|
|
"rendered_markdown",
|
2023-02-08 21:40:48 +01:00
|
|
|
"tooltip_hotkey_hints",
|
2024-02-28 12:27:51 +01:00
|
|
|
"popover_hotkey_hints",
|
2021-03-18 01:32:29 +01:00
|
|
|
],
|
2023-03-22 02:18:19 +01:00
|
|
|
precompileOptions: {strict: true},
|
2021-03-18 01:32:29 +01:00
|
|
|
preventIndent: true,
|
2023-02-07 21:59:06 +01:00
|
|
|
// This replaces relative image resources with
|
|
|
|
// a computed require() path to them, so their
|
|
|
|
// webpack-hashed URLs are used.
|
2023-02-22 23:03:47 +01:00
|
|
|
inlineRequires: /^(\.\.\/)+(images|static)\//,
|
2021-03-18 01:32:29 +01:00
|
|
|
},
|
2019-06-25 11:39:03 +02:00
|
|
|
},
|
2018-04-25 22:09:48 +02:00
|
|
|
// load fonts and files
|
|
|
|
{
|
2024-08-22 18:08:03 +02:00
|
|
|
test: /\.(eot|jpg|svg|ttf|otf|png|webp|woff2?)$/,
|
2021-03-18 01:32:29 +01:00
|
|
|
type: "asset/resource",
|
2019-03-24 11:54:17 +01:00
|
|
|
},
|
2017-07-28 22:33:40 +02:00
|
|
|
],
|
|
|
|
},
|
|
|
|
output: {
|
2024-11-13 00:36:33 +01:00
|
|
|
path: path.resolve(import.meta.dirname, "../static/webpack-bundles"),
|
2023-10-16 06:58:50 +02:00
|
|
|
publicPath: "auto",
|
2020-07-15 01:29:15 +02:00
|
|
|
filename: production ? "[name].[contenthash].js" : "[name].js",
|
2021-03-18 01:32:29 +01:00
|
|
|
assetModuleFilename: production
|
|
|
|
? "files/[name].[hash][ext][query]"
|
|
|
|
: // Avoid directory traversal bug that upstream won't fix
|
|
|
|
// (https://github.com/webpack/webpack/issues/11937)
|
|
|
|
(pathData) => "files" + path.join("/", pathData.filename!),
|
2020-07-15 01:29:15 +02:00
|
|
|
chunkFilename: production ? "[contenthash].js" : "[id].js",
|
2023-04-06 19:46:47 +02:00
|
|
|
crossOriginLoading: "anonymous",
|
2017-07-28 22:33:40 +02:00
|
|
|
},
|
2020-09-26 06:09:38 +02:00
|
|
|
// We prefer cheap-module-source-map over any eval-* options
|
|
|
|
// because stacktrace-gps doesn't currently support extracting
|
|
|
|
// the source snippets with the eval-* options.
|
2020-07-15 01:29:15 +02:00
|
|
|
devtool: production ? "source-map" : "cheap-module-source-map",
|
2019-08-30 19:16:28 +02:00
|
|
|
optimization: {
|
2021-06-10 22:13:56 +02:00
|
|
|
minimize: env.minimize ?? production,
|
2019-08-30 19:16:28 +02:00
|
|
|
minimizer: [
|
2021-03-17 23:36:20 +01:00
|
|
|
new CssMinimizerPlugin({
|
2021-03-18 01:32:29 +01:00
|
|
|
minify: CssMinimizerPlugin.cleanCssMinify,
|
2019-08-30 19:16:28 +02:00
|
|
|
}),
|
2021-03-18 01:32:29 +01:00
|
|
|
"...",
|
2019-08-30 19:16:28 +02:00
|
|
|
],
|
2019-10-22 02:14:28 +02:00
|
|
|
splitChunks: {
|
|
|
|
chunks: "all",
|
|
|
|
// webpack/examples/many-pages suggests 20 requests for HTTP/2
|
|
|
|
maxAsyncRequests: 20,
|
|
|
|
maxInitialRequests: 20,
|
|
|
|
},
|
2019-08-30 19:16:28 +02:00
|
|
|
},
|
2024-08-02 05:06:04 +02:00
|
|
|
plugins,
|
2020-09-26 04:45:57 +02:00
|
|
|
devServer: {
|
2023-03-21 20:06:35 +01:00
|
|
|
client: {
|
|
|
|
overlay: {
|
|
|
|
runtimeErrors: false,
|
|
|
|
},
|
|
|
|
},
|
2021-09-10 21:28:29 +02:00
|
|
|
devMiddleware: {
|
|
|
|
publicPath: "/webpack/",
|
2022-08-23 17:14:40 +02:00
|
|
|
stats: {
|
|
|
|
// We want just errors and a clear, brief notice
|
|
|
|
// whenever webpack compilation has finished.
|
|
|
|
preset: "minimal",
|
|
|
|
assets: false,
|
|
|
|
modules: false,
|
|
|
|
},
|
2021-09-10 21:28:29 +02:00
|
|
|
},
|
2020-04-04 01:47:18 +02:00
|
|
|
headers: {
|
|
|
|
"Access-Control-Allow-Origin": "*",
|
2023-04-12 20:33:53 +02:00
|
|
|
"Timing-Allow-Origin": "*",
|
2020-04-04 01:47:18 +02:00
|
|
|
},
|
2021-09-10 21:28:29 +02:00
|
|
|
},
|
|
|
|
infrastructureLogging: {
|
|
|
|
level: "warn",
|
|
|
|
},
|
|
|
|
watchOptions: {
|
|
|
|
ignored: [
|
|
|
|
"**/node_modules/**",
|
|
|
|
// Prevent Emacs file locks from crashing webpack-dev-server
|
|
|
|
// https://github.com/webpack/webpack-dev-server/issues/2821
|
|
|
|
"**/.#*",
|
|
|
|
],
|
2021-03-11 22:31:30 +01:00
|
|
|
},
|
2020-09-26 04:45:57 +02:00
|
|
|
};
|
2019-06-29 10:13:08 +02:00
|
|
|
|
|
|
|
const serverConfig: webpack.Configuration = {
|
2021-03-18 01:32:29 +01:00
|
|
|
...baseConfig,
|
2020-09-26 04:49:49 +02:00
|
|
|
name: "server",
|
2019-06-29 10:13:08 +02:00
|
|
|
target: "node",
|
|
|
|
entry: {
|
2024-03-04 21:00:20 +01:00
|
|
|
katex_server: "babel-loader!./server/katex_server.ts",
|
2019-06-29 10:13:08 +02:00
|
|
|
"katex-cli": "shebang-loader!katex/cli",
|
|
|
|
},
|
|
|
|
output: {
|
2024-11-13 00:36:33 +01:00
|
|
|
path: path.resolve(import.meta.dirname, "../static/webpack-bundles"),
|
2019-06-29 10:13:08 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-03-18 01:32:29 +01:00
|
|
|
return [frontendConfig, serverConfig];
|
2015-10-26 17:11:44 +01:00
|
|
|
};
|
2024-04-26 01:00:10 +02:00
|
|
|
export default config;
|