Skip to content

Commit 2e82d19

Browse files
committed
fully functional build sequence for JS
1 parent bd7f40d commit 2e82d19

File tree

7 files changed

+107
-12
lines changed

7 files changed

+107
-12
lines changed

pyproject.toml

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,8 @@ installer = "uv"
5858

5959
[[tool.hatch.build.hooks.build-scripts.scripts]]
6060
commands = [
61-
'bun install --cwd "src/js/packages/@reactpy/app" --production --frozen-lockfile',
62-
# TODO: Remove 'assets' from this path after turning ReactPy into ASGI middleware.
63-
'bun build "./src/js/packages/@reactpy/app/src/index.ts" --outdir "./src/reactpy/static/assets/" --minify --sourcemap=linked',
61+
"hatch run javascript:build",
62+
'hatch run "src/build_scripts/copy_dir.py" "src/js/packages/@reactpy/app/dist" "src/reactpy/static/assets"',
6463
]
6564
artifacts = []
6665

@@ -166,13 +165,23 @@ type_check = "mypy --strict src/reactpy"
166165
detached = true
167166

168167
[tool.hatch.envs.javascript.scripts]
169-
check = [
170-
'bun install --cwd "src/js" --frozen-lockfile',
171-
'bun run --cwd "src/js" check',
168+
check = ['bun install --cwd "src/js"', 'bun run --cwd "src/js" check']
169+
fix = ['bun install --cwd "src/js"', 'bun run --cwd "src/js" format']
170+
build = [
171+
'hatch run "src/build_scripts/clean_js_dir.py"',
172+
'hatch run javascript:build_packages',
173+
'hatch run javascript:build_app',
172174
]
173-
fix = [
174-
'bun install --cwd "src/js" --frozen-lockfile',
175-
'bun run --cwd "src/js" format',
175+
build_packages = [
176+
'bun install --cwd "src/js"',
177+
'bun install --cwd "src/js/packages/event-to-object"',
178+
'bun install --cwd "src/js/packages/@reactpy/client"',
179+
'bun run --cwd "src/js/packages/event-to-object" build',
180+
'bun run --cwd "src/js/packages/@reactpy/client" build',
181+
]
182+
build_app = [
183+
'bun install --cwd "src/js/packages/@reactpy/app"',
184+
'bun run --cwd "src/js/packages/@reactpy/app" build',
176185
]
177186

178187
#########################

src/build_scripts/clean_js_dir.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# /// script
2+
# requires-python = ">=3.11"
3+
# dependencies = []
4+
# ///
5+
6+
# Deletes `dist`, `node_modules`, and `tsconfig.tsbuildinfo` from all JS packages in the JS source directory.
7+
8+
import contextlib
9+
import glob
10+
import os
11+
import pathlib
12+
import shutil
13+
14+
# Get the path to the JS source directory
15+
js_src_dir = pathlib.Path(__file__).parent.parent / "js"
16+
17+
# Get the paths to all `dist` folders in the JS source directory
18+
dist_dirs = glob.glob(str(js_src_dir / "**/dist"), recursive=True)
19+
20+
# Get the paths to all `node_modules` folders in the JS source directory
21+
node_modules_dirs = glob.glob(str(js_src_dir / "**/node_modules"), recursive=True)
22+
23+
# Get the paths to all `tsconfig.tsbuildinfo` files in the JS source directory
24+
tsconfig_tsbuildinfo_files = glob.glob(
25+
str(js_src_dir / "**/tsconfig.tsbuildinfo"), recursive=True
26+
)
27+
28+
# Delete all `dist` folders
29+
for dist_dir in dist_dirs:
30+
with contextlib.suppress(FileNotFoundError):
31+
shutil.rmtree(dist_dir)
32+
33+
# Delete all `node_modules` folders
34+
for node_modules_dir in node_modules_dirs:
35+
with contextlib.suppress(FileNotFoundError):
36+
shutil.rmtree(node_modules_dir)
37+
38+
# Delete all `tsconfig.tsbuildinfo` files
39+
for tsconfig_tsbuildinfo_file in tsconfig_tsbuildinfo_files:
40+
with contextlib.suppress(FileNotFoundError):
41+
os.remove(tsconfig_tsbuildinfo_file)

src/build_scripts/copy_dir.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# /// script
2+
# requires-python = ">=3.11"
3+
# dependencies = []
4+
# ///
5+
6+
# ruff: noqa: INP001
7+
import logging
8+
import shutil
9+
import sys
10+
from pathlib import Path
11+
12+
13+
def copy_files(source: Path, destination: Path) -> None:
14+
if destination.exists():
15+
shutil.rmtree(destination)
16+
destination.mkdir()
17+
18+
for file in source.iterdir():
19+
if file.is_file():
20+
shutil.copy(file, destination / file.name)
21+
else:
22+
copy_files(file, destination / file.name)
23+
24+
25+
if __name__ == "__main__":
26+
if len(sys.argv) != 3:
27+
logging.error(
28+
"Script used incorrectly!\nUsage: python copy_dir.py <source_dir> <destination>"
29+
)
30+
sys.exit(1)
31+
32+
root_dir = Path(__file__).parent.parent.parent
33+
src = Path(root_dir / sys.argv[1])
34+
dest = Path(root_dir / sys.argv[2])
35+
36+
if not src.exists():
37+
logging.error("Source directory %s does not exist", src)
38+
sys.exit(1)
39+
40+
copy_files(src, dest)
14.2 KB
Binary file not shown.

src/js/packages/@reactpy/app/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
"description": "ReactPy's client-side entry point. This is strictly for internal use and is not designed to be distributed.",
44
"license": "MIT",
55
"dependencies": {
6-
"@reactpy/client": "^0.3.2",
7-
"event-to-object": "^0.1.2",
6+
"@reactpy/client": "file:../client",
7+
"event-to-object": "file:../../event-to-object",
88
"preact": "^10.7.0"
99
},
1010
"devDependencies": {
1111
"@types/react": "^17.0",
1212
"@types/react-dom": "^17.0"
13+
},
14+
"scripts": {
15+
"build": "bun build \"src/index.ts\" --outdir \"dist\" --minify --sourcemap=linked"
1316
}
1417
}
0 Bytes
Binary file not shown.

src/js/packages/@reactpy/client/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
"type": "module",
1818
"main": "dist/index.js",
1919
"dependencies": {
20-
"event-to-object": "^0.1.2",
2120
"json-pointer": "^0.6.2",
2221
"preact": "^10.25.4"
2322
},
@@ -26,6 +25,9 @@
2625
"@types/react": "^17.0",
2726
"@types/react-dom": "^17.0"
2827
},
28+
"peerDependencies": {
29+
"event-to-object": "<1.0.0"
30+
},
2931
"scripts": {
3032
"build": "tsc -b",
3133
"checkTypes": "tsc --noEmit"

0 commit comments

Comments
 (0)