44from loguru import logger
55from pathlib import Path
66from tempfile import TemporaryDirectory
7- from typing import Optional , List , Union , Dict , Sequence
7+ from typing import Optional , List , Union , Dict , Sequence , Any , cast
88
99from .utils import Spinner
1010
@@ -110,9 +110,6 @@ def install(
110110 delete_web_modules (exp , skip_missing = True )
111111
112112 with TemporaryDirectory () as tempdir :
113- if BUILD_DIR .exists ():
114- shutil .rmtree (BUILD_DIR )
115-
116113 tempdir_path = Path (tempdir )
117114 temp_static_dir = tempdir_path / "static"
118115
@@ -122,7 +119,15 @@ def install(
122119 with open (temp_static_dir / "package.json" ) as f :
123120 package_json = json .load (f )
124121
125- package_json ["snowpack" ].setdefault ("install" , []).extend (export_list )
122+ temp_build_dir = temp_static_dir / "build"
123+
124+ cache = _read_idom_build_cache (temp_build_dir )
125+
126+ export_list = list (set (cache ["export_list" ] + export_list ))
127+ package_list = list (set (cache ["package_list" ] + package_list ))
128+
129+ pkg_snowpack = package_json .setdefault ("snowpack" , {})
130+ pkg_snowpack .setdefault ("install" , []).extend (export_list )
126131
127132 with (temp_static_dir / "package.json" ).open ("w+" ) as f :
128133 json .dump (package_json , f )
@@ -132,7 +137,15 @@ def install(
132137 _run_subprocess (["npm" , "install" ] + package_list , temp_static_dir )
133138 _run_subprocess (["npm" , "run" , "build" ], temp_static_dir )
134139
135- shutil .copytree (temp_static_dir / "build" , BUILD_DIR , symlinks = True )
140+ cache ["export_list" ] = export_list
141+ cache ["package_list" ] = package_list
142+
143+ _write_idom_build_cache (temp_build_dir , cache )
144+
145+ if BUILD_DIR .exists ():
146+ shutil .rmtree (BUILD_DIR )
147+
148+ shutil .copytree (temp_build_dir , BUILD_DIR , symlinks = True )
136149
137150
138151def restore () -> None :
@@ -163,5 +176,23 @@ def _delete_os_paths(*paths: Path) -> None:
163176 shutil .rmtree (p )
164177
165178
179+ def _read_idom_build_cache (path : Path ) -> Dict [str , Any ]:
180+ cache_file = path / ".idom-cache.json"
181+ if not cache_file .exists ():
182+ return {
183+ "package_list" : [],
184+ "export_list" : [],
185+ }
186+ else :
187+ with cache_file .open () as f :
188+ return cast (Dict [str , Any ], json .load (f ))
189+
190+
191+ def _write_idom_build_cache (path : Path , cache : Dict [str , Any ]) -> None :
192+ cache_file = path / ".idom-cache.json"
193+ with cache_file .open ("w+" ) as f :
194+ json .dump (cache , f )
195+
196+
166197def _to_list_of_str (value : Sequence [str ]) -> List [str ]:
167198 return [value ] if isinstance (value , str ) else list (value )
0 commit comments