@@ -14,8 +14,8 @@ def __init__(self, source_dir: ContextDir = 'input_dir'):
14
14
def __call__ (self , path : Path , output_paths : list [Path ]):
15
15
parent_dir = self .context [self .source_dir ]
16
16
data = '\n \n ' .join (
17
- (parent_dir / filename . strip () ).read_text (self .encoding )
18
- for filename in path .open () if filename
17
+ (parent_dir / f ).read_text (self .encoding )
18
+ for filename in path .open () if ( f := filename . strip ()) and not f . startswith ( '#' )
19
19
)
20
20
21
21
for o_path in output_paths :
@@ -27,15 +27,37 @@ def __call__(self, path: Path, output_paths: list[Path]):
27
27
28
28
class CSSMinifierStep (Step ):
29
29
encoding = 'utf-8'
30
+
30
31
@classmethod
31
32
def get_dependencies (cls ):
32
33
return {
33
- pip_dependency ('csscompressor' ),
34
+ pip_dependency ('lightningcss' )
34
35
}
35
36
37
+ def __init__ (self ,
38
+ error_recovery : bool = False ,
39
+ parser_flags : dict [str , bool ] | None = None ,
40
+ unused_symbols : set [str ] | None = None ,
41
+ browsers_list : list [str ] | None = ['defaults' ],
42
+ minify : bool = True ):
43
+
44
+ self .error_recovery = error_recovery
45
+ self .parser_flags = parser_flags or {}
46
+ self .unused_symbols = unused_symbols
47
+ self .browsers_list = browsers_list
48
+ self .minify = minify
49
+
36
50
def __call__ (self , path : Path , output_paths : list [Path ]):
37
- import csscompressor
38
- data = csscompressor .compress (path .read_text (self .encoding ))
51
+ import lightningcss
52
+ data = lightningcss .process_stylesheet (
53
+ path .read_text (self .encoding ),
54
+ filename = str (path ),
55
+ error_recovery = self .error_recovery ,
56
+ parser_flags = lightningcss .calc_parser_flags (** self .parser_flags ),
57
+ unused_symbols = self .unused_symbols ,
58
+ browsers_list = self .browsers_list ,
59
+ minify = self .minify
60
+ )
39
61
for o_path in output_paths :
40
62
o_path .parent .mkdir (parents = True , exist_ok = True )
41
63
output_paths [0 ].write_text (data , self .encoding )
@@ -45,16 +67,30 @@ def __call__(self, path: Path, output_paths: list[Path]):
45
67
46
68
class HTMLMinifierStep (Step ):
47
69
encoding = 'utf-8'
70
+ minify_css = False
71
+ minify_js = False
48
72
49
73
@classmethod
50
74
def get_dependencies (cls ):
51
75
return {
52
- pip_dependency ('minify-html-onepass' , check_name = 'minify_html_onepass' ),
76
+ (
77
+ pip_dependency ('minify-html-onepass' , check_name = 'minify_html_onepass' )
78
+ | pip_dependency ('minify-html' , check_name = 'minify_html' )
79
+ ),
53
80
}
54
81
55
82
def __call__ (self , path : Path , output_paths : list [Path ]):
56
- import minify_html_onepass
57
- data = minify_html_onepass .minify (path .read_text (self .encoding ))
83
+ params = {'minify_css' : self .minify_css , 'minify_js' : self .minify_js }
84
+ try :
85
+ from minify_html_onepass import minify
86
+ except ImportError :
87
+ from minify_html import minify
88
+ params |= {
89
+ 'do_not_minify_doctype' : True ,
90
+ 'ensure_spec_compliant_unquoted_attribute_values' : True ,
91
+ 'keep_spaces_between_attributes' : True ,
92
+ }
93
+ data = minify (path .read_text (self .encoding ), ** params )
58
94
for o_path in output_paths :
59
95
o_path .parent .mkdir (parents = True , exist_ok = True )
60
96
output_paths [0 ].write_text (data , self .encoding )
0 commit comments