@@ -176,6 +176,7 @@ def module_from_file(
176176 resolve_exports_depth : int = 5 ,
177177 symlink : bool = False ,
178178 unmount_before_update : bool = False ,
179+ replace_existing : bool = False ,
179180) -> WebModule :
180181 """Load a :class:`WebModule` from a :data:`URL_SOURCE` using a known framework
181182
@@ -198,19 +199,26 @@ def module_from_file(
198199 only be used if the imported package failes to re-render when props change.
199200 Using this option has negative performance consequences since all DOM
200201 elements must be changed on each render. See :issue:`461` for more info.
202+ replace_existing:
203+ Whether to replace the source for a module with the same name if
204+ if already exists. Otherwise raise an error.
201205 """
202206 source_file = Path (file )
203207 target_file = _web_module_path (name )
204208 if not source_file .exists ():
205209 raise FileNotFoundError (f"Source file does not exist: { source_file } " )
206210 elif target_file .exists () or target_file .is_symlink ():
207- raise FileExistsError (f"{ name !r} already exists as { target_file .resolve ()} " )
208- else :
209- target_file .parent .mkdir (parents = True , exist_ok = True )
210- if symlink :
211- target_file .symlink_to (source_file )
211+ if not replace_existing :
212+ raise FileExistsError (f"{ name !r} already exists as { target_file .resolve ()} " )
212213 else :
213- shutil .copy (source_file , target_file )
214+ target_file .unlink ()
215+
216+ target_file .parent .mkdir (parents = True , exist_ok = True )
217+ if symlink :
218+ target_file .symlink_to (source_file )
219+ else :
220+ shutil .copy (source_file , target_file )
221+
214222 return WebModule (
215223 source = name + module_name_suffix (name ),
216224 source_type = NAME_SOURCE ,
0 commit comments