pyesm 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyesm-0.1.0/.github/workflows/ci.yml +44 -0
- pyesm-0.1.0/.github/workflows/release.yml +44 -0
- pyesm-0.1.0/.gitignore +19 -0
- pyesm-0.1.0/.pre-commit-config.yaml +27 -0
- pyesm-0.1.0/LICENSE +21 -0
- pyesm-0.1.0/PKG-INFO +316 -0
- pyesm-0.1.0/README.md +296 -0
- pyesm-0.1.0/pyproject.toml +68 -0
- pyesm-0.1.0/src/pyesm/__init__.py +3 -0
- pyesm-0.1.0/src/pyesm/__main__.py +4 -0
- pyesm-0.1.0/src/pyesm/_pyproject.py +100 -0
- pyesm-0.1.0/src/pyesm/cache.py +93 -0
- pyesm-0.1.0/src/pyesm/cli.py +340 -0
- pyesm-0.1.0/src/pyesm/config.py +125 -0
- pyesm-0.1.0/src/pyesm/contrib/__init__.py +0 -0
- pyesm-0.1.0/src/pyesm/contrib/django/__init__.py +1 -0
- pyesm-0.1.0/src/pyesm/contrib/django/apps.py +7 -0
- pyesm-0.1.0/src/pyesm/contrib/django/rendering.py +87 -0
- pyesm-0.1.0/src/pyesm/contrib/django/templatetags/__init__.py +0 -0
- pyesm-0.1.0/src/pyesm/contrib/django/templatetags/pyesm.py +12 -0
- pyesm-0.1.0/src/pyesm/crawler.py +103 -0
- pyesm-0.1.0/src/pyesm/errors.py +39 -0
- pyesm-0.1.0/src/pyesm/hashing.py +25 -0
- pyesm-0.1.0/src/pyesm/http.py +19 -0
- pyesm-0.1.0/src/pyesm/importmap.py +70 -0
- pyesm-0.1.0/src/pyesm/lockfile.py +137 -0
- pyesm-0.1.0/src/pyesm/providers/__init__.py +22 -0
- pyesm-0.1.0/src/pyesm/providers/base.py +86 -0
- pyesm-0.1.0/src/pyesm/providers/esmsh.py +64 -0
- pyesm-0.1.0/src/pyesm/providers/jsdelivr.py +72 -0
- pyesm-0.1.0/src/pyesm/resolve.py +112 -0
- pyesm-0.1.0/src/pyesm/scanner.py +47 -0
- pyesm-0.1.0/src/pyesm/shims.py +22 -0
- pyesm-0.1.0/src/pyesm/vendor.py +131 -0
- pyesm-0.1.0/tests/conftest.py +63 -0
- pyesm-0.1.0/tests/fake_cdn.py +143 -0
- pyesm-0.1.0/tests/test_cache_vendor.py +82 -0
- pyesm-0.1.0/tests/test_cli.py +87 -0
- pyesm-0.1.0/tests/test_config.py +121 -0
- pyesm-0.1.0/tests/test_crawler_resolve.py +65 -0
- pyesm-0.1.0/tests/test_django.py +170 -0
- pyesm-0.1.0/tests/test_http_live.py +24 -0
- pyesm-0.1.0/tests/test_importmap.py +75 -0
- pyesm-0.1.0/tests/test_lockfile.py +62 -0
- pyesm-0.1.0/tests/test_providers.py +51 -0
- pyesm-0.1.0/tests/test_pyproject_edit.py +84 -0
- pyesm-0.1.0/tests/test_scanner.py +34 -0
- pyesm-0.1.0/tests/test_shims.py +18 -0
- pyesm-0.1.0/uv.lock +402 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
|
|
8
|
+
permissions:
|
|
9
|
+
contents: read
|
|
10
|
+
|
|
11
|
+
concurrency:
|
|
12
|
+
group: ${{ github.workflow }}-${{ github.ref }}
|
|
13
|
+
cancel-in-progress: true
|
|
14
|
+
|
|
15
|
+
jobs:
|
|
16
|
+
lint:
|
|
17
|
+
runs-on: ubuntu-latest
|
|
18
|
+
steps:
|
|
19
|
+
- uses: actions/checkout@v4
|
|
20
|
+
- uses: astral-sh/setup-uv@v5
|
|
21
|
+
with:
|
|
22
|
+
enable-cache: true
|
|
23
|
+
- run: uv sync
|
|
24
|
+
- uses: actions/cache@v4
|
|
25
|
+
with:
|
|
26
|
+
path: ~/.cache/pre-commit
|
|
27
|
+
key: pre-commit-${{ hashFiles('.pre-commit-config.yaml') }}
|
|
28
|
+
- run: uv run pre-commit run --all-files --show-diff-on-failure
|
|
29
|
+
|
|
30
|
+
test:
|
|
31
|
+
runs-on: ubuntu-latest
|
|
32
|
+
strategy:
|
|
33
|
+
fail-fast: false
|
|
34
|
+
matrix:
|
|
35
|
+
python-version: ["3.12", "3.13"]
|
|
36
|
+
env:
|
|
37
|
+
UV_PYTHON: ${{ matrix.python-version }}
|
|
38
|
+
steps:
|
|
39
|
+
- uses: actions/checkout@v4
|
|
40
|
+
- uses: astral-sh/setup-uv@v5
|
|
41
|
+
with:
|
|
42
|
+
enable-cache: true
|
|
43
|
+
- run: uv sync
|
|
44
|
+
- run: uv run pytest -q
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
name: Release
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags: ["v*"]
|
|
6
|
+
|
|
7
|
+
permissions:
|
|
8
|
+
contents: read
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
build:
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
steps:
|
|
14
|
+
- uses: actions/checkout@v4
|
|
15
|
+
- uses: astral-sh/setup-uv@v5
|
|
16
|
+
|
|
17
|
+
- name: Check tag matches project version
|
|
18
|
+
run: |
|
|
19
|
+
version=$(grep -m1 '^version = ' pyproject.toml | sed 's/.*"\(.*\)".*/\1/')
|
|
20
|
+
tag="${GITHUB_REF_NAME#v}"
|
|
21
|
+
if [ "$version" != "$tag" ]; then
|
|
22
|
+
echo "tag $tag does not match pyproject version $version" >&2
|
|
23
|
+
exit 1
|
|
24
|
+
fi
|
|
25
|
+
|
|
26
|
+
- run: uv build
|
|
27
|
+
|
|
28
|
+
- uses: actions/upload-artifact@v4
|
|
29
|
+
with:
|
|
30
|
+
name: dist
|
|
31
|
+
path: dist/
|
|
32
|
+
|
|
33
|
+
publish:
|
|
34
|
+
needs: build
|
|
35
|
+
runs-on: ubuntu-latest
|
|
36
|
+
environment: pypi
|
|
37
|
+
permissions:
|
|
38
|
+
id-token: write # trusted publishing (OIDC); no API token needed
|
|
39
|
+
steps:
|
|
40
|
+
- uses: actions/download-artifact@v4
|
|
41
|
+
with:
|
|
42
|
+
name: dist
|
|
43
|
+
path: dist/
|
|
44
|
+
- uses: pypa/gh-action-pypi-publish@release/v1
|
pyesm-0.1.0/.gitignore
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
repos:
|
|
2
|
+
- repo: https://github.com/astral-sh/ruff-pre-commit
|
|
3
|
+
rev: v0.15.16
|
|
4
|
+
hooks:
|
|
5
|
+
- id: ruff-check
|
|
6
|
+
args: [--fix]
|
|
7
|
+
- id: ruff-format
|
|
8
|
+
|
|
9
|
+
- repo: https://github.com/pre-commit/pre-commit-hooks
|
|
10
|
+
rev: v5.0.0
|
|
11
|
+
hooks:
|
|
12
|
+
- id: trailing-whitespace
|
|
13
|
+
- id: end-of-file-fixer
|
|
14
|
+
- id: check-toml
|
|
15
|
+
- id: check-yaml
|
|
16
|
+
- id: check-merge-conflict
|
|
17
|
+
- id: check-added-large-files
|
|
18
|
+
|
|
19
|
+
- repo: local
|
|
20
|
+
hooks:
|
|
21
|
+
# Run pyright through uv so it resolves against the project venv.
|
|
22
|
+
- id: pyright
|
|
23
|
+
name: pyright
|
|
24
|
+
entry: uv run pyright
|
|
25
|
+
language: system
|
|
26
|
+
types: [python]
|
|
27
|
+
pass_filenames: false
|
pyesm-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 novucs
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
pyesm-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pyesm
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Fast, Python-native, npm-free ESM dependency vendoring with import maps and Subresource Integrity
|
|
5
|
+
Project-URL: Homepage, https://github.com/novucs/pyesm
|
|
6
|
+
Author: novucs
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Keywords: cdn,django,esm,importmap,jsdelivr,sri,static
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Classifier: Topic :: Software Development :: Build Tools
|
|
15
|
+
Requires-Python: >=3.12
|
|
16
|
+
Requires-Dist: httpx>=0.27
|
|
17
|
+
Provides-Extra: django
|
|
18
|
+
Requires-Dist: django>=4.2; extra == 'django'
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
# pyesm
|
|
22
|
+
|
|
23
|
+
A fast, Python-native, **npm-free** tool that reads ESM dependencies from `pyproject.toml`,
|
|
24
|
+
vendors the compiled module graph from a CDN into a local static directory, and emits a standard
|
|
25
|
+
**import map** with **Subresource Integrity (SRI)** on by default.
|
|
26
|
+
|
|
27
|
+
- **No Node, no npm, no bundler.** Pure Python: `pip install pyesm` (or `uv add pyesm`) and go.
|
|
28
|
+
- **Framework-agnostic core** writes a static `importmap.json` + vendored files.
|
|
29
|
+
- **Optional Django integration** renders the import map through `staticfiles` storage at request
|
|
30
|
+
time, so it survives `ManifestStaticFilesStorage` / WhiteNoise filename hashing.
|
|
31
|
+
- Deterministic, lockfile-driven, SRI on by default.
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## Install
|
|
36
|
+
|
|
37
|
+
```console
|
|
38
|
+
$ pip install pyesm # or: uv add pyesm
|
|
39
|
+
$ pip install "pyesm[django]" # with the optional Django integration
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
- **Python 3.12+.**
|
|
43
|
+
- Runtime dependencies are minimal: just `httpx`.
|
|
44
|
+
- **No Node toolchain and no compiled extensions**, ever.
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
## Quick start
|
|
49
|
+
|
|
50
|
+
```console
|
|
51
|
+
$ pyesm add react@^18.2.0 react-dom@^18.2.0 # resolve, lock, vendor, and write the import map
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
```html
|
|
55
|
+
<script type="importmap" src="/static/pyesm/importmap.json"></script>
|
|
56
|
+
<script type="module">import "react"</script>
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
That's it: `react` (and its whole module graph) now loads from your own static files, with
|
|
60
|
+
integrity enforced and zero requests to the CDN at runtime. (Drop the `@range` to take the latest.)
|
|
61
|
+
|
|
62
|
+
Already have deps in `pyproject.toml`? Skip `add` and run `pyesm sync`.
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
## How it works
|
|
67
|
+
|
|
68
|
+
The design follows four load-bearing decisions:
|
|
69
|
+
|
|
70
|
+
1. **The CDN resolves and pins; pyesm crawls.** We don't reimplement npm semver. We ask a CDN's ESM
|
|
71
|
+
endpoint for `name@range`, pin it to an exact version (jsDelivr via its data API, esm.sh via
|
|
72
|
+
redirect), then crawl the returned module graph.
|
|
73
|
+
2. **Relocate via the import map, never by editing bytes.** CDN-built ESM references sibling modules
|
|
74
|
+
by *root-relative* path (e.g. `/npm/react@18.3.1/+esm`). pyesm adds each such specifier to the
|
|
75
|
+
import map as a key pointing at the local vendored copy. The browser resolves the specifier against
|
|
76
|
+
your site's origin and the map transparently redirects it to the local file. Vendored `.js` is
|
|
77
|
+
written byte-for-byte as fetched.
|
|
78
|
+
3. **No fragile relative edges.** Because cross-module references are absolute (root-relative) paths,
|
|
79
|
+
the import map is the single indirection layer; there is nothing to rewrite inside the files.
|
|
80
|
+
4. **Integrity is computed over the vendored bytes.** Every module gets a `sha384` stored in the lock;
|
|
81
|
+
`sync` recomputes and verifies on every run and **fails loudly** on mismatch (the CDN changed bytes
|
|
82
|
+
under a pinned URL) rather than silently overwriting. By default the import map also carries an SRI
|
|
83
|
+
`integrity` entry for every URL (opt out with `integrity = false`). Because bytes are never edited,
|
|
84
|
+
the hash stays valid even when `ManifestStaticFilesStorage` renames the *file*.
|
|
85
|
+
|
|
86
|
+
A **global content-addressed cache** (`~/.cache/pyesm/<hash>`) is shared across all projects;
|
|
87
|
+
identical modules are downloaded once, ever, and hardlinked into each project's output directory.
|
|
88
|
+
|
|
89
|
+
---
|
|
90
|
+
|
|
91
|
+
## Configuration
|
|
92
|
+
|
|
93
|
+
All configuration lives under `[tool.pyesm]` in `pyproject.toml`.
|
|
94
|
+
|
|
95
|
+
| Key | Default | Meaning |
|
|
96
|
+
|---------------|---------------------------------|--------------------------------------------------------------------------|
|
|
97
|
+
| `provider` | `"jsdelivr"` | CDN to vendor from: `jsdelivr` or `esmsh`. |
|
|
98
|
+
| `output-dir` | `"static/pyesm"` | Where vendored files are written (relative to project root). |
|
|
99
|
+
| `base-url` | `"/static/pyesm/"` | Public URL prefix used in the **static** import map. Must end with `/`. |
|
|
100
|
+
| `importmap` | `"static/pyesm/importmap.json"` | Output path for the static import map. |
|
|
101
|
+
| `production` | `true` | Request production (vs dev) builds where the CDN distinguishes (esm.sh). |
|
|
102
|
+
| `shims` | `"auto"` | es-module-shims injection: `auto`, `always`, or `never`. |
|
|
103
|
+
| `concurrency` | `16` | Max parallel downloads. |
|
|
104
|
+
| `integrity` | `true` | Emit the SRI `integrity` block in the import map. |
|
|
105
|
+
|
|
106
|
+
Dependencies go in a separate table. Keys containing dots, slashes, or scopes must be quoted:
|
|
107
|
+
|
|
108
|
+
```toml
|
|
109
|
+
[tool.pyesm.dependencies]
|
|
110
|
+
react = "^18.2.0"
|
|
111
|
+
"react-dom" = "^18.2.0"
|
|
112
|
+
lit = "3"
|
|
113
|
+
"htmx.org" = "2"
|
|
114
|
+
"@scope/pkg" = "1.2.3"
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
|
|
119
|
+
## CLI reference
|
|
120
|
+
|
|
121
|
+
The single entry point is `pyesm`. Running it bare prints help.
|
|
122
|
+
|
|
123
|
+
| Command | Network? | Behavior |
|
|
124
|
+
|--------------------------------|--------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
125
|
+
| `pyesm add <pkg>[@range] …` | yes | Add to `[tool.pyesm.dependencies]`, re-resolve, update lock, vendor. |
|
|
126
|
+
| `pyesm remove <pkg> …` | yes | Remove from deps, re-resolve, prune now-unused vendored files. |
|
|
127
|
+
| `pyesm lock` | yes | Re-resolve from `pyproject.toml`, rewrite `pyesm.lock`. |
|
|
128
|
+
| `pyesm sync` (alias `install`) | only if cold | Make local files + import map match the lock; download missing modules and verify every integrity. **Offline & near-instant when the cache is warm.** |
|
|
129
|
+
| `pyesm build` | no | (Re)emit the static `importmap.json` from the lock. |
|
|
130
|
+
| `pyesm clean` | no | Remove the contents of `output-dir` (keeps the lock). |
|
|
131
|
+
| `pyesm outdated` | yes | Report deps whose range now resolves to a newer pinned version. |
|
|
132
|
+
|
|
133
|
+
`add` accepts version ranges inline, scope-aware:
|
|
134
|
+
|
|
135
|
+
```console
|
|
136
|
+
$ pyesm add lit@3 "@scope/pkg@1.2.3"
|
|
137
|
+
$ pyesm remove react-dom
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Global flags
|
|
141
|
+
|
|
142
|
+
| Flag | Effect |
|
|
143
|
+
|------------------|-------------------------------------------------------------------------------|
|
|
144
|
+
| `--frozen` | Fail if `pyesm.lock` is missing or stale. Never mutates the lock (a CI gate). |
|
|
145
|
+
| `--offline` | Never hit the network; fail if a needed module isn't cached. |
|
|
146
|
+
| `--provider <p>` | Override the configured provider for this run. |
|
|
147
|
+
| `-q` / `-v` | Quieter / more verbose output. |
|
|
148
|
+
| `--version` | Print the pyesm version. |
|
|
149
|
+
|
|
150
|
+
---
|
|
151
|
+
|
|
152
|
+
## The lockfile (`pyesm.lock`)
|
|
153
|
+
|
|
154
|
+
`lock` writes a deterministic JSON lockfile next to `pyproject.toml`. **Commit it**: it drives
|
|
155
|
+
reproducible, offline `sync` in CI and deploys. It captures:
|
|
156
|
+
|
|
157
|
+
- `provider` and `inputs_hash`: a hash of the resolved dependency table; lets `sync` skip
|
|
158
|
+
re-resolution when `pyproject.toml` is unchanged.
|
|
159
|
+
- `imports`: each bare specifier → its pinned entry-module URL.
|
|
160
|
+
- `modules`: every node in the crawled graph: `url` (canonical CDN URL), `path` (local file),
|
|
161
|
+
`integrity` (`sha384-…`), `deps`, and `keys` (the root-relative specifiers that map to it).
|
|
162
|
+
|
|
163
|
+
Two `lock` runs on an unchanged `pyproject.toml` produce byte-identical files (modulo genuine CDN
|
|
164
|
+
drift, which surfaces as an explicit failure, never a silent change).
|
|
165
|
+
|
|
166
|
+
---
|
|
167
|
+
|
|
168
|
+
## Static mode (default)
|
|
169
|
+
|
|
170
|
+
`pyesm build` (and `sync`) writes `importmap.json` using `base-url` to form public URLs. Embed it
|
|
171
|
+
however you like:
|
|
172
|
+
|
|
173
|
+
```html
|
|
174
|
+
<!-- external -->
|
|
175
|
+
<script type="importmap" src="/static/pyesm/importmap.json"></script>
|
|
176
|
+
|
|
177
|
+
<!-- or inline the JSON contents directly into a <script type="importmap"> -->
|
|
178
|
+
|
|
179
|
+
<script type="module">import "react"</script>
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
### es-module-shims and cross-browser SRI
|
|
183
|
+
|
|
184
|
+
Native import-map `integrity` shipped in Chromium and Safari, but not everywhere; browsers that don't
|
|
185
|
+
understand the `integrity` key silently ignore it and load modules **unverified**. To enforce SRI
|
|
186
|
+
everywhere, pyesm can inject the [es-module-shims](https://github.com/guybedford/es-module-shims)
|
|
187
|
+
polyfill, controlled by `shims`:
|
|
188
|
+
|
|
189
|
+
- `auto` (default): vendor and inject the polyfill so integrity is enforced even where the browser
|
|
190
|
+
wouldn't.
|
|
191
|
+
- `always`: same as auto.
|
|
192
|
+
- `never`: don't vendor or inject.
|
|
193
|
+
|
|
194
|
+
The polyfill is **vendored** like every other file: downloaded once (at lock/sync) from the
|
|
195
|
+
configured provider, stored in the lock with its own `sha384`, and served from `output-dir` with an
|
|
196
|
+
`integrity` attribute. Production makes no CDN request for it. In Django mode the `<script>` tag is
|
|
197
|
+
emitted for you; in static mode reference the vendored file yourself
|
|
198
|
+
(`<base-url>es-module-shims@<version>.js`, integrity in the lock).
|
|
199
|
+
|
|
200
|
+
---
|
|
201
|
+
|
|
202
|
+
## Django integration
|
|
203
|
+
|
|
204
|
+
Add the app to your settings:
|
|
205
|
+
|
|
206
|
+
```python
|
|
207
|
+
INSTALLED_APPS = [
|
|
208
|
+
# …
|
|
209
|
+
"pyesm.contrib.django",
|
|
210
|
+
]
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
Render the map at request time with the template tag:
|
|
214
|
+
|
|
215
|
+
```django
|
|
216
|
+
{% load pyesm %}
|
|
217
|
+
<head>
|
|
218
|
+
{% pyesm_importmap %} {# emits <script type="importmap">…</script>, plus the shims tag per `shims` #}
|
|
219
|
+
</head>
|
|
220
|
+
|
|
221
|
+
<script type="module">import "react"</script>
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
Why request-time instead of a static file: the tag routes **only the values** through
|
|
225
|
+
`staticfiles_storage.url("pyesm/<path>")`, so the rendered map contains the storage-hashed URL
|
|
226
|
+
(e.g. `/static/pyesm/react@18.3.1/+esm.4af3.js`). This makes it survive
|
|
227
|
+
`ManifestStaticFilesStorage` and WhiteNoise filename hashing. The `integrity` values come straight
|
|
228
|
+
from the lock and stay valid because the bytes are never edited. The rendered map is cached per
|
|
229
|
+
process and invalidated when the staticfiles manifest changes.
|
|
230
|
+
|
|
231
|
+
A typical deploy is `pyesm sync` → `collectstatic`.
|
|
232
|
+
|
|
233
|
+
Relevant settings (optional):
|
|
234
|
+
|
|
235
|
+
| Setting | Default | Meaning |
|
|
236
|
+
|-----------------------|---------------|-------------------------------------------------------|
|
|
237
|
+
| `PYESM_PROJECT_ROOT` | auto-detected | Directory containing `pyproject.toml` / `pyesm.lock`. |
|
|
238
|
+
| `PYESM_STATIC_PREFIX` | `"pyesm"` | Static path prefix the vendored files live under. |
|
|
239
|
+
|
|
240
|
+
---
|
|
241
|
+
|
|
242
|
+
## Caching & performance
|
|
243
|
+
|
|
244
|
+
- **Global content-addressed cache** at `~/.cache/pyesm/<sha384>`, shared across projects. Override
|
|
245
|
+
the location with the `PYESM_CACHE_DIR` environment variable (or `XDG_CACHE_HOME`).
|
|
246
|
+
- Modules are **hardlinked** from the cache into `output-dir` (a byte copy only when crossing
|
|
247
|
+
filesystems). Bytes are never rewritten.
|
|
248
|
+
- The crawl and the downloads run concurrently on `asyncio` via a single pooled `httpx.AsyncClient`,
|
|
249
|
+
bounded by `concurrency`.
|
|
250
|
+
- A warm-cache `sync` of a small graph completes in well under a second and makes **no network
|
|
251
|
+
calls**.
|
|
252
|
+
|
|
253
|
+
---
|
|
254
|
+
|
|
255
|
+
## Continuous integration
|
|
256
|
+
|
|
257
|
+
`sync` is the command to run in CI and on deploy. It's deterministic and needs no network when the
|
|
258
|
+
cache is warm.
|
|
259
|
+
|
|
260
|
+
```console
|
|
261
|
+
$ pyesm sync --frozen # fail if pyesm.lock is missing or out of date with pyproject.toml
|
|
262
|
+
$ pyesm sync --offline # fail rather than touch the network (requires a warm cache)
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
`--frozen` never mutates the lock, so it's a safe gate against forgetting to commit a lock update.
|
|
266
|
+
|
|
267
|
+
---
|
|
268
|
+
|
|
269
|
+
## Providers
|
|
270
|
+
|
|
271
|
+
No provider requires Node. (JSPM is intentionally excluded: its generator is Node-only.)
|
|
272
|
+
|
|
273
|
+
- **`jsdelivr`** (default): vendors transformed ESM from `cdn.jsdelivr.net/npm/<name>@<ver>/+esm`.
|
|
274
|
+
Because the `+esm` endpoint serves range URLs without redirecting, pyesm pins the exact version via
|
|
275
|
+
jsDelivr's data API before crawling, so a caret range vendors a single pinned copy.
|
|
276
|
+
- **`esmsh`**: vendors from `esm.sh`, using its `?meta` endpoint where available and following
|
|
277
|
+
redirects to pin. esm.sh entry URLs aren't version-pinned in the path; pyesm vendors the frozen
|
|
278
|
+
re-export shim plus its pinned target, all locked by integrity.
|
|
279
|
+
|
|
280
|
+
Switch per-run with `--provider`, or set `provider` in config.
|
|
281
|
+
|
|
282
|
+
---
|
|
283
|
+
|
|
284
|
+
## Limitations
|
|
285
|
+
|
|
286
|
+
- **Runtime-computed dynamic imports** (`import(someVariable)`) can't be discovered statically, so
|
|
287
|
+
their targets aren't vendored; they'd load from the CDN at runtime. Static `import("…literal…")`
|
|
288
|
+
*is* discovered.
|
|
289
|
+
- **`outdated` is a no-op for esm.sh** deps, because esm.sh entry URLs don't pin a version in the URL
|
|
290
|
+
to compare against. jsDelivr pins exactly and reports accurately.
|
|
291
|
+
- CDN output (`+esm`, esm.sh transforms) is **not guaranteed byte-stable** across CDN updates. That's
|
|
292
|
+
fine at serve time because you host your own frozen copy, but a `sync` that finds a hash mismatch
|
|
293
|
+
against a still-pinned URL **fails loudly** rather than silently overwriting.
|
|
294
|
+
|
|
295
|
+
---
|
|
296
|
+
|
|
297
|
+
## Development
|
|
298
|
+
|
|
299
|
+
```console
|
|
300
|
+
$ uv sync # create the venv and install deps
|
|
301
|
+
$ uv run pre-commit install # enable the git hooks (ruff + pyright)
|
|
302
|
+
$ uv run pytest # run the test suite
|
|
303
|
+
$ uv build # build the wheel/sdist
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
Pre-commit runs `ruff format`, `ruff check`, the standard hygiene hooks, and `pyright`. Run them on
|
|
307
|
+
demand with `uv run pre-commit run --all-files`.
|
|
308
|
+
|
|
309
|
+
### Releasing
|
|
310
|
+
|
|
311
|
+
Pushing a `v*` tag (matching the `pyproject.toml` version) builds the sdist + wheel and publishes to
|
|
312
|
+
PyPI via Trusted Publishing (OIDC, no stored token):
|
|
313
|
+
|
|
314
|
+
```console
|
|
315
|
+
$ git tag v0.1.0 && git push --tags
|
|
316
|
+
```
|