@@ -176,6 +176,7 @@ def module_from_file(
176
176
resolve_exports_depth : int = 5 ,
177
177
symlink : bool = False ,
178
178
unmount_before_update : bool = False ,
179
+ replace_existing : bool = True ,
179
180
) -> WebModule :
180
181
"""Load a :class:`WebModule` from a :data:`URL_SOURCE` using a known framework
181
182
@@ -198,19 +199,26 @@ def module_from_file(
198
199
only be used if the imported package failes to re-render when props change.
199
200
Using this option has negative performance consequences since all DOM
200
201
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.
201
205
"""
202
206
source_file = Path (file )
203
207
target_file = _web_module_path (name )
204
208
if not source_file .exists ():
205
209
raise FileNotFoundError (f"Source file does not exist: { source_file } " )
206
210
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 ()} " )
212
213
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
+
214
222
return WebModule (
215
223
source = name + module_name_suffix (name ),
216
224
source_type = NAME_SOURCE ,
0 commit comments