Skip to content

Commit

Permalink
feat: reduce install size by setting avif and svg dependencies op…
Browse files Browse the repository at this point in the history
…tional
  • Loading branch information
boidolr committed Nov 4, 2023
1 parent c36f78a commit 3016477
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
language: python
entry: optimize-avif
types: [avif]
additional_dependencies: [".[avif]"]

- id: optimize-jpg
name: optimize jpgs
Expand All @@ -25,6 +26,7 @@
language: python
entry: optimize-svg
types: [svg]
additional_dependencies: [".[svg]"]

- id: optimize-webp
name: optimize webps
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ $(VENV)/$(MARKER): $(VENV)
## venv : Initialize virtual environment with dependencies.
.PHONY: venv
venv: $(VENV)/$(MARKER)
$(VENV)/pip install -q .[tests]
$(VENV)/pip install -q .[tests,svg,avif]


.PHONY: all
Expand Down
7 changes: 6 additions & 1 deletion pre_commit_images/optimize_avif.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
from pathlib import Path
from typing import IO

import pillow_avif # noqa: F401
from PIL import Image

from .optimizer import _optimize_images

try:
import pillow_avif # noqa: F401
except ImportError:
warnings.warn('Missing `pillow_avif` dependency, install optional "[avif]" dependency group')
sys.exit(1)


def main(argv: Sequence[str] | None = None) -> int:
parser = argparse.ArgumentParser()
Expand Down
13 changes: 9 additions & 4 deletions pre_commit_images/optimize_svg.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
#!/usr/bin/env python3
import argparse
import pathlib
import sys
import warnings
from collections.abc import Sequence
from pathlib import Path
from typing import IO

from scour import scour

from .optimizer import _optimize_images

try:
from scour import scour
except ImportError:
warnings.warn('Missing `scour` dependency, install optional "[svg]" dependency group')
sys.exit(1)


def main(argv: Sequence[str] | None = None) -> int:
parser = argparse.ArgumentParser()
Expand All @@ -22,7 +27,7 @@ def main(argv: Sequence[str] | None = None) -> int:
)
args = parser.parse_args(argv)

def optimize(source: Path, target: IO[bytes]) -> None:
def optimize(source: pathlib.Path, target: IO[bytes]) -> None:
data = source.read_text(encoding="utf-8")
options = {
"enable_viewboxing": True,
Expand Down
8 changes: 6 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ classifiers = [
requires-python = ">=3.10"
dependencies = [
"pillow==10.1.0",
"pillow-avif-plugin==1.4.1",
"scour==0.38.2",
]

[project.optional-dependencies]
avif = [
"pillow-avif-plugin==1.4.1",
]
svg = [
"scour==0.38.2",
]
dev = [
"pre-commit",
"ruff",
Expand Down
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ envlist = py310,py311,py312,pre-commit
isolated_build = True

[testenv]
extras = tests
extras = tests,svg,avif
commands =
pytest {posargs:tests}

Expand All @@ -13,6 +13,6 @@ deps = pre-commit
commands = pre-commit run --all-files

[testenv:static]
extras = tests
extras = tests,svg,avif
commands =
pytest --ignore tests

0 comments on commit 3016477

Please sign in to comment.