2020-07-29 05:00:23 +02:00
|
|
|
/// <reference types="webpack-dev-server" />
|
|
|
|
|
2020-10-07 12:24:12 +02:00
|
|
|
import path from "path";
|
2020-07-24 06:02:07 +02:00
|
|
|
|
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";
|
|
|
|
import TerserPlugin from "terser-webpack-plugin";
|
2020-07-24 06:02:07 +02: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
|
|
|
|
2020-08-29 04:36:53 +02:00
|
|
|
import DebugRequirePlugin from "./tools/debug-require-webpack-plugin";
|
|
|
|
import assets from "./tools/webpack.assets.json";
|
2017-05-22 22:49:20 +02:00
|
|
|
|
2020-07-29 04:57:59 +02:00
|
|
|
const cacheLoader: webpack.RuleSetUseItem = {
|
|
|
|
loader: "cache-loader",
|
|
|
|
options: {
|
2020-10-07 12:24:12 +02:00
|
|
|
cacheDirectory: path.resolve(__dirname, "var/webpack-cache"),
|
2020-07-29 04:57:59 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-09-26 06:32:14 +02:00
|
|
|
export default (_env: unknown, argv: {mode?: string}): webpack.Configuration[] => {
|
|
|
|
const production: boolean = argv.mode === "production";
|
|
|
|
|
2019-03-24 11:54:17 +01:00
|
|
|
const config: webpack.Configuration = {
|
2019-08-16 05:30:53 +02:00
|
|
|
name: "frontend",
|
2018-04-17 21:59:17 +02:00
|
|
|
mode: production ? "production" : "development",
|
2020-08-29 04:36:53 +02:00
|
|
|
context: __dirname,
|
2020-09-26 04:44:32 +02:00
|
|
|
entry: production
|
|
|
|
? assets
|
|
|
|
: Object.fromEntries(
|
|
|
|
Object.entries(assets).map(([name, paths]) => [
|
|
|
|
name,
|
|
|
|
[...paths, "./static/js/debug"],
|
|
|
|
]),
|
|
|
|
),
|
2017-07-28 22:33:40 +02:00
|
|
|
module: {
|
|
|
|
rules: [
|
2021-03-01 23:33:52 +01:00
|
|
|
{
|
|
|
|
test: require.resolve("./static/js/zulip_test"),
|
|
|
|
loader: "expose-loader",
|
|
|
|
options: {exposes: "zulip_test"},
|
|
|
|
},
|
2020-07-29 03:03:53 +02:00
|
|
|
{
|
2020-08-29 04:36:53 +02:00
|
|
|
test: require.resolve("./tools/debug-require"),
|
2020-07-29 03:03:53 +02:00
|
|
|
loader: "expose-loader",
|
|
|
|
options: {exposes: "require"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: require.resolve("jquery"),
|
|
|
|
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
|
|
|
{
|
2019-07-10 00:05:46 +02:00
|
|
|
test: /\.(js|ts)$/,
|
2019-10-04 23:08:11 +02:00
|
|
|
include: [
|
2020-10-07 12:24:12 +02:00
|
|
|
path.resolve(__dirname, "static/shared/js"),
|
|
|
|
path.resolve(__dirname, "static/js"),
|
2019-10-04 23:08:11 +02:00
|
|
|
],
|
2020-07-15 01:29:15 +02:00
|
|
|
use: [cacheLoader, "babel-loader"],
|
2017-07-28 22:33:40 +02:00
|
|
|
},
|
|
|
|
// Uses script-loader on minified files so we don't change global variables in them.
|
|
|
|
// Also has the effect of making processing these files fast
|
2017-08-06 03:53:44 +02:00
|
|
|
// Currently the source maps don't work with these so use unminified files
|
|
|
|
// if debugging is required.
|
2017-07-28 22:33:40 +02:00
|
|
|
{
|
2018-05-28 08:09:49 +02:00
|
|
|
// We dont want to match admin.js
|
|
|
|
test: /(\.min|min\.|zxcvbn)\.js/,
|
2020-07-15 01:29:15 +02:00
|
|
|
use: [cacheLoader, "script-loader"],
|
2017-07-28 22:33:40 +02:00
|
|
|
},
|
2018-04-25 22:09:48 +02:00
|
|
|
// regular css files
|
|
|
|
{
|
|
|
|
test: /\.css$/,
|
2020-10-07 12:24:12 +02:00
|
|
|
exclude: path.resolve(__dirname, "static/styles"),
|
2019-06-06 11:01:44 +02:00
|
|
|
use: [
|
2020-12-09 23:43:09 +01:00
|
|
|
MiniCssExtractPlugin.loader,
|
2019-06-04 21:11:31 +02:00
|
|
|
cacheLoader,
|
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$/,
|
2020-10-07 12:24:12 +02:00
|
|
|
include: path.resolve(__dirname, "static/styles"),
|
2019-06-06 11:01:44 +02:00
|
|
|
use: [
|
2020-12-09 23:43:09 +01:00
|
|
|
MiniCssExtractPlugin.loader,
|
2019-06-04 21:11:31 +02:00
|
|
|
cacheLoader,
|
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$/,
|
2020-04-30 21:31:23 +02:00
|
|
|
use: [
|
|
|
|
cacheLoader,
|
|
|
|
{
|
2020-07-15 01:29:15 +02:00
|
|
|
loader: "handlebars-loader",
|
2020-04-30 21:31:23 +02:00
|
|
|
options: {
|
2021-04-10 00:26:01 +02:00
|
|
|
ignoreHelpers: true,
|
2020-04-30 21:31:23 +02:00
|
|
|
// Tell webpack not to explicitly require these.
|
|
|
|
knownHelpers: [
|
2020-07-15 00:34:28 +02:00
|
|
|
"if",
|
|
|
|
"unless",
|
|
|
|
"each",
|
|
|
|
"with",
|
2020-04-30 21:31:23 +02:00
|
|
|
// The ones below are defined in static/js/templates.js
|
2020-07-15 00:34:28 +02:00
|
|
|
"plural",
|
|
|
|
"eq",
|
|
|
|
"and",
|
|
|
|
"or",
|
|
|
|
"not",
|
|
|
|
"t",
|
|
|
|
"tr",
|
|
|
|
"rendered_markdown",
|
2020-04-30 21:31:23 +02:00
|
|
|
],
|
|
|
|
preventIndent: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2019-06-25 11:39:03 +02:00
|
|
|
},
|
2018-04-25 22:09:48 +02:00
|
|
|
// load fonts and files
|
|
|
|
{
|
2020-08-13 00:27:39 +02:00
|
|
|
test: /\.(eot|jpg|svg|ttf|otf|png|woff2?)$/,
|
2020-07-15 00:34:28 +02:00
|
|
|
use: [
|
|
|
|
{
|
|
|
|
loader: "file-loader",
|
|
|
|
options: {
|
|
|
|
name: production ? "[name].[hash].[ext]" : "[path][name].[ext]",
|
|
|
|
outputPath: "files/",
|
|
|
|
},
|
2019-03-24 11:54:17 +01:00
|
|
|
},
|
2020-07-15 00:34:28 +02:00
|
|
|
],
|
2019-03-24 11:54:17 +01:00
|
|
|
},
|
2017-07-28 22:33:40 +02:00
|
|
|
],
|
|
|
|
},
|
|
|
|
output: {
|
2020-10-07 12:24:12 +02:00
|
|
|
path: path.resolve(__dirname, "static/webpack-bundles"),
|
2020-07-15 01:29:15 +02:00
|
|
|
filename: production ? "[name].[contenthash].js" : "[name].js",
|
|
|
|
chunkFilename: production ? "[contenthash].js" : "[id].js",
|
2017-07-28 22:33:40 +02:00
|
|
|
},
|
|
|
|
resolve: {
|
2019-06-21 02:01:59 +02:00
|
|
|
extensions: [".ts", ".js"],
|
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: {
|
|
|
|
minimizer: [
|
2021-03-17 23:36:20 +01:00
|
|
|
new CssMinimizerPlugin({
|
|
|
|
sourceMap: true,
|
|
|
|
minify: (data: Record<string, string>, sourceMap) => {
|
|
|
|
// css-minimizer-webpack-plugin needs this require
|
|
|
|
// inside the function.
|
|
|
|
// eslint-disable-next-line @typescript-eslint/consistent-type-imports, @typescript-eslint/no-var-requires
|
|
|
|
const CleanCSS: typeof import("clean-css") = require("clean-css");
|
|
|
|
const [[filename, styles]] = Object.entries(data);
|
|
|
|
const out = new CleanCSS({sourceMap: true}).minify({
|
|
|
|
[filename]: {styles, sourceMap},
|
|
|
|
});
|
|
|
|
return {css: out.styles, map: out.sourceMap, warnings: out.warnings};
|
2019-08-30 19:16:28 +02:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
new TerserPlugin({
|
|
|
|
cache: true,
|
|
|
|
parallel: true,
|
|
|
|
}),
|
|
|
|
],
|
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
|
|
|
},
|
2019-10-22 01:26:30 +02:00
|
|
|
plugins: [
|
2020-12-29 10:56:28 +01:00
|
|
|
new webpack.ProgressPlugin({
|
|
|
|
handler(percentage) {
|
|
|
|
if (percentage === 1) {
|
|
|
|
console.log(
|
|
|
|
"\u001B[34mi 「wdm」\u001B[0m:",
|
|
|
|
"Webpack compilation successful.",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}),
|
2020-02-25 04:34:09 +01:00
|
|
|
new DebugRequirePlugin(),
|
2019-10-22 01:26:30 +02:00
|
|
|
new BundleTracker({
|
|
|
|
filename: production
|
2021-03-17 03:11:41 +01:00
|
|
|
? "../../webpack-stats-production.json"
|
|
|
|
: "../../var/webpack-stats-dev.json",
|
|
|
|
relativePath: true,
|
|
|
|
// Respecify many defaults until https://github.com/django-webpack/webpack-bundle-tracker/pull/55 is merged
|
|
|
|
path: path.resolve(__dirname, "static/webpack-bundles"),
|
|
|
|
integrity: false,
|
|
|
|
integrityHashes: [],
|
2019-10-22 01:26:30 +02:00
|
|
|
}),
|
2020-07-15 00:34:28 +02:00
|
|
|
...(production
|
2019-10-22 01:26:30 +02:00
|
|
|
? []
|
|
|
|
: [
|
2020-07-15 00:34:28 +02:00
|
|
|
// Better logging from console for hot reload
|
|
|
|
new webpack.NamedModulesPlugin(),
|
|
|
|
// script-loader should load sourceURL in dev
|
|
|
|
new webpack.LoaderOptionsPlugin({debug: true}),
|
|
|
|
]),
|
2019-10-22 01:26:30 +02:00
|
|
|
// Extract CSS from files
|
|
|
|
new MiniCssExtractPlugin({
|
2019-10-22 02:02:14 +02:00
|
|
|
filename: production ? "[name].[contenthash].css" : "[name].css",
|
2019-10-22 02:14:28 +02:00
|
|
|
chunkFilename: production ? "[contenthash].css" : "[id].css",
|
2019-10-22 01:26:30 +02:00
|
|
|
}),
|
2019-10-22 02:02:14 +02:00
|
|
|
new HtmlWebpackPlugin({
|
|
|
|
filename: "5xx.html",
|
|
|
|
template: "static/html/5xx.html",
|
|
|
|
chunks: ["error-styles"],
|
|
|
|
}),
|
2019-10-22 01:26:30 +02:00
|
|
|
],
|
2020-09-26 04:45:57 +02:00
|
|
|
devServer: {
|
2018-04-25 21:16:08 +02:00
|
|
|
clientLogLevel: "error",
|
2020-04-04 01:47:18 +02:00
|
|
|
headers: {
|
|
|
|
"Access-Control-Allow-Origin": "*",
|
|
|
|
},
|
|
|
|
publicPath: "/webpack/",
|
2018-04-25 21:16:08 +02:00
|
|
|
stats: "errors-only",
|
2020-12-29 10:56:28 +01:00
|
|
|
noInfo: true,
|
2020-09-26 04:45:57 +02:00
|
|
|
},
|
2021-03-11 22:31:30 +01:00
|
|
|
watchOptions: {
|
|
|
|
ignored: [
|
|
|
|
// Prevent Emacs file locks from crashing webpack-dev-server
|
|
|
|
// https://github.com/webpack/webpack-dev-server/issues/2821
|
|
|
|
"**/.#*",
|
|
|
|
],
|
|
|
|
},
|
2020-09-26 04:45:57 +02:00
|
|
|
};
|
2019-06-29 10:13:08 +02:00
|
|
|
|
|
|
|
const serverConfig: webpack.Configuration = {
|
2020-09-26 04:49:49 +02:00
|
|
|
name: "server",
|
2019-06-29 10:13:08 +02:00
|
|
|
mode: production ? "production" : "development",
|
|
|
|
target: "node",
|
2020-08-29 04:36:53 +02:00
|
|
|
context: __dirname,
|
2019-06-29 10:13:08 +02:00
|
|
|
entry: {
|
|
|
|
"katex-cli": "shebang-loader!katex/cli",
|
|
|
|
},
|
|
|
|
output: {
|
2020-10-07 12:24:12 +02:00
|
|
|
path: path.resolve(__dirname, "static/webpack-bundles"),
|
2019-06-29 10:13:08 +02:00
|
|
|
filename: "[name].js",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
return [config, serverConfig];
|
2015-10-26 17:11:44 +01:00
|
|
|
};
|