Skip to content

Commit 1950728

Browse files
committed
upgrade snowpack
1 parent 69238e0 commit 1950728

13 files changed

+931
-1884
lines changed

MANIFEST.in

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
graft idom/client/static/
2-
prune idom/client/static/node_modules
1+
graft idom/client/static/build
32
include idom/py.typed
43
include LICENSE
54
include requirements/prod.txt

idom/client/manage.py

+27-37
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,25 @@
44
from loguru import logger
55
from pathlib import Path
66
from tempfile import TemporaryDirectory
7-
from typing import Optional, List, Union, Dict, Any, Sequence
7+
from typing import Optional, List, Union, Dict, Sequence
88

99
from .utils import Spinner
1010

1111

1212
STATIC_DIR = Path(__file__).parent / "static"
13-
14-
CORE_MODULES = STATIC_DIR / "core_modules"
1513
NODE_MODULES = STATIC_DIR / "node_modules"
16-
WEB_MODULES = STATIC_DIR / "web_modules"
14+
BUILD_DIR = STATIC_DIR / "build"
15+
CORE_MODULES = BUILD_DIR / "core_modules"
16+
WEB_MODULES = BUILD_DIR / "web_modules"
17+
1718

1819
STATIC_SHIMS: Dict[str, Path] = {}
1920

2021

2122
def find_path(url_path: str) -> Optional[Path]:
2223
url_path = url_path.strip("/")
2324

24-
builtin_path = STATIC_DIR.joinpath(*url_path.split("/"))
25+
builtin_path = BUILD_DIR.joinpath(*url_path.split("/"))
2526
if builtin_path.exists():
2627
return builtin_path
2728

@@ -83,9 +84,9 @@ def delete_web_modules(names: Sequence[str], skip_missing: bool = False) -> None
8384

8485
def installed() -> List[str]:
8586
names: List[str] = []
86-
for path in WEB_MODULES.rglob("*"):
87-
if path.is_file() and path.suffix == ".js":
88-
rel_path = path.relative_to(WEB_MODULES)
87+
for path in WEB_MODULES.rglob("*.js"):
88+
rel_path = path.relative_to(WEB_MODULES)
89+
if rel_path.parent.name != "common":
8990
names.append(str(rel_path.with_suffix("")))
9091
return list(sorted(names))
9192

@@ -108,51 +109,40 @@ def install(
108109
for exp in export_list:
109110
delete_web_modules(exp, skip_missing=True)
110111

111-
package_json = _package_json()
112-
package_json["snowpack"]["webDependencies"].extend(export_list)
113-
114112
with TemporaryDirectory() as tempdir:
113+
if BUILD_DIR.exists():
114+
shutil.rmtree(BUILD_DIR)
115+
115116
tempdir_path = Path(tempdir)
117+
temp_static_dir = tempdir_path / "static"
118+
119+
shutil.copytree(STATIC_DIR, temp_static_dir, symlinks=True)
120+
assert (temp_static_dir / "package.json").exists()
116121

117-
if NODE_MODULES.exists():
118-
shutil.copytree(
119-
NODE_MODULES, tempdir_path / NODE_MODULES.name, symlinks=True
120-
)
122+
with open(temp_static_dir / "package.json") as f:
123+
package_json = json.load(f)
121124

122-
with (tempdir_path / "package.json").open("w+") as f:
125+
package_json["snowpack"].setdefault("install", []).extend(export_list)
126+
127+
with (temp_static_dir / "package.json").open("w+") as f:
123128
json.dump(package_json, f)
124129

125130
with Spinner(f"Installing: {', '.join(package_list)}"):
126-
_run_subprocess(["npm", "install"], tempdir)
127-
_run_subprocess(["npm", "install"] + package_list, tempdir)
128-
_run_subprocess(["npm", "run", "snowpack"], tempdir)
131+
_run_subprocess(["npm", "install"], temp_static_dir)
132+
_run_subprocess(["npm", "install"] + package_list, temp_static_dir)
133+
_run_subprocess(["npm", "run", "build"], temp_static_dir)
134+
135+
shutil.copytree(temp_static_dir / "build", BUILD_DIR, symlinks=True)
129136

130137

131138
def restore() -> None:
132139
with Spinner("Restoring"):
133140
_delete_os_paths(WEB_MODULES, NODE_MODULES)
134141
_run_subprocess(["npm", "install"], STATIC_DIR)
135-
_run_subprocess(["npm", "run", "snowpack"], STATIC_DIR)
142+
_run_subprocess(["npm", "run", "build"], STATIC_DIR)
136143
STATIC_SHIMS.clear()
137144

138145

139-
def _package_json() -> Dict[str, Any]:
140-
with (STATIC_DIR / "package.json").open("r") as f:
141-
dependencies = json.load(f)["dependencies"]
142-
143-
return {
144-
"dependencies": dependencies,
145-
"scripts": {"snowpack": "./node_modules/.bin/snowpack"},
146-
"devDependencies": {"snowpack": "^1.6.0"},
147-
"snowpack": {
148-
"installOptions": {
149-
"dest": str(WEB_MODULES),
150-
},
151-
"webDependencies": [],
152-
},
153-
}
154-
155-
156146
def _run_subprocess(args: List[str], cwd: Union[str, Path]) -> None:
157147
try:
158148
subprocess.run(

idom/client/static/core_modules/layout.js

+10-15
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
1-
import React, {
2-
useEffect,
3-
useState,
4-
useMemo,
5-
Suspense,
6-
} from "../web_modules/react.js";
71

8-
import ReactDOM from "../web_modules/react-dom.js";
9-
import htm from "../web_modules/htm.js";
10-
import * as jsonpatch from "../web_modules/fast-json-patch.js";
2+
import * as react from "react";
3+
import * as reactDOM from "react-dom";
4+
import htm from "htm";
5+
import * as jsonpatch from "fast-json-patch";
116

12-
import serializeEvent from "./event-to-object.js";
7+
import serializeEvent from "./event-to-object";
138

14-
const html = htm.bind(React.createElement);
9+
const html = htm.bind(react.createElement);
1510
const alreadyImported = {};
1611

1712
export function renderLayout(mountElement, endpoint) {
1813
const cmpt = html`<${Layout} endpoint=${endpoint} />`;
19-
return ReactDOM.render(cmpt, mountElement);
14+
return reactDOM.render(cmpt, mountElement);
2015
}
2116

2217
export default function Layout({ endpoint }) {
@@ -36,8 +31,8 @@ export default function Layout({ endpoint }) {
3631
endpoint = new_uri + endpoint;
3732
}
3833

39-
const socket = useMemo(() => new WebSocket(endpoint), [endpoint]);
40-
const [state, setState] = useState({ model: {} });
34+
const socket = react.useMemo(() => new WebSocket(endpoint), [endpoint]);
35+
const [state, setState] = react.useState({ model: {} });
4136

4237
socket.onmessage = (event) => {
4338
const [pathPrefix, patch] = JSON.parse(event.data);
@@ -152,7 +147,7 @@ function eventHandler(sendEvent, eventSpec) {
152147
}
153148

154149
function useLazyModule(source) {
155-
const [module, setModule] = useState(alreadyImported[source]);
150+
const [module, setModule] = react.useState(alreadyImported[source]);
156151
if (!module) {
157152
dynamicImport(source).then(setModule);
158153
}

0 commit comments

Comments
 (0)