Skip to content

Commit

Permalink
Switch to lightningcss from csscompressor (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
pydsigner committed Aug 11, 2023
1 parent bc1a7fd commit 3bcad39
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ markdown = ["anchovy[jinja]", "markdown_it_py>=3.0.0"]
css = ["tinycss2>=1.1.1"]
pretty = ["rich>=12.5.1"]
pillow = ["Pillow>=9.2.0"]
minify = ["csscompressor>=0.9.5", "minify-html-onepass>=0.11.1"]
minify = ["lightningcss>=0.1.1,<1.0", "minify-html-onepass>=0.11.1"]
web = ["anchovy[markdown]", "anchovy[css]", "anchovy[pillow]", "anchovy[minify]"]
base = ["anchovy[web]", "anchovy[pretty]"]
# Currently, [all] is the same as [base]; this will change in the future if
Expand Down
28 changes: 25 additions & 3 deletions src/anchovy/minify.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,37 @@ def __call__(self, path: Path, output_paths: list[Path]):

class CSSMinifierStep(Step):
encoding = 'utf-8'

@classmethod
def get_dependencies(cls):
return {
pip_dependency('csscompressor'),
pip_dependency('lightningcss')
}

def __init__(self,
error_recovery: bool = False,
parser_flags: dict[str, bool] | None = None,
unused_symbols: set[str] | None = None,
browsers_list: list[str] | None = ['defaults'],
minify: bool = True):

self.error_recovery = error_recovery
self.parser_flags = parser_flags or {}
self.unused_symbols = unused_symbols
self.browsers_list = browsers_list
self.minify = minify

def __call__(self, path: Path, output_paths: list[Path]):
import csscompressor
data = csscompressor.compress(path.read_text(self.encoding))
import lightningcss
data = lightningcss.process_stylesheet(
path.read_text(self.encoding),
filename=str(path),
error_recovery=self.error_recovery,
parser_flags=lightningcss.calc_parser_flags(**self.parser_flags),
unused_symbols=self.unused_symbols,
browsers_list=self.browsers_list,
minify=self.minify
)
for o_path in output_paths:
o_path.parent.mkdir(parents=True, exist_ok=True)
output_paths[0].write_text(data, self.encoding)
Expand Down

0 comments on commit 3bcad39

Please sign in to comment.