Skip to content
This repository was archived by the owner on Oct 16, 2024. It is now read-only.

Commit d96abbc

Browse files
committed
Separate load importmap from generate
1 parent f93b6d7 commit d96abbc

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

importmap/core.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ def __init__(
4444
):
4545
self.config_filename = config_filename
4646
self.lock_filename = lock_filename
47-
self.config = {}
48-
self.map = {}
49-
self.map_dev = {}
47+
self.load()
5048

5149
def load(self):
5250
# TODO django check to compare lock and config hash
@@ -60,10 +58,18 @@ def load(self):
6058
self.delete_lockfile()
6159
return
6260

63-
config_hash = hash_for_data(self.config)
6461
lockfile = self.load_lockfile()
62+
if lockfile:
63+
self.map = lockfile["importmap"]
64+
self.map_dev = lockfile["importmap_dev"]
65+
else:
66+
self.map = {}
67+
self.map_dev = {}
6568

66-
if not lockfile or lockfile["config_hash"] != config_hash:
69+
def generate(self, force=False):
70+
config_hash = hash_for_data(self.config)
71+
lockfile = self.load_lockfile()
72+
if force or not lockfile or lockfile["config_hash"] != config_hash:
6773
# Generate both maps now, tag will choose which to use at runtime
6874
self.map = self.generate_map()
6975
self.map_dev = self.generate_map(development=True)
@@ -73,11 +79,6 @@ def load(self):
7379
lockfile["importmap_dev"] = self.map_dev
7480
self.save_lockfile(lockfile)
7581

76-
elif lockfile:
77-
# Use map from up-to-date lockfile
78-
self.map = lockfile["importmap"]
79-
self.map_dev = lockfile["importmap_dev"]
80-
8182
def load_config(self):
8283
# TODO raise custom exceptions
8384

importmap/management/commands/importmap_generate.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
class Command(BaseCommand):
77
help = "Generate importmap.lock"
88

9+
def add_arguments(self, parser):
10+
parser.add_argument("--force", action="store_true")
11+
912
def handle(self, *args, **options):
1013
importmap = Importmap()
11-
importmap.load()
12-
# force option to make sure a new lock is updated? or trust the logic?
14+
importmap.generate(force=options["force"])

importmap/templatetags/importmap.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
@register.inclusion_tag("importmap/scripts.html")
1212
def importmap_scripts():
1313
importmap = Importmap()
14-
importmap.load()
1514

1615
if settings.DEBUG:
1716
return {"importmap": json.dumps(importmap.map_dev, indent=2, sort_keys=True)}

0 commit comments

Comments
 (0)