forked from zulip/zulip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack
executable file
·149 lines (129 loc) · 5.52 KB
/
webpack
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python3
import argparse
import json
import os
import subprocess
from typing import NoReturn
os.chdir(os.path.join(os.path.dirname(__file__), ".."))
def build_for_prod_or_puppeteer(quiet: bool) -> NoReturn:
"""Builds for production, writing the output to disk"""
webpack_args = ["node"]
with open("/proc/meminfo") as meminfo:
if int(next(meminfo).split()[1]) < 3 * 1024 * 1024:
webpack_args += ["--max-old-space-size=1536"]
webpack_args += ["node_modules/.bin/webpack-cli", "--mode=production"]
if quiet:
webpack_args += ["--stats=errors-only"]
os.execvp(webpack_args[0], webpack_args)
def build_for_dev_server(host: str, port: str, minify: bool, disable_host_check: bool) -> None:
"""watches and rebuilds on changes, serving files from memory via webpack-dev-server"""
# This is our most dynamic configuration, which we use for our
# dev server. The key piece here is that we identify changes to
# files as devs make edits and serve new assets on the fly.
webpack_args = ["node", "node_modules/.bin/webpack-cli", "serve"]
webpack_args += [
# webpack-cli has a bug where it ignores --watch-poll with
# multi-config, and we don't need the katex-cli part anyway.
"--config-name=frontend",
f"--host={host}",
f"--port={port}",
# We add the hot flag using the cli because it takes care
# of addition to entry points and adding the plugin
# automatically
"--hot",
]
if minify:
webpack_args.append("--env=minimize")
if disable_host_check:
webpack_args.append("--allowed-hosts=all")
else:
webpack_args.append("--allowed-hosts=" + ",".join([host, ".zulipdev.com", ".zulipdev.org"]))
# Tell webpack-dev-server to fall back to periodic polling on
# filesystems where inotify is known to be broken.
cwd = os.getcwd()
inotify_broken = False
with open("/proc/self/mounts") as mounts:
for line in mounts:
fields = line.split()
if fields[1] == cwd:
inotify_broken = fields[2] in ["nfs", "vboxsf", "prl_fs"]
if inotify_broken:
os.environ["CHOKIDAR_USEPOLLING"] = "1"
os.environ["CHOKIDAR_INTERVAL"] = "1000"
# TODO: This process should also fall back to periodic polling if
# inotify_broken.
import pyinotify
try:
webpack_process = subprocess.Popen(webpack_args)
class WebpackConfigFileChangeHandler(pyinotify.ProcessEvent):
def process_default(self, event: pyinotify.Event) -> None:
nonlocal webpack_process
print("Restarting webpack-dev-server due to config changes...")
webpack_process.terminate()
webpack_process.wait()
webpack_process = subprocess.Popen(webpack_args)
watch_manager = pyinotify.WatchManager()
event_notifier = pyinotify.Notifier(watch_manager, WebpackConfigFileChangeHandler())
# We did a chdir to the root of the Zulip checkout above.
for filepath in [
"webpack.config.ts",
"tools/webpack.assets.json",
"tools/webpack.dev-assets.json",
]:
watch_manager.add_watch(filepath, pyinotify.IN_MODIFY)
event_notifier.loop()
finally:
webpack_process.terminate()
webpack_process.wait()
def build_for_most_tests() -> None:
"""Generates a stub asset stat file for django so backend test can render a page"""
# Tests like test-backend, test-api, and test-home-documentation use
# our "test" configuration. The one exception is Puppeteer, which uses
# our production configuration.
#
# It's not completely clear why we don't also use the same
# configuration for ALL tests, but figuring out the full history here
# was out of the scope of the effort here to add some comments and
# clean up names.
with open("tools/webpack.assets.json") as json_data:
entries = json.load(json_data)
with open("tools/webpack.dev-assets.json") as json_data:
entries.update(json.load(json_data))
stat_data = {
"status": "done",
"chunks": {entry: [f"{entry}-stubentry.js"] for entry in entries},
}
directory = "var"
if not os.path.exists(directory):
os.makedirs(directory)
with open(os.path.join(directory, "webpack-stats-test.json"), "w") as outfile:
json.dump(stat_data, outfile)
parser = argparse.ArgumentParser()
parser.add_argument(
"--test",
action="store_true",
help="generate a stub webpack-stats.json file (for backend testing)",
)
parser.add_argument("--quiet", action="store_true", help="Minimizes webpack output while running")
parser.add_argument(
"--watch", action="store_true", help="watch for changes to source files (for development)"
)
parser.add_argument(
"--host", default="127.0.0.1", help="set the host for the webpack server to run on"
)
parser.add_argument("--port", default="9994", help="set the port for the webpack server to run on")
parser.add_argument(
"--minify", action="store_true", help="Minify and optimize the assets (for development)"
)
parser.add_argument(
"--disable-host-check", action="store_true", help="Disable host check for webpack-dev-server"
)
args = parser.parse_args()
if "PUPPETEER_TESTS" in os.environ:
build_for_prod_or_puppeteer(args.quiet)
elif args.test:
build_for_most_tests()
elif args.watch:
build_for_dev_server(args.host, args.port, args.minify, args.disable_host_check)
else:
build_for_prod_or_puppeteer(args.quiet)