Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/turbohtml/_internal/_jsminify.py: 100%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

10 statements  

1""" 

2The JavaScript-minifier options object and its registration with the C core. 

3 

4``JSMinify`` lives here, apart from :mod:`turbohtml._internal._minify`, because handing the type to the 

5extension (``_register_js_minify``) puts it in the C module state, and through it the whole 

6defining module. ``_minify`` pulls in the render layer and module-level default instances; that 

7reference graph keeps the extension's module state alive at interpreter shutdown, so its teardown 

8(and the freelist drain it runs) never executes. Registering from this module, which imports only 

9``_html``, keeps the graph small enough to collect, the way :mod:`turbohtml.extract._article` and 

10:mod:`turbohtml.extract._structured_data` register their record types. 

11""" 

12 

13from __future__ import annotations 

14 

15from dataclasses import dataclass 

16 

17from turbohtml._html import _register_js_minify 

18 

19__all__ = ["JSMinify"] 

20 

21 

22@dataclass(frozen=True, slots=True) 

23class JSMinify: 

24 """ 

25 Which optional JavaScript-minifier passes to run. 

26 

27 Whitespace and numeric-literal minification is unconditional, as is comment removal 

28 -- except ``/*! ... */`` bang comments and any comment carrying an ``@license`` or 

29 ``@preserve`` annotation, which are kept byte-exact as a leading banner so a license 

30 header survives (the same rule the CSS minifier applies). ``mangle`` renames local 

31 bindings to short names (the bulk of the size win) and ``fold`` runs constant folding 

32 and dead-code elimination; turning either off keeps that aspect of the source readable 

33 (e.g. ``mangle=False`` for debuggable output). 

34 

35 :param mangle: rename local bindings to short names. 

36 :param fold: constant-fold and eliminate dead code. 

37 :raises TypeError: if constructed with an unexpected or extra argument. 

38 """ 

39 

40 mangle: bool = True 

41 fold: bool = True 

42 

43 

44# Hand the type to the serializer so Minify(minify_js=...) reaches it with a module-state 

45# pointer load (speed over a per-call import), not a global; mirrors the other type registers. 

46_register_js_minify(JSMinify)