Skip to content

Commit 307f84d

Browse files
authored
Merge pull request #160 from akx/named-assets
Support named assets
2 parents c6df9f5 + d478fd7 commit 307f84d

File tree

8 files changed

+69
-0
lines changed

8 files changed

+69
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ This will add a `<script>` tag including your JS/TS script :
170170
- The path must be relative to your `root` key inside your ViteJS config file.
171171
- The path must be a key inside your manifest file `manifest.json` file
172172
generated by ViteJS.
173+
- Alternatively, the path can be an asset name (e.g. `"name": "foo"` in your manifest).
174+
If you set up multiple entrypoints (`build.rollupOptions.input`) in your Vite config,
175+
e.g. by following [the Multi-Page App](https://vite.dev/guide/build.html#multi-page-app)
176+
docs, this would be the key in the entrypoint object.
173177
- In general, this path does not require a `/` at the beginning
174178
(follow your `manifest.json` file).
175179

django_vite/core/asset_loader.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class ManifestEntry(NamedTuple):
6161
"""
6262

6363
file: str
64+
name: Optional[str] = None
6465
src: Optional[str] = None
6566
isEntry: Optional[bool] = False
6667
isDynamicEntry: Optional[bool] = False
@@ -185,6 +186,8 @@ def _parse_manifest(self) -> ParsedManifestOutput:
185186
}
186187
manifest_entry = ManifestEntry(**filtered_manifest_entry_data)
187188
entries[path] = manifest_entry
189+
if manifest_entry.name:
190+
entries[manifest_entry.name] = manifest_entry
188191
if self.legacy_polyfills_motif in path:
189192
legacy_polyfills_entry = manifest_entry
190193

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
globalThis.sayOne=()=>"one";
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"one.js": {
3+
"file": "assets/one-90jdqqZN.js",
4+
"name": "one",
5+
"src": "one.js",
6+
"isEntry": true
7+
}
8+
}

tests/data/named_assets/one.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
globalThis.sayOne = () => 'one';
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "named_assets",
3+
"description": "Example that generates a manifest with named assets",
4+
"private": true,
5+
"version": "0.0.0",
6+
"type": "module",
7+
"devDependencies": {
8+
"vite": "^6.2.0"
9+
}
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import path from 'node:path';
2+
3+
export default {
4+
build: {
5+
manifest: 'manifest.json',
6+
rollupOptions: {
7+
input: {
8+
'one': path.resolve('./one.js'),
9+
},
10+
},
11+
},
12+
};

tests/tests/test_asset_loader.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,33 @@ def test_parse_manifest_during_dev_mode(dev_mode_true):
156156
def test_load_dynamic_import_manifest(patch_settings):
157157
warnings = check_loader_instance()
158158
assert len(warnings) == 0
159+
160+
161+
@pytest.mark.parametrize(
162+
"patch_settings",
163+
[
164+
{
165+
"DJANGO_VITE": {
166+
"default": {
167+
"dev_mode": False,
168+
"manifest_path": Path(settings.STATIC_ROOT).parent
169+
/ "named_assets"
170+
/ "dist"
171+
/ "manifest.json",
172+
},
173+
},
174+
},
175+
],
176+
indirect=True,
177+
)
178+
def test_named_assets(patch_settings):
179+
"""
180+
Test that assets can be loaded via name.
181+
"""
182+
warnings = check_loader_instance()
183+
assert len(warnings) == 0
184+
loader = DjangoViteAssetLoader.instance()
185+
by_name = loader.generate_vite_asset("one")
186+
by_src = loader.generate_vite_asset("one.js")
187+
assert "<script" in by_name
188+
assert by_src == by_name

0 commit comments

Comments
 (0)