webpack: Run with node --experimental-strip-types.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2024-11-12 15:36:33 -08:00 committed by Tim Abbott
parent a70babaec4
commit 45de69465a
3 changed files with 36 additions and 28 deletions

View File

@ -13,6 +13,13 @@ os.chdir(os.path.join(os.path.dirname(__file__), "../web"))
from version import ZULIP_VERSION from version import ZULIP_VERSION
webpack_command = [
"node",
"--experimental-strip-types",
"--no-warnings=ExperimentalWarning",
"../node_modules/webpack-cli/bin/cli.js",
]
def build_for_prod_or_puppeteer(quiet: bool, config_name: str | None = None) -> NoReturn: def build_for_prod_or_puppeteer(quiet: bool, config_name: str | None = None) -> NoReturn:
"""Builds for production, writing the output to disk""" """Builds for production, writing the output to disk"""
@ -21,7 +28,8 @@ def build_for_prod_or_puppeteer(quiet: bool, config_name: str | None = None) ->
if int(next(meminfo).split()[1]) < 3 * 1024 * 1024: if int(next(meminfo).split()[1]) < 3 * 1024 * 1024:
os.environ["NODE_OPTIONS"] = "--max-old-space-size=1536" os.environ["NODE_OPTIONS"] = "--max-old-space-size=1536"
webpack_args = [ webpack_args = [
"../node_modules/.bin/webpack-cli", *webpack_command,
"build",
"--mode=production", "--mode=production",
f"--env=ZULIP_VERSION={ZULIP_VERSION}", f"--env=ZULIP_VERSION={ZULIP_VERSION}",
] ]
@ -45,8 +53,9 @@ def build_for_dev_server(host: str, port: str, minify: bool, disable_host_check:
# This is our most dynamic configuration, which we use for our # This is our most dynamic configuration, which we use for our
# dev server. The key piece here is that we identify changes to # dev server. The key piece here is that we identify changes to
# files as devs make edits and serve new assets on the fly. # files as devs make edits and serve new assets on the fly.
webpack_args = ["../node_modules/.bin/webpack-cli", "serve"] webpack_args = [
webpack_args += [ *webpack_command,
"serve",
# webpack-cli has a bug where it ignores --watch-poll with # webpack-cli has a bug where it ignores --watch-poll with
# multi-config, and we don't need the katex part anyway. # multi-config, and we don't need the katex part anyway.
"--config-name=frontend", "--config-name=frontend",

View File

@ -5,11 +5,10 @@
import path from "node:path"; import path from "node:path";
import type {ResolveRequest} from "enhanced-resolve"; import type {ResolveRequest} from "enhanced-resolve";
import type {Chunk, Compiler, WebpackPluginInstance} from "webpack"; import webpack from "webpack";
import {NormalModule, Template} from "webpack";
export default class DebugRequirePlugin implements WebpackPluginInstance { export default class DebugRequirePlugin implements webpack.WebpackPluginInstance {
apply(compiler: Compiler): void { apply(compiler: webpack.Compiler): void {
const resolved = new Map<string, Set<string>>(); const resolved = new Map<string, Set<string>>();
const nameSymbol = Symbol("DebugRequirePluginName"); const nameSymbol = Symbol("DebugRequirePluginName");
type NamedRequest = ResolveRequest & { type NamedRequest = ResolveRequest & {
@ -63,7 +62,7 @@ export default class DebugRequirePlugin implements WebpackPluginInstance {
debugRequirePath = await new Promise((resolve) => { debugRequirePath = await new Promise((resolve) => {
resolver.resolve( resolver.resolve(
{}, {},
__dirname, import.meta.dirname,
"./debug-require.js", "./debug-require.js",
{}, {},
(err?: Error | null, result?: string | false) => { (err?: Error | null, result?: string | false) => {
@ -77,7 +76,7 @@ export default class DebugRequirePlugin implements WebpackPluginInstance {
compiler.hooks.compilation.tap("DebugRequirePlugin", (compilation) => { compiler.hooks.compilation.tap("DebugRequirePlugin", (compilation) => {
compilation.mainTemplate.hooks.bootstrap.tap( compilation.mainTemplate.hooks.bootstrap.tap(
"DebugRequirePlugin", "DebugRequirePlugin",
(source: string, chunk: Chunk) => { (source: string, chunk: webpack.Chunk) => {
if (compilation.chunkGraph === undefined) { if (compilation.chunkGraph === undefined) {
return source; return source;
} }
@ -87,7 +86,7 @@ export default class DebugRequirePlugin implements WebpackPluginInstance {
compilation.chunkGraph.hasModuleInGraph( compilation.chunkGraph.hasModuleInGraph(
chunk, chunk,
(m) => { (m) => {
if (m instanceof NormalModule) { if (m instanceof webpack.NormalModule) {
const id = compilation.chunkGraph.getModuleId(m); const id = compilation.chunkGraph.getModuleId(m);
if (id === null) { if (id === null) {
return false; return false;
@ -113,7 +112,7 @@ export default class DebugRequirePlugin implements WebpackPluginInstance {
} }
ids.sort(); ids.sort();
return Template.asString([ return webpack.Template.asString([
source, source,
`__webpack_require__.debugRequireIds = ${JSON.stringify( `__webpack_require__.debugRequireIds = ${JSON.stringify(
Object.fromEntries(ids), Object.fromEntries(ids),

View File

@ -1,6 +1,7 @@
/// <reference types="webpack-dev-server" /> /// <reference types="webpack-dev-server" />
import path from "node:path"; import path from "node:path";
import * as url from "node:url";
import type {ZopfliOptions} from "@gfx/zopfli"; import type {ZopfliOptions} from "@gfx/zopfli";
import {gzip} from "@gfx/zopfli"; import {gzip} from "@gfx/zopfli";
@ -8,13 +9,12 @@ import CompressionPlugin from "compression-webpack-plugin";
import CssMinimizerPlugin from "css-minimizer-webpack-plugin"; import CssMinimizerPlugin from "css-minimizer-webpack-plugin";
import HtmlWebpackPlugin from "html-webpack-plugin"; import HtmlWebpackPlugin from "html-webpack-plugin";
import MiniCssExtractPlugin from "mini-css-extract-plugin"; import MiniCssExtractPlugin from "mini-css-extract-plugin";
import {DefinePlugin} from "webpack"; import webpack from "webpack";
import type webpack from "webpack";
import BundleTracker from "webpack-bundle-tracker"; import BundleTracker from "webpack-bundle-tracker";
import DebugRequirePlugin from "./debug-require-webpack-plugin.ts"; import DebugRequirePlugin from "./debug-require-webpack-plugin.ts";
import assets from "./webpack.assets.json"; import assets from "./webpack.assets.json" with {type: "json"};
import dev_assets from "./webpack.dev-assets.json"; import dev_assets from "./webpack.dev-assets.json" with {type: "json"};
const config = ( const config = (
env: {minimize?: true; puppeteer_tests?: true; ZULIP_VERSION?: string} = {}, env: {minimize?: true; puppeteer_tests?: true; ZULIP_VERSION?: string} = {},
@ -24,23 +24,23 @@ const config = (
const baseConfig: webpack.Configuration = { const baseConfig: webpack.Configuration = {
mode: production ? "production" : "development", mode: production ? "production" : "development",
context: __dirname, context: import.meta.dirname,
cache: { cache: {
type: "filesystem", type: "filesystem",
buildDependencies: { buildDependencies: {
config: [__filename], config: [import.meta.filename],
}, },
}, },
}; };
const plugins: webpack.WebpackPluginInstance[] = [ const plugins: webpack.WebpackPluginInstance[] = [
new DefinePlugin({ new webpack.DefinePlugin({
DEVELOPMENT: JSON.stringify(!production), DEVELOPMENT: JSON.stringify(!production),
ZULIP_VERSION: JSON.stringify(env.ZULIP_VERSION ?? "development"), ZULIP_VERSION: JSON.stringify(env.ZULIP_VERSION ?? "development"),
}), }),
new DebugRequirePlugin(), new DebugRequirePlugin(),
new BundleTracker({ new BundleTracker({
path: path.join(__dirname, production ? ".." : "../var"), path: path.join(import.meta.dirname, production ? ".." : "../var"),
filename: production ? "webpack-stats-production.json" : "webpack-stats-dev.json", filename: production ? "webpack-stats-production.json" : "webpack-stats-dev.json",
}), }),
// Extract CSS from files // Extract CSS from files
@ -79,17 +79,17 @@ const config = (
module: { module: {
rules: [ rules: [
{ {
test: require.resolve("./src/zulip_test.ts"), test: path.resolve(import.meta.dirname, "src/zulip_test.ts"),
loader: "expose-loader", loader: "expose-loader",
options: {exposes: "zulip_test"}, options: {exposes: "zulip_test"},
}, },
{ {
test: require.resolve("./debug-require.js"), test: path.resolve(import.meta.dirname, "debug-require.js"),
loader: "expose-loader", loader: "expose-loader",
options: {exposes: "require"}, options: {exposes: "require"},
}, },
{ {
test: require.resolve("jquery"), test: url.fileURLToPath(import.meta.resolve("jquery")),
loader: "expose-loader", loader: "expose-loader",
options: {exposes: ["$", "jQuery"]}, options: {exposes: ["$", "jQuery"]},
}, },
@ -119,15 +119,15 @@ const config = (
{ {
test: /\.[cm]?[jt]s$/, test: /\.[cm]?[jt]s$/,
include: [ include: [
path.resolve(__dirname, "shared/src"), path.resolve(import.meta.dirname, "shared/src"),
path.resolve(__dirname, "src"), path.resolve(import.meta.dirname, "src"),
], ],
loader: "babel-loader", loader: "babel-loader",
}, },
// regular css files // regular css files
{ {
test: /\.css$/, test: /\.css$/,
exclude: path.resolve(__dirname, "styles"), exclude: path.resolve(import.meta.dirname, "styles"),
use: [ use: [
MiniCssExtractPlugin.loader, MiniCssExtractPlugin.loader,
{ {
@ -141,7 +141,7 @@ const config = (
// PostCSS loader // PostCSS loader
{ {
test: /\.css$/, test: /\.css$/,
include: path.resolve(__dirname, "styles"), include: path.resolve(import.meta.dirname, "styles"),
use: [ use: [
MiniCssExtractPlugin.loader, MiniCssExtractPlugin.loader,
{ {
@ -198,7 +198,7 @@ const config = (
], ],
}, },
output: { output: {
path: path.resolve(__dirname, "../static/webpack-bundles"), path: path.resolve(import.meta.dirname, "../static/webpack-bundles"),
publicPath: "auto", publicPath: "auto",
filename: production ? "[name].[contenthash].js" : "[name].js", filename: production ? "[name].[contenthash].js" : "[name].js",
assetModuleFilename: production assetModuleFilename: production
@ -272,7 +272,7 @@ const config = (
"katex-cli": "shebang-loader!katex/cli", "katex-cli": "shebang-loader!katex/cli",
}, },
output: { output: {
path: path.resolve(__dirname, "../static/webpack-bundles"), path: path.resolve(import.meta.dirname, "../static/webpack-bundles"),
}, },
}; };