Skip to content

Commit bfad7cf

Browse files
👌 IMPROVE: Add hash method to Jinja context (#5)
1 parent f468dd5 commit bfad7cf

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

tests/example_src/example4.j2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ "src/example1.scss" | compiled_name }}?digest={{ "src/example1.scss" | hash }}

tests/test_run_compile.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,32 @@ def test_jinja_basic(src_folder: Path):
128128
assert "b" in (src_folder / "dist" / "example1.txt").read_text("utf8")
129129

130130

131+
def test_jinja_hash(src_folder: Path):
132+
config = create_config(
133+
src_folder,
134+
{
135+
"sass": {
136+
"files": {
137+
"src/example1.scss": "dist/example1.css",
138+
},
139+
"precision": 5,
140+
"sourcemap": True,
141+
"format": "compressed",
142+
"encoding": "utf8",
143+
},
144+
"jinja": {
145+
"files": {"src/example4.j2": "dist/example4.txt"},
146+
},
147+
},
148+
)
149+
result = CliRunner().invoke(run_compile, ["-c", str(config)])
150+
assert result.exit_code == 3, result.output
151+
assert (src_folder / "dist" / "example4.txt").exists(), result.output
152+
assert "example1.css?digest=" in (src_folder / "dist" / "example4.txt").read_text(
153+
"utf8"
154+
)
155+
156+
131157
def test_full(src_folder: Path):
132158
config = create_config(
133159
src_folder,

web_compile/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,14 @@ def _get_compiled_name(path):
424424
raise KeyError(f"No compiled path: {path}")
425425
return file_map.get(Path(path)).name
426426

427+
def _get_hash(path):
428+
if not path or Path(path) not in file_map:
429+
raise KeyError(f"No compiled path: {path}")
430+
input_path = root / Path(path)
431+
return hash_file(input_path.read_text("utf8"))
432+
427433
jinja_env.filters["compiled_name"] = _get_compiled_name
434+
jinja_env.filters["hash"] = _get_hash
428435
for input_str, output_str in (jinja_files or {}).items():
429436
input_path = root / input_str
430437
output_path = root / output_str

0 commit comments

Comments
 (0)