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

Commit df44b1b

Browse files
authored
Move config to pyproject.yaml (#25)
1 parent d2cb610 commit df44b1b

File tree

8 files changed

+179
-237
lines changed

8 files changed

+179
-237
lines changed

README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,23 @@ INSTALLED_APPS = [
3131
]
3232
```
3333

34-
### 2. Create an `importmap.toml` file
34+
### 2. Configuring an import map
3535

36-
This should live next to your `manage.py` file.
37-
Here you'll add a list of "packages" you want to use.
36+
You JavaScript dependencies are conveniently located in your`pyproject.toml` file.
3837

39-
The "name" can be anything, but should probably be the same as what it you would import from in typical bundling setups (i.e. `import React from "react"`).
40-
41-
The "source" will get passed on to the [jspm.org generator](https://jspm.org/docs/api#install), but is basically the `<npm package>@<version>` you want to use.
38+
They are listed under `[tool.importmap.dependencies]` and you can add them there. The format is `name = "version"`,
39+
similar to how you would add a dependency to your `package.json` file.
4240

4341
```toml
44-
[[packages]]
45-
name = "react"
46-
source = "react@17.0.2"
42+
# pyproject.toml
43+
[tool.importmap.dependencies]
44+
react = "17.0.2"
45+
react-dom = "17.0.2"
4746
```
4847

48+
[jspm.org generator](https://jspm.org/docs/api#install) is used lock and serve the dependencies,
49+
but is basically just like installing them via `npm i <npm package>@<version>`.
50+
4951
### 3. Run `importmap_generate`
5052

5153
To resolve the import map, you'll need to run `python manage.py importmap_generate`.

importmap/core.py

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,22 @@
22
import json
33
import logging
44
import os
5+
from pathlib import Path
56

6-
import tomli
7-
from marshmallow import Schema, fields
7+
try:
8+
import tomllib
9+
except ImportError:
10+
import tomli as tomllib
811

912
from .generator import ImportmapGenerator
1013

1114
logger = logging.getLogger(__name__)
1215

1316

14-
DEFAULT_CONFIG_FILENAME = "importmap.toml"
17+
DEFAULT_CONFIG_FILENAME = "pyproject.toml"
1518
DEFAULT_LOCK_FILENAME = "importmap.lock"
1619

1720

18-
class PackageSchema(Schema):
19-
name = fields.String(required=True)
20-
source = fields.String(required=True)
21-
# preload
22-
# vendor, or vendor all is one option?
23-
24-
25-
class ConfigSchema(Schema):
26-
packages = fields.List(fields.Nested(PackageSchema), required=True)
27-
28-
29-
class LockfileSchema(Schema):
30-
config_hash = fields.String(required=True)
31-
importmap = fields.Dict(required=True)
32-
importmap_dev = fields.Dict(required=True)
33-
34-
3521
def hash_for_data(data):
3622
return hashlib.md5(json.dumps(data, sort_keys=True).encode("utf-8")).hexdigest()
3723

@@ -42,8 +28,8 @@ def __init__(
4228
config_filename=DEFAULT_CONFIG_FILENAME,
4329
lock_filename=DEFAULT_LOCK_FILENAME,
4430
):
45-
self.config_filename = config_filename
46-
self.lock_filename = lock_filename
31+
self.config_file = Path(config_filename)
32+
self.lock_file = Path(lock_filename)
4733
self.load()
4834

4935
@classmethod
@@ -70,7 +56,7 @@ def load(self):
7056
# No config = no map and no lockfile
7157
self.map = {}
7258
self.map_dev = {}
73-
self.delete_lockfile()
59+
self.lock_file.unlink(missing_ok=True)
7460
return
7561

7662
lockfile = self.load_lockfile()
@@ -95,34 +81,23 @@ def generate(self, force=False):
9581
self.save_lockfile(lockfile)
9682

9783
def load_config(self):
98-
# TODO raise custom exceptions
99-
100-
if not os.path.exists(self.config_filename):
101-
logger.warning(f"{self.config_filename} not found")
84+
if not self.config_file.exists():
10285
return {}
86+
with self.config_file.open("rb") as f:
87+
pyproject = tomllib.load(f)
10388

104-
with open(self.config_filename, "r") as f:
105-
# why doesn't tomli.load(f) work?
106-
toml_data = tomli.loads(f.read())
107-
108-
return ConfigSchema().load(toml_data)
89+
return pyproject["tool"]["importmap"]
10990

11091
def load_lockfile(self):
111-
if not os.path.exists(self.lock_filename):
92+
if not self.lock_file.exists():
11293
return {}
11394

114-
with open(self.lock_filename, "r") as f:
115-
json_data = json.load(f)
116-
117-
return LockfileSchema().load(json_data)
95+
with self.lock_file.open("r") as f:
96+
return json.load(f)
11897

11998
def save_lockfile(self, lockfile):
120-
with open(self.lock_filename, "w+") as f:
99+
with self.lock_file.open("w+") as f:
121100
json.dump(lockfile, f, indent=2, sort_keys=True)
122101

123-
def delete_lockfile(self):
124-
if os.path.exists(self.lock_filename):
125-
os.remove(self.lock_filename)
126-
127102
def generate_map(self, *args, **kwargs):
128103
return ImportmapGenerator.from_config(self.config, *args, **kwargs).generate()

importmap/generator.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ def __init__(self, targets, development=False, provider="jspm"):
1717

1818
@classmethod
1919
def from_config(cls, config, *args, **kwargs):
20-
targets = []
21-
22-
for map in config["packages"]:
23-
targets.append(map["source"])
24-
25-
return cls(targets, *args, **kwargs)
20+
return cls(
21+
[
22+
f"{package}@{version}"
23+
for package, version in config["dependencies"].items()
24+
],
25+
*args,
26+
**kwargs,
27+
)
2628

2729
def get_env(self):
2830
if self.development:

0 commit comments

Comments
 (0)